Comments (10)
This should normally work, but not for short flags with an optional argument. This is similar in behaviour to gnu getopt. Can you show a short example code for which this fails?
from go-flags.
This should normally work, but not for short flags with an optional argument. This is similar in behaviour to gnu getopt. Can you show a short example code for which this fails?
Right, in this case it's an optional argument. I had never noticed that this doesn't work with GNU getopt. So I assume that this is to be expected and there is no workaround? Why would it be so for optional arguments, does it make parsing difficult/impossible?
from go-flags.
It makes parsing ambiguous. If an argument is optional, you can no longer tell if with "-f config.yml" the config.yml should be the optional argument or a positional argument. Not only is it ambiguous, it also becomes hard to use -f without argument (since it would either always have to appear last, or before another option). Allowing only -f=config.yml removes these issues.
There is no workaround, and I don't think there should be. Maybe in your case the argument should simply not be optional?
from go-flags.
It makes parsing ambiguous. If an argument is optional, you can no longer tell if with "-f config.yml" the config.yml should be the optional argument or a positional argument. Not only is it ambiguous, it also becomes hard to use -f without argument (since it would either always have to appear last, or before another option). Allowing only -f=config.yml removes these issues.
I don't entirely understand. If -f
is specified, it means that something is going to follow. i.e.: what follows can't be a positional argument. The way I understand it, the flag itself is optional, you can just leave it out. If, however, you DO specify -f
, then you also need to supply an argument (it's not a boolean, it's a string).
The only thing left is if the users specifies something after -f
that's supposed to be a positional argument. This is a user error, and in this case (as I believe in most cases), the program will just inform the user that that argument doesn't exactly validate.
What do you say?
EDIT: alternatively, there could be a provision for specifying that an argument is itself optional, and not that its value is optional.
from go-flags.
That's not how optional arguments work. The term optional can be a bit misleading I guess, but the documentation describes it quite clearly. The flag itself is not marked optional, rather the argument to the flag is optional. This means that you can specify only the flag, which will then assume an the value specified in the optional-value tag.
Consider for example this:
Option string short:"o" optional:"yes" optional-value:"something" default:"another thing"
When I would specify only -o
, the Option
field will be set to "something"
. However, when I specify -o=value
the field will be set to "value"
. When -o
is not specified at all, it will take on the value specified by the default
tag, in this case "another thing"
.
from go-flags.
Is suppose such a thing could be useful (different values for absence of flag vis-a-vis flag specified but empty). But in this case I would like a flag that defaults to "config.yml" when it is not present, but when it is present, the user must input a value. i.e.: there would be no "optional-value". Is that possible then?
EDIT: From the examples in README.md:
Supports -I/usr/include -I=/usr/include -I /usr/include option argument specification
Well, the last one is what I need! (how would I go about obtaining such a thing?)
from go-flags.
Yes, just don't mark it optional, and give it a default value.
from go-flags.
Yes, just don't mark it optional, and give it a default value.
Alright, perfect! It somehow doesn't seem to work entirely right when it's used in a command (with AddCommand
), but I can easily get around that with this:
if x.Option == "" {
x.Option = DEFAULT_OPTION
}
So now everything is great, thanks.
from go-flags.
I see, now that is a bug! I've fixed the defaults for options in commands, so no workaround should be needed anymore.
from go-flags.
And that's 3 lines less code, thanks!
from go-flags.
Related Issues (20)
- Documentation: Add more Examples HOT 1
- Help Displayed for func(bool) should be as for bool
- 1.5.0 tag, but no release HOT 1
- Allow to specify default value for a positional argument
- Add a working completion example for 'zsh' shell
- Add a man generator for markup (ronn-style)
- Need proper documentation on how to correctly handle errors HOT 13
- Can flags be used to parse command line arguments from 'go test' ? HOT 2
- Is this an abandoned project?
- Is this an abandoned project? HOT 21
- Program description
- A little leverage
- help format default posix in windows, I don't want to use -tags=forceposix, let them use -tags=forcewindows HOT 2
- Fix CVE-2022-29526 vulnerability caused by golang.org/x/sys? HOT 2
- How to group subcommands HOT 3
- Is it possible to have dynamically generated help descriptions?
- Passing options as argument to other options without using positional-args HOT 1
- custom type positional argument ValueValidator implementation is ignored
- How do I add a flag and pass the value there?
- Multiple short options are not detected with `IgnoreUnknown` flag.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-flags.