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

A way to require "optional" arguments to present #27

Closed
zhihaoy opened this issue Jul 29, 2019 · 3 comments
Closed

A way to require "optional" arguments to present #27

zhihaoy opened this issue Jul 29, 2019 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@zhihaoy
Copy link
Contributor

zhihaoy commented Jul 29, 2019

Currently it's very inconvenient to make an option in the form of -o output required. It is not checked if it does not present, if you get("-o") you get a logic error, which requires lots of post processing. There should be a way to declare that this argument must present. If not presents, exception; if only -o presents, status quo (a different exception).

Also, the current situation makes -o equivalent to not presenting if there is a default_value. I think it's not a very good interface. I tried to simulate a "all or nothing" default value with .implicit_value(x).nargs(1), but no luck.

@wtdcode
Copy link
Contributor

wtdcode commented Aug 3, 2019

Same here.

Take gcc as an example.

/tmp $ echo "" > 1.c
/tmp $ gcc 1.c -o
gcc.exe: error: missing filename after '-o'

I will try to draft a PR if I am free.

@wtdcode
Copy link
Contributor

wtdcode commented Aug 4, 2019

See #30.

So you can write like

	program.add_argument("-o", "--output")
		.required()
		.help("specify the output file.");

or with a default value

	program.add_argument("-o", "--output")
		.default_value(std::string("-"))
                .required()
		.help("specify the output file.");

the program will throw an exception if no value is provided.

$ ./test.exe -o
error: -o: no value provided.

Give it a try. :)

@p-ranav p-ranav self-assigned this Aug 6, 2019
@p-ranav p-ranav added the enhancement New feature or request label Aug 6, 2019
@wtdcode
Copy link
Contributor

wtdcode commented Aug 6, 2019

To avoid break changes, I rewrite my PR.

See #33 for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants