-ab=0 --value1=0 --value2 1
This kind of thing
-a -b=0 --value=0 --value2=1
Will do this
(The number of values must be specified in advance with FlagN
--Flags like -a
--Priced flags like -a = b
--Unequal priced flags, such as -a b
--Flags that group single characters, such as -abc
(expanded as -a -b -c)
--A flag with a value above, such as -abc = d
. Only the last flag (c in this case) is set to a value
--A version without the above equals, such as -abc d
--Treats values that do not start with -
and are not flag values as Arg
--Comma separated values are recognized as multiple values. That is, if FlagN of a is 2, then --a = b, c
and --a b c
are the same.
--Even if a flag with the same name is specified multiple times, it remains the same, but it can be merged with a method
--FlagN specifies the number of values, which is currently ignored for comma separated values.
Create an App with New, call the setting method, and then call the normalize method.
ToStrings is attached to the method whose return value is string slice. If it is not attached, [] nzargv.Value
is returned. Since Value has Name and Value, you can retrieve the value of the flag.
For more information, see GitHub or go.dev.
Useful libraries for creating command line apps, such as spf13 / cobra and urafave / cli There are many.
However, these libraries are just for creating CLI apps, and they often feel over-functional, making them difficult to use when you want to create a slightly special application.
However, handling command line arguments is more cumbersome than you might think, and it's unproductive to write this every time.
So, when processing command line arguments, I created a library that normalizes in a form that is easy to handle in advance.
Recommended Posts