当前位置:网站首页>Command-Line
Command-Line
2022-04-21 06:37:00 【熊小憨】
命令行参数
argsWithProg := os.Args
argsWithoutProg := os.Args[1:]
fmt.Println(argsWithProg)
fmt.Println(argsWithoutProg)
命令行中运行程序并输入:
./command-line-arguments a b c d
命令行输出:
[./command-line-arguments a b c d]
[a b c d]
命令行标志
wordPtr := flag.String("word", "foo", "a string")
numbPtr := flag.Int("numb", 42, "an int")
forkPtr := flag.Bool("fork", false, "a bool")
var svar string
flag.StringVar(&svar, "svar", "bar", "a string var")
flag.Parse()
fmt.Println("word:", *wordPtr)
fmt.Println("numb:", *numbPtr)
fmt.Println("fork:", *forkPtr)
fmt.Println("svar:", svar)
fmt.Println("tail:", flag.Args())
% ./command-line-flags -word=opt -numb=7 -fork -svar=flag
word: opt
numb: 7
fork: true
svar: flag
tail: []
% ./command-line-flags -word=opt
word: opt
numb: 42
fork: false
svar: bar
tail: []
% ./command-line-flags -word=opt a1 a2 a3
word: opt
numb: 42
fork: false
svar: bar
tail: [a1 a2 a3]
% ./command-line-flags -word=opt a1 a2 a3 -numb=7
word: opt
numb: 42
fork: false
svar: bar
tail: [a1 a2 a3 -numb=7]
% ./command-line-flags -h
Usage of ./command-line-flags:
-fork
a bool
-numb int
an int (default 42)
-svar string
a string var (default “bar”)
-word string
a string (default “foo”)
% ./command-line-flags -wat
flag provided but not defined: -wat
Usage of ./command-line-flags:
-fork
a bool
-numb int
an int (default 42)
-svar string
a string var (default “bar”)
-word string
a string (default “foo”)
命令行子参数
fooCmd := flag.NewFlagSet("foo", flag.ExitOnError)
fooEnable := fooCmd.Bool("enable", false, "enable")
fooName := fooCmd.String("name", "", "name")
barCmd := flag.NewFlagSet("bar", flag.ExitOnError)
barLevel := barCmd.Int("level", 0, "level")
if len(os.Args) < 2 {
fmt.Println("expected 'foo' or 'bar' subcommands")
os.Exit(1)
}
switch os.Args[1] {
case "foo":
fooCmd.Parse(os.Args[2:])
fmt.Println("subcommand 'foo'")
fmt.Println(" enable:", *fooEnable)
fmt.Println(" name:", *fooName)
fmt.Println(" tail:", fooCmd.Args())
case "bar":
barCmd.Parse(os.Args[2:])
fmt.Println("subcommand 'bar'")
fmt.Println(" level:", *barLevel)
fmt.Println(" tail:", barCmd.Args())
default:
fmt.Println("expected 'foo' or 'bar' subcommands")
os.Exit(1)
}
% ./command-line-subcommands foo -enable -name=joe a1 a2
subcommand ‘foo’
enable: true
name: joe
tail: [a1 a2]
% ./command-line-subcommands bar -level 8 a1
subcommand ‘bar’
level: 8
tail: [a1]
% ./command-line-subcommands bar -enable a1
flag provided but not defined: -enable
Usage of bar:
-level int
level
版权声明
本文为[熊小憨]所创,转载请带上原文链接,感谢
https://blog.csdn.net/suma110/article/details/124268434
边栏推荐
- 策略模式(2.28-3.6)
- Three layer switch and router are connected to the Internet
- Cs5518, Mipi to dual LVDS, replacement: gm8775 of Guoteng, tc358775 of Toshiba, dual LVDS, domestic perfect replacement, DSI to dual channel LVDS, LVDS clock frequency up to 154mhz, 1920 x1200
- Longxun series video conversion, lt9211, lt8918, functions: LVDS to bt656, LVDS to Mipi (CSI \ DSI) RGB to Mipi (DSI \ CSI) bt656 \ 601 \ 1120 to hdmi1 4\DVI
- NP and OSPF default routes
- NP、OSPF监测调试
- IPV4-IGP
- NP and OSPF monitoring and commissioning
- MySQL common table structure
- IPV4-IGP
猜你喜欢
随机推荐
NP、OSPF Stub区域
PIM-DM
NP and OSPF monitoring and commissioning
Detailed explanation of the whole evolution process and architecture design of large websites
状态模式(4.4-4.10)
金融信息安全实训——22/4/18
File system structure analysis and data recovery
Defining “Disinformation“(定义“虚假信息”)
About multithreading
2020-12-24
Sort method ----- > Hill sort, heap sort
Nsctf - some topics WP
Guanghuaxin audio codec Daquan cjc4344, cjc8988, cjc5340, cjc6811 guanghuaxin audio codec, cjc4344, cjc8988, cs5340
树状数组
Financial information security training - 22 / 4 / 18
写入数据进入磁盘文件代码示例如下:
【计组】冯诺依曼体系结构
論文閱讀:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi
Convergencewarning: linear failed to converge, increase the number of iterations
NP, OSPF route aggregation









