integrii / flaggy

Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.
The Unlicense
856 stars 30 forks source link

Separate subcommand flags from global flags #68

Open freb opened 4 years ago

freb commented 4 years ago

I asked about this on the slack channel a while back. Since it is one of only two issues I have with flaggy, I thought I'd ask about it here.

When exploring a CLI program I start by viewing the help at the root command, I figure out which subcommand I want to use, then I view its available options. Currently in the help output flaggy groups all flags together in the Flags section. It is often difficult to figure out which flags apply specifically to the subcommand, especially when you have many global flags.

This is the default output:

go run main.go policy delete mypolicy -h
delete

  Usage:
    delete [policy_name]

  Positional Variables: 
    policy_name   (Required)

  Flags: 
       --version      Displays the program version string.
    -h --help         Displays help with available flag, subcommand, and positional value parameters.
    -a --associated-data   also delete associated data
       --addr         address of server

I'd like to see something like this instead:

delete

  Usage:
    delete [policy_name]

  Positional Variables: 
    policy_name   (Required)

  Subcommand Flags:
    -a --associated-data   also delete associated data

  Flags: 
       --version      Displays the program version string.
    -h --help         Displays help with available flag, subcommand, and positional value parameters.
       --addr         address of server

Here is some output from the Dgraph command line, which I believe uses Cobra. The root help shows a Flags section, but the help for a subcommand moves that flags section to Global Flags and uses the Flags section for the subcommand:

> $ dgraph acl --help
[Decoder]: Using assembly version of decoder
Run the Dgraph acl tool

Usage:
  dgraph acl [command]

Available Commands:
  add         Run Dgraph acl tool to add a user or group
  del         Run Dgraph acl tool to delete a user or group
  info        Show info about a user or group
  mod         Run Dgraph acl tool to modify a user's password, a user's group list, or agroup's predicate permissions

Flags:
  -a, --alpha string               Dgraph Alpha gRPC server address (default "127.0.0.1:9080")
  -h, --help                       help for acl
      --tls_cacert string          The CA Cert file used to verify server certificates.
      --tls_use_system_ca          Include System CA into CA Certs. (default true)

Global Flags:
      --alsologtostderr                  log to standard error as well as files
      --bindall                          Use 0.0.0.0 instead of localhost to bind to all addresses on local machine. (default true)
      --block_rate int                   Block profiling rate. Must be used along with block profile_mode
      --config string                    Configuration file.

Use "dgraph acl [command] --help" for more information about a command.

It's been a while since I've dug into the code. I'm curious if the required information is available to do this in a template (which I don't think it is). And how hard you think it would be make that expose that to the help template.

integrii commented 4 years ago

This looks really useful and should be do-able by simply adding some more metadata to our flags and editing the default help template.