-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
How do I define Usage for BoolWithInverseFlag? #1883
Comments
@Skeeve yes that is the current behavior, you are using the BoolWithInverseFlag correctly. I think what you want is the output to have "--no-summary" on a different line correct ? You might be able to modify the help template to get what you want. Let me take a look and see what can be done. |
Yes. On a different line and with a different text. |
I am changing labels on this issue to target v3, because that's what the code in the issue uses. I think this issue was classified as v2 by mistake. I also reworded the issue a bit. |
@Skeeve, would this be OK for you? This is how Dart's
The problem with your example in the issue is: where to define the second
but currently But your use case seems that semantically, you have 3 actions for "summary": "suppress the default summary", something in the middle, and "only send the summary". If this is the case then I'm afraid |
You are right with your assumption.
So I think I have to stick with defining two flags. But to answer your question: I think, that option would be better than what cli has now. |
Would you be up to submit a Pull Request with a fix? |
I didn't change anything in cli, so no. I cannot submit a pull request. It's just that I define two normal bool flags in my program. |
Yeah yeah – I meant "are you willing to modify urfave/cli code that fixes how help is displayed?". |
Sorry, I don't get it. I'm not a native speaker, so I think I need more explanation what you want me to do. |
No problem :) That's okay. Would you like to make an open-source contribution? More specifically:
This is what I mean. I hope it's easier to understand now. Ah, and I don't want you to do anything. I'm only asking. |
Okay. Understood. Will think about it. I will try to find some time on Sunday to look into that. Will be a good training in understanding GO for me. |
@bartekpacia I thought a bit about it and in fact, I wouldn't need to define two variables as the BoolWithInversFlag already has three states: https://play.golang.com/p/MetA6KyQmM_f
which is exactly what I want. Remains the issue: how to define different texts. But I think this is just a case of proper wording. Additionally the If that's okay with you, I'll try my luck ;) at coding it. |
That's an interesting find. If it solves your problem, that's great. But I'm not sure if it's "the way" to solve this problem – I think it's no longer "declarative" then. Maybe a new Examplepackage main
import (
"context"
"fmt"
cli "github.com/urfave/cli/v3"
)
func main() {
cmd := &cli.Command{
Flags: []cli.Flag{
&cli.BoolWithInverseFlag{
BoolFlag: &cli.BoolFlag{
Name: "env",
},
},
},
Action: func(_ context.Context, cmd *cli.Command) error {
var value *bool
value = cmd.BoolWithInverse("env")
if value != nil {
if value {
fmt.Println("env is set to true")
} else {
fmt.Println("env is set to false")
}
} else {
fmt.Println("env is not set")
}
return nil
},
}
_ = cmd.Run(context.Background(), []string{"prog"})
_ = cmd.Run(context.Background(), []string{"prog", "--no-env"})
_ = cmd.Run(context.Background(), []string{"prog", "--env"})
fmt.Println("flags:", len(flagWithInverse.Flags()))
}
Sure! This is an open-source project, the more people submit fixes (even small ones), the better for everyone. |
Why? What bothers you? That the default should only be shown if a default is set? To me this seems quite logical. Or is there anything else which doesn't feel right for you? |
The current BoolFlag already has all the information I need.
My understanding of the BoolWithInverseFlag is, that it only provides a third option:
If the bool is a counter, the value will be a positive or negative number depending on the amount of times it was given. Maybe even 0 if positive and negative were given the same number of times. I had a quick look at BoolWithInverseFlag but couldn't understand the implementation fully. To me it seems as if it simply combines two BoolFlags which is, I think, overvomplicated. I think a simple Bool would do. The only difference is, that the cli parser needs to know that the negative switch needs to be taken into account. |
P.S. Can you please educate me on how to run tests? Maybe the question sounds dumb, but I'm still new to go. |
@Skeeve, thank you very much for looking into the code and sharing your thoughts. Indeed I think something may be wrong – maybe we don't need
Sure! To run all tests in all packages in the directory, do:
But in case of
I hope it helps :) See also this StackOverflow answer. If you have any more questions just ask! |
I do think so, provided it's okay if every BoolFlag has a negative counterpart. On the other hand: Maybe the config options If If We need to also allow the simultaneous use of |
Yes, I agree with almost everything that you wrote above. I'm curious what other maintainers think. One thing I don't agree with is this:
At first glance, it seems illogical to me. |
Didn‘t my argument make any sense? About having an environment variable or a default config file defining (for example) |
In general, arguments/flags passed as arguments on the command-line are given higher priority than the ones in the config file. So in your example, I'd expect Frankly, I've never used |
Me neither. But if what you say is true, then I think my arguments don’t count. |
So how do we find out? |
@bartekpacia is this actively being worked on? i would be interested in taking this on |
@mahadzaryab1 Go ahead and work on it. Its a good first issue. |
I defined a "summary" boolean flag as a
BoolWithInverseFlag
.But the help/usage text doesn't look very helpful to me:
See on Go Playground
Actual output
Expected output
This is the output I expected:
So far, with my current knowledge, I can only achieve it by defining two separate
BoolFlag
s, which is an ugly workaround.Am I misundertstanding the BoolWithInverseFlag? Am I using it wrong?
The text was updated successfully, but these errors were encountered: