-
Notifications
You must be signed in to change notification settings - Fork 273
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
Flag with optional value #134
Comments
Only by checking If the value differs from the default. A useful trick is to do something like .Default("\0").Placeholder("PASSWORD") then if the password is still "\0" prompt. |
Thanks Alec, that exactly what I need. |
Just leaving a note here in case anyone stumbles upon this issue looking for the same solution. @alecthomas solution either no longer works or he didn't understand the problem @mremond and I are trying to solve. The MySQL command line client may be called in one of three ways.
As far as I can tell this can't be accomplished with Kingpin. pass := kingpin.Flag("password", "Password.").Default("\000").Short('p').String() That leaves the end user with two possibilities: don't use the
The user should not be prompted for a password when Here's the solution I'm using, which perfectly recreates the MySQL behavior. for i, a := range os.Args {
if a == "-p" || a == "--password" {
os.Args[i] = "--password=\000"
}
}
pass := kingpin.Flag("password", "Password.").Short('p').String()
if *pass == "\000" {
// Prompt for the user's password.
} Note: MySQL doesn't allow a space character after the |
@mremond 's problem wasn't the same as yours, afaik. You are correct, there is no builtin way to do what you're asking for. |
It's a bit of an edge case for sure. One simple fix which may be useful outside of this one situation would be having an error handler. The handler could return Kingpin.OnError(func(flag string, err error) error {
if flag == "--password" {
// prompt for password
return nil
}
// Return the original error, or a new one.
// return errors.New("Are you crazy!")
return err
}) Either way, Kingpin is great and pretty flexible! |
For what its worth, @headzoo described exactly what i'm looking to do as well. Thanks! |
@headzoo thanks, but I still do not understand what the point of Kingpin's |
It sets Flags with optional values, ie. that can either take |
Compatibility with some standard GNU/Linux tools requires this, for example
|
Ah interesting. If there's a default value, there's no ambiguity because Kong could just set it to that value and pass it through transparently. Seems reasonable, PRs welcome. |
When user reads the manual where it mentions default value for a flag, it generally means default when flag is not passed. |
It definitely does set the default value when the value is not passed. |
Sorry, you are right. |
Something like |
Something like that could work, yeah. I think the complication is that eg. in the GNU ls |
It would probably have to be an error to use |
No actually in this case both are needed. |
Ah yes, good point 👍 |
My main concern is the ambiguity it causes when parsing and separating flags and their values from standalone arguments. For example, you can have a file named "always" in current directory, and run |
We can still have both |
Is there already a way to have flag that can be empty, like "-p Password"?
If Password is not provided from the command-line, I would like to prompt user to enter it after launching command. This avoid showing the password in user bash history.
The text was updated successfully, but these errors were encountered: