Thank you!
It does handle subcommands, I should write an example of that. In short, you
use RegisterNew to create a new getopt.Set or RegisterSet to register with a
previously created set. You will have to call set.Parse to parse the args to
the subcommand. If you need to parse a particular set multiple times that has
default values then you will want to use Dup. Also, you probably want a named
structure. Something like this (beware, I have not compiled this code):
Example with no global defaults
type cmdOpts struct { … }
v, set := options.RegisterNew(“command”, &cmdOpts{})
opts := v.(*cmdOpts)
set.Parse(args)
Example with global and local defaults:
type cmdOpts struct {
Name string `getopt:"--name=NAME name of the widget"`
Count int `getopt:"--count -c=COUNT number of widgets"`
}
var cmdGlobalOpts = &cmdOpts {
Name: “global”,
}
opts := options.Dup(&cmdGlobalOpts).(*cmdOpts)
opts.Count = 42
v, set := options.RegisterNew(“command”, opts)
set.Parse(args)
I will try to get an example added to the codebase in the next week or so.
BTW, my hope is that in most common cases you can just use the options
packages. You can also use github.com/pborman/getopt/v2 in conjunction with
the options package if need be.
-Paul
From: <[email protected]> on behalf of "[email protected]"
<[email protected]>
Date: Wednesday, April 10, 2019 at 4:00 AM
To: golang-nuts <[email protected]>
Subject: [go-nuts] Re: Easy to use (getopt based) flags package
looks lovely, how do you manage sub commands ?
On Wednesday, April 10, 2019 at 2:24:21 AM UTC+2, Paul Borman wrote:
I have never been quite happy with the flags packages available for Go,
including the standard flags package as well as my own getopt packages (v1 and
v2). They are just too cumbersome to use. I finally have come up with a
solution that I like.
github.com/pborman/options<https://godoc.org/github.com/pborman/options>
The command line options are declared as a simple structure with struct tags,
for example:
// Declare the command line flags. The help information does not need to line
up,
// I just think it looks prettier this way.
var opts = struct {
Help options.Help `getopt:"--help display help"`
Name string `getopt:"--name=NAME name of the widget"`
Count int `getopt:"--count -c=COUNT number of widgets"`
Verbose bool `getopt:"-v be verbose"`
Timeout time.Duration `getopt:"--timeout duration of run"`
}{
Name: “gopher”,
}
func main() {
// Assign args to the positional parameters.
args := options.RegisterAndParse(&opts)
fmt.Printf(“Name is: %s\n”, opts.Name)
…
}
The command usage displayed by passing —help to the above program will look
something like:
Usage: prog [-v] [-c COUNT] [--help] [--name NAME] [--timeout value]
[parameters ...]
-c, --count=COUNT number of widgets
--help display help
--name=NAME name of the widget [gopher]
--timeout=value
duration of run
-v be verbose
The package is built on top of the
github.com/pborman/getopt/v2<https://godoc.org/github.com/pborman/getopt/v2>
package and as such they can be used together. The options package also
supports reading command line arguments from a file by using the options.Flags
type.
I hope that some of you might find this helpful in writing command line
programs.
-Paul
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
[email protected]<mailto:[email protected]>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.