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 an example how to use it #23

Closed
certik opened this issue Jun 5, 2019 · 2 comments
Closed

Add an example how to use it #23

certik opened this issue Jun 5, 2019 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@certik
Copy link

certik commented Jun 5, 2019

I created an example by copy & pasting things from the README:

#include "include/argparse.hpp"

int main(int argc, char *argv[]) {
	argparse::ArgumentParser program("test");

	program.add_argument("square")
	  .help("display the square of a given number")
	  .action([](const std::string& value) { return std::stoi(value); });

	program.add_argument("--verbose")
	  .default_value(false)
	  .implicit_value(true);

	program.parse_args(argc, argv);

	int input = program.get<int>("square");

	if (program["--verbose"] == true) {
	  std::cout << "The square of " << input << " is " << (input * input) << std::endl;
	}
	else {
	  std::cout << (input * input) << std::endl;
	}

	return 0;
}

And it seems to fail:

$ ./a.out 4
16
$ ./a.out --help
terminate called after throwing an instance of 'std::runtime_error'
  what():  help called
Aborted (core dumped)
@p-ranav p-ranav self-assigned this Jun 5, 2019
@p-ranav p-ranav added the bug Something isn't working label Jun 5, 2019
p-ranav added a commit that referenced this issue Jun 5, 2019
@p-ranav
Copy link
Owner

p-ranav commented Jun 5, 2019

Updated README for usage. Please test, confirm and close.

@certik
Copy link
Author

certik commented Jun 5, 2019

I think it works now, thanks! I get:

$ ./a.out --help
help called
Usage: test [options] square 

Positional arguments:
square    	display the square of a given number

Optional arguments:
-h --help 	show this help message and exit
--verbose 	

@certik certik closed this as completed Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants