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

Add a no- version of a boolean #2075

Open
trajano opened this issue Nov 16, 2023 · 8 comments
Open

Add a no- version of a boolean #2075

trajano opened this issue Nov 16, 2023 · 8 comments

Comments

@trajano
Copy link

trajano commented Nov 16, 2023

Say I have

serviceLogsCmd.Flags().BoolVarP(&serviceLogsFollow, "follow", "f", true, "Follow log output")

It would be nice to have a --no-follow automatically generated.

@marckhouzam
Copy link
Collaborator

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.

@trajano
Copy link
Author

trajano commented Nov 16, 2023

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.

@marckhouzam
Copy link
Collaborator

marckhouzam commented Nov 16, 2023

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

@trajano
Copy link
Author

trajano commented Nov 16, 2023

@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 

@trajano
Copy link
Author

trajano commented Nov 17, 2023

This is the equivalent in python argparse
image

@marckhouzam
Copy link
Collaborator

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.

@trajano
Copy link
Author

trajano commented Nov 17, 2023

Can you move this ticket there?

@marckhouzam
Copy link
Collaborator

Sorry but I’m not a maintainer for spf13/pflag

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

No branches or pull requests

2 participants