LoginSignup
28
22

More than 3 years have passed since last update.

golang でコマンドライン引数を使う

Last updated at Posted at 2020-03-28

2種類のやり方があります

  • os パッケージの Args を使う
  • flag パッケージを使う

tl; dr

  • flag を使うのが良さそう
    • 型指定やオプショナル引数、デフォルト値の設定等、色々できるから

flag

使い方

main.go
package main

import (
    "flag"
    "fmt"
)

func main() {
    flag.Parse()
    fmt.Println(flag.Args())
}
$ go run main.go foo bar baz
[foo bar baz]
  • 解析には flag.Parse() を呼び出します
    • flag.Parse の内部では os.Args[1:] が渡されて解析される
      • os.Args[0:] には実行コマンドが格納されている
  • Parse は全ての flag が定義された後かつ、いづれかの flag にアクセスする前にコールする必要がある
    • これを守らないと flag provided but not defined: -hogehoge みたいなエラーが出る
//github.com/golang/go/blob/master/src/flag/flag.go#L963-L966
// Parse parses flag definitions from the argument list, which should not
// include the command name. Must be called after all flags in the FlagSet
// are defined and before flags are accessed by the program.
// The return value will be ErrHelp if -help or -h were set but not defined.

オプショナルな引数を使う

こんな感じ
package main

import (
        "fmt"
        "flag"
        "os"
)

func main(){
        f := flag.String("flag1", "hoge", "flag 1")
        flag.Parse()

        fmt.Println("Hello %s", *f)
}
$ go run sample.go -flag1=fuga
Hello fuga
$ go run sample.go
Hello hoge
  • オプションを定義するには以下の二つの方法がある
    • var str = flag.String("オプション名", "初期値", "説明")
    • flag.StringVar(&str, "オプション名", "初期値", "説明")
  • 戻り値がポインタなので、使うときは実体参照する
  • オプションの指定方法
    • -flag
    • -flag=x
    • -flag x // non-boolean flags only
    • (※ハイフンは二つでも認識されるらしい)

os.Args

この辺を読む

go test で flag を使う

VSCode の拡張で使いたい

  • 以下のような設定を setting.json に記述する
  • VSCode のエディタ上のテスト関数の上に表示される run test ボタンでテスト実行した際に、自動でフラグが付加される
"go.testFlags": [
    "-flag1=hoge",
    "-flag2=fuga"
  ]
28
22
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
22