Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flags showing up before subcommands #17

Closed
phinze opened this issue May 2, 2015 · 8 comments
Closed

Flags showing up before subcommands #17

phinze opened this issue May 2, 2015 · 8 comments

Comments

@phinze
Copy link
Contributor

phinze commented May 2, 2015

Filing this for hashicorp/terraform#1773

Current behavior: Except for help/version, flags before a subcommand are silently ignored.

Possible solutions:

(A) Flags before subcommand cause error
(B) Flags before subcommand are passed in as subcommand args

Unsure what the better behavior. Originally I thought (A) but then I swung to (B) after staring at it a bit.

Thoughts?

@failshell
Copy link

At the very least, it should warn the user about it being ignored.

In the best case, it should follow the usual UNIX tool: flags can be used in any order.

@mitchellh
Copy link
Owner

I'm going to go with (A) because with git this is an error and it makes it cleaner later to implement top-level flags. Doing this now.

@fatih
Copy link
Contributor

fatih commented Jun 1, 2015

Im using cli too and the way I use is the b option. It also feels idiomatic because I can pass global flags. Example:

$ foo --no-color subcommand -bar -quz 0

Here --no-color is independent of subcommands. But to do this, I'm all my global flags before I pass it to cli. I even created a package for it, so filtering can be made easily: https://github.com/fatih/flags

Having said that, as seen it complicates things a little bit, so I agree with @mitchellh here, however it's also nice to be able to use global flags, such as --no-color.

@mitchellh
Copy link
Owner

@fatih Yeah we have some global args for some of our projects as well. We preprocess the os.Args and remove the args that we use, and pass the rest into cli. So this approach will work.

@mitchellh
Copy link
Owner

Done!

@fatih
Copy link
Contributor

fatih commented Jun 1, 2015

@mitchellh It seems git allows flags before subcommand.

usage: git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [-c name=value] [--help]
           <command> [<args>]

Any flag before <command> is valid. So I think we should allow flags before commands, though it needs to be known to the cli. I think we should add a feature to allow an extend the current global flags --version and --help. What do you think?

@mitchellh
Copy link
Owner

@fatih Git allows only global flags. It doesn't allow subcommand flags before it.

I think extending the global flags might be useful, but there are easy workarounds (pre-processing the arg slice for example).

@fatih
Copy link
Contributor

fatih commented Jun 1, 2015

@mitchellh yeah, I'm saying that --version and --help are both global flags. Just saying we should have a feature so people can add their own global flags. Because it's really tedious to implement it manually by ourselves.

So for example, say for packer the output would be in the form :

usage: packer [--template-path] [--version] [--help] <command> [<args>]

Available commands are:
    build       build image(s) from template
    fix         fixes templates from old versions of packer
    inspect     see components of a template
    push        push template files to a Packer build service
    validate    check that a template is valid
    version     Prints the Packer version

Note the --template-path global flag. This is just an example, but it would have the advantages:

  1. Help would auto generate the global flags
  2. No manual pre-processing of commands are needed, this would be built in
  3. --version and --help wouldn't be the only global flags, people would have the choice to add their own

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants