cli flags
# Node.js
const yargs = require("yargs");
const { foo = "default value", qux = false } = yargs.argv;
console.log("foo:", foo);
console.log("qux:", qux);
1
2
3
4
5
2
3
4
5
Output
$ node examples/cli_flags.js --foo='bar' --qux=true
foo: bar
qux: true
1
2
3
2
3
# Go
package main
import (
"flag"
"fmt"
)
func main() {
var foo string
flag.StringVar(&foo, "foo", "default value", "a string var")
var qux bool
flag.BoolVar(&qux, "qux", false, "a bool var")
flag.Parse()
fmt.Println("foo:", foo)
fmt.Println("qux:", qux)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Output
$ go run examples/cli_flags.go -foo='bar' -qux=true
foo: bar
qux: true
1
2
3
2
3
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22