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

Positional arguments #15

Closed
wavexx opened this issue Sep 9, 2015 · 5 comments
Closed

Positional arguments #15

wavexx opened this issue Sep 9, 2015 · 5 comments

Comments

@wavexx
Copy link

wavexx commented Sep 9, 2015

It would be nice to improve a bit the handling of positional arguments to be
more unix-y. A practical example of what I'd have in mind:

cxxopts::Options options(argv[0], " - example command line options");
options.add_options()
  ("f,flag", "desc")
  ("input", "", cxxopts::value<string>(input), "INPUT")
  ("output", "", cxxopts::value<string>(output), "OUTPUT");
opts.parse(argc, argv, {"input", "output"});

Where:

  • the empty description suppresses the help for that option
  • the third argument to Options::parse is a list of positional arguments.
    Just a vector<string>, which is moved to m_positional (now a vector<string>
    as well).
  • short usage also iterates though m_positional to find argument names for
    consistent short help.
  • parse() finally consumes positional arguments in a simple loop.
    If a vector<> is encountered, it just eats all the remaining arguments.
  • parse_positional() is now just a compatibility stub that adds an element
    to m_positional, although I find it less clear than the above behavior.

This should result in the following help:

Usage:
  ./example [OPTION...] INPUT OUTPUT - example command line options

  -f, --flag desc

With no changes to the public interface (and minimal source changes as well).

@jarro2783
Copy link
Owner

This sounds pretty reasonable. Are you suggesting that you would be able to make the changes?

Just so I understand, you want to link positional arguments to different named arguments? At the moment I think it just puts everything into one positional argument. So in the above example, parsing the first positional argument lets me get it as options.get['input'] and the second as options.get['output'].

I'm not sure what you mean by eating the remaining arguments. Do you mean that if one of the positional arguments takes a vector then it just gets all the rest of the positional arguments?

@wavexx
Copy link
Author

wavexx commented Sep 10, 2015

On 10/09/15 06:38, jarro2783 wrote:

This sounds pretty reasonable. Are you suggesting that you would be able
to make the changes?

I would be, though not now. I was looking at the code too see if this
would be possible, and it looks like.

Just so I understand, you want to link positional arguments to different
named arguments? At the moment I think it just puts everything into one
positional argument. So in the above example, parsing the first
positional argument lets me get it as |options.get['input']| and the
second as |options.get['output']|.

Yes.

I'm not sure what you mean by eating the remaining arguments. Do you
mean that if one of the positional arguments takes a vector then it just
gets all the rest of the positional arguments?

Right. It's pretty common for me to have:

$ cmd [flags] cmd file [file ...]

With the above, if you link "file" to vector, you effectively
get all the final positional arguments.

Granted, you can do the same without, but I think it would be a nice touch.

@jarro2783
Copy link
Owner

I don't think it's hard. I'm not really in a position to do it right now either. We can leave this issue open and someone can do it eventually. Let us know if you feel like doing it before I do.

@jarro2783
Copy link
Owner

I'm working on this now.

@jarro2783
Copy link
Owner

This is done now.

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