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

Support for integer flags like -6 #1462

Open
corlinp opened this issue Jul 23, 2021 · 2 comments
Open

Support for integer flags like -6 #1462

corlinp opened this issue Jul 23, 2021 · 2 comments
Labels
area/flags-args Changes to functionality around command line flags and args kind/feature A feature request for cobra; new or enhanced behavior

Comments

@corlinp
Copy link

corlinp commented Jul 23, 2021

Most compression utilities do something like this to allow users to set compression level

> gzip --help

 -1 ... -9             set compression level

How can one implement this in Cobra without a bunch of repeated .Flags() entries?

@corlinp corlinp changed the title Better support for integer flags like -6 Support for integer flags like -6 Jul 23, 2021
@github-actions
Copy link

This issue is being marked as stale due to a long period of inactivity

@johnSchnake
Copy link
Collaborator

FWIW On Mac the help for gzip looks different:

gzip -h
Apple gzip 321.100.11
usage: gzip [-123456789acdfhklLNnqrtVv] [-S .suffix] [<file> [<file> ...]]
 -1 --fast            fastest (worst) compression
 -2 .. -8             set compression level
 -9 --best            best (slowest) compression

In this case you can see they have different short/long flag names for 1 and 9 so those are already singled out.

For me, the problem would just be that it sounds like you already have a work around and the problem isn't blocking anyone. You can also just define it as an integer flag with validation and better error messages. It just seems this would get messy when you try and think about a few different issues:

  • it might be intuitive (wrong, but intuitive) for someone to try -10 but then this would be parsed as the flag -1 and an extra argument so the help message would also potentially be confusing
  • If someone wants to allow double/triple digits it wont be short flags anymore but long flags. We'd have to figure out how to expose those options in a consistent and intuitive way
  • If you allow numbers (even double/triple digits now) can you have two different sets of these defined on the same commands? E.g. -1 ..9 for compression and --108 for some custom property? What if they provide something like --55 will we error out and say the flag isn't found or will we have to try and find the "closest" integer flags and say that value is out of their bounds?

It just seems to me that each one of these is a special, one-off use case and not something general that really needs to be built in as a default option.

@johnSchnake johnSchnake added area/flags-args Changes to functionality around command line flags and args kind/feature A feature request for cobra; new or enhanced behavior and removed kind/stale labels Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/flags-args Changes to functionality around command line flags and args kind/feature A feature request for cobra; new or enhanced behavior
Projects
None yet
Development

No branches or pull requests

2 participants