jawher / mow.cli

A versatile library for building CLI applications in Go
MIT License
872 stars 55 forks source link

Request: support enum flags #5

Open insasho opened 9 years ago

insasho commented 9 years ago

This library is off to a great start! Support for enumerations is a common feature of flag parsing libraries and it would be great if mow.cli could support this too.

Proposed design:

cmd.EnumOpt("format", "printable", []string{"printable", "binary"}, "help text")

Per-option help text could be set via the struct variants of the Enum methods.

My specific use case is that I need to add a flag that will support many different values over time but that only currently supports two. For example, the --format flag supports only printable and binary today but it might later need base64. I could use multiple boolean flags but this would require me to implement my own default-value and validation logic (undesirable).

Another use case is the --net flag for docker run:

--net="bridge"   : Set the Network mode for the container
                              'bridge': creates a new network stack for the container on the docker bridge
                              'none': no networking for this container
                              'container:<name|id>': reuses another container network stack
                              'host': use the host network stack inside the container

Thanks.

jawher commented 9 years ago

Thanks for the feedback.

Good proposal, adding it to the to do list.

gwatts commented 7 years ago

Not as neat as the builtin type proposed here, but I implemented something similar using the new(ish) custom type feature https://godoc.org/github.com/gwatts/flagvals#OneOfString

eg.

netMode := flagvals.NewOneOfString("bridge", "none", "container", "host)
netMode.Value = "bridge" // set a default
cmd.Var(cli.VarOpt{
    Name: "net",
    Desc: "Set the Network mode for the container.  Either bridge, none, container or host",
    Value: netMode,
})