Go Command Line Parsing: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 6: | Line 6: | ||
Go provides support for command line "flag" parsing in the [[Go Package flag|flag package]]. | Go provides support for command line "flag" parsing in the [[Go Package flag|flag package]]. | ||
Flags (example: "-path=/something" or "-max=100") are declared with: | |||
<pre> | |||
sPtr := flag.Int("s", ".", "the path vale") | |||
iPtr := flag.Int("i", 1, "the i value") | |||
</pre> | |||
Note that the flag declaration methods return ''pointers'' that can be used to read the value of the flags after parsing: | |||
<pre> | |||
flag.Parse() | |||
</pre> | |||
Then their value is read from the previously obtained pointers: | |||
<pre> | |||
fmt.Println("flag s value: ", *sPtr) | |||
fmt.Println("flag i value: ", *iPtr) | |||
</pre> | |||
<pre> | |||
fmt.Println("flag s value: ", *sPtr) | |||
fmt.Println("flag i value: ", *iPtr) | |||
</pre> | |||
=Example= | =Example= |
Revision as of 23:51, 1 April 2016
Internal
Overview
Go provides support for command line "flag" parsing in the flag package.
Flags (example: "-path=/something" or "-max=100") are declared with:
sPtr := flag.Int("s", ".", "the path vale") iPtr := flag.Int("i", 1, "the i value")
Note that the flag declaration methods return pointers that can be used to read the value of the flags after parsing:
flag.Parse()
Then their value is read from the previously obtained pointers:
fmt.Println("flag s value: ", *sPtr) fmt.Println("flag i value: ", *iPtr)
fmt.Println("flag s value: ", *sPtr) fmt.Println("flag i value: ", *iPtr)