cobra
cobra copied to clipboard
Add a `no-` version of a boolean
Say I have
serviceLogsCmd.Flags().BoolVarP(&serviceLogsFollow, "follow", "f", true, "Follow log output")
It would be nice to have a --no-follow
automatically generated.
Although this sounds like a good idea, I wonder if it’s needed in practice.
In your above example, the —follow
flag has a default value of true. This implies that the user does not need to specify it in the command line for it to be “turned on”. In such a case maybe it’s simply better to have the flag be —no-follow
with a default value of false.
Actually I want follow
to be the default because I generally follow logs in docker.
So --no-follow
would override it. If I created a no-follow
flag I'd have to think of the situation of something crazy like this myself.
docker service logs --follow --no-follow --follow --follow=false --follow --no-follow
Or at the very least assume that this is not "normal" behaviour (note with ls
they allow switching things on and off on the same command and the last value before --
wins. That last statement I think could be another Issue that should be raised.
I think your scenarios different than what I’m used to. So I probably don’t understand it well. Here’s how I imagine things.
# implies follow is enabled
# this is actually what you have now
myapp logs
# if you remove your follow flag,
# and instead have a no-follow flag
# this will disable follow
myapp logs --no-follow
@marckhouzam you have a correct example. The main purpose I see is the use of shell aliases.
The concept is to allow someone to say the following (which I sort of do as well) Say
alias ls=ls -FCs
Then if I do
ls -1
it translates to
ls -FCs -1
So the 1
overrides the C
parameter.
But with the no-prefix
we can have the concept of follow
being false by default
for example
docker service logs
will not do follow, but if I do an alias to make it follow
alias dsl="docker service logs --follow"
If I want to remove the follow for a command I would have no capability without retyping the whole command. If I had that --no-follow
then I can simply
dsl --no-follow
which translates to
docker service logs --follow --no-follow
And the command line processor will take the "last" version of the flag so make it equivalent to
docker service logs --no-follow
This is the equivalent in python argparse
I see your point. Thanks for the explanation. To do this automatically would require a flag rapper API because the flags are handled by the spf13/pflag project directly.
Can you move this ticket there?
Sorry but I’m not a maintainer for spf13/pflag