Skip to content

Flags mimic bool - #228

Closed
sakateka wants to merge 3 commits into
alecthomas:masterfrom
sakateka:flags_mimic_bool
Closed

sakateka wants to merge 3 commits into
alecthomas:masterfrom
sakateka:flags_mimic_bool

Conversation

@sakateka

@sakateka sakateka commented Feb 6, 2018

Copy link
Copy Markdown
Contributor

Hi!
I have an idea how to add flags similar to Booleans, but taking optional values.
I also figured out how to make isSetByUser posible for users.
I implemented this idea and use it in my application.
But when implementing I did not understand how to set the default value correctly and used a dirty hack :-)
If these flags are interesting, I can try to make the proper setting of default values.

@alecthomas

Copy link
Copy Markdown
Owner

Hello. I guess I'm not sure what you're referring to. Can you show me an example?

@sakateka

sakateka commented Feb 8, 2018

Copy link
Copy Markdown
Contributor Author

A good example is the lsof utility
If you run it with the -i option, it will show all network connections
lsof -i
If you pass an optional filter to -i like this :22 then only ssh connections will be shown
lsof -i :22
If run without the -i option, then all open files will be shown, including network connections
lsof

My example, I want to implement the -t flag, which will add counting timings to a given field in the log.
If the user did not call the -t option, then do not count the timings.
If the user simply passed -t then the default timings are request_time
If the user passed the argument for -t, then count the timings specified by the user
For example, -t ssl will count the timings for the ssl_handshake_time field

@alecthomas

Copy link
Copy Markdown
Owner

The problem with this is how do you differentiate between an optional parameter to a flag, and a positional argument? This works for lsof because it does not support positional arguments.

eg. command -i <param> <arg> vs. command -i <arg> vs. command -i <param>

@sakateka

sakateka commented Feb 8, 2018

Copy link
Copy Markdown
Contributor Author

Yes, it's a problem.
I remove all positional arguments from the command line, then call kingpin.MustParse and therefore my app behaves like losf.

As an option, the user should be aware of the problem and start the app like this command <args> -i
The documentation should clarify this point.
Or user can do so, specify an option with an empty value of -i= and after the space specify arguments, in this case -i should get the default value
command -i= args
All these are workarounds that do not add simplicity :-(
It seems to me that clarification in the documentation is the most correct way.

@alecthomas

Copy link
Copy Markdown
Owner

There's also the issue of ambiguity with combined short flags, eg. -i and -j can be combined as -ij.

I don't think there's any way to support this that's reasonable, sorry :(

@alecthomas alecthomas closed this Feb 8, 2018
@sakateka

sakateka commented Feb 8, 2018 •

Copy link
Copy Markdown
Contributor Author

No problems)
Indeed, this will only work if positional arguments are prohibited.

@sakateka
sakateka deleted the flags_mimic_bool branch February 8, 2018 07:58
@sakateka

sakateka commented Feb 8, 2018

Copy link
Copy Markdown
Contributor Author

I remembered the second part of my plan. )
There is still useful functionality that you can add here, it's IsSetByUsermethod
It will look like this.

app.Flag("timings", "Count timings").Short ('t').
    IsSetByUser(&args.TimingsSetByUser).Default ("up").StringVar(&args.Timings)

@alecthomas

Copy link
Copy Markdown
Owner

I didn't quite understand what problem that solved?

@sakateka

sakateka commented Feb 8, 2018 •

Copy link
Copy Markdown
Contributor Author

If the flag have the default value (or not even), the variable will always be set to the default value or to the value provided by the user.
If there is a default value I want to know whether the user has set it (suddenly it has set a default value) or whether it is set by the parser.
There is no way to understand whether the user has set the value or whether it is the default value set by the parser.
I've seen questions about this functionality, not sure what's in this project))

@matteopic

Copy link
Copy Markdown

I agree with @sakateka.
I have a case with a user that set a flag and my application records that setting.
When user start the application again, I would let him to omit that flag and use previous value as default, but currently I don't know how I can do that.

@sakateka

sakateka commented Apr 1, 2019

Copy link
Copy Markdown
Contributor Author

@sakateka sakateka mentioned this pull request Apr 15, 2019
alecthomas pushed a commit that referenced this pull request Apr 27, 2019
gzdunek pushed a commit to gravitational/kingpin that referenced this pull request May 11, 2023
Details alecthomas#228 (comment)

(cherry picked from commit ef6e7c1)
gzdunek added a commit to gravitational/kingpin that referenced this pull request May 11, 2023
* Add tests and fix edge cases

(cherry picked from commit bd4c463)

* Write example of app and short help flags

(cherry picked from commit ff0d2a1)

* OverrideDefaultFromEnvar is deprecated

(cherry picked from commit de569dc)

* add IsSetByUser

Details alecthomas#228 (comment)

(cherry picked from commit ef6e7c1)

---------

Co-authored-by: Emilien Kenler <emilien@kkvesper.jp>
Co-authored-by: Benjamin Kane <bbkane@users.noreply.github.com>
Co-authored-by: Wataru Ashihara <wataash0607@gmail.com>
Co-authored-by: Sergey Kacheev <skacheev@yandex-team.ru>
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

Successfully merging this pull request may close these issues.

3 participants

⌂ 换网址