From 1bd5d39a995fe0fd6c39834d07acf00185a8754c Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Mon, 1 Apr 2019 19:45:09 -0400 Subject: [PATCH] Update README.md --- README.md | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 982d97b..a140c4c 100644 --- a/README.md +++ b/README.md @@ -63,13 +63,18 @@ program.add_argument("--verbose") .default_value(false) .implicit_value(true); -program.parse_args({ "./main", "--verbose" }); +program.parse_args(argc, argv); if (program["--verbose"] == true) { std::cout << "Verbosity enabled" << std::endl; } ``` +```bash +$ ./main --verbose +Verbosity enabled +``` + Here's what's happening: * The program is written so as to display something when --verbose is specified and display nothing when not. * To show that the option is actually optional, there is no error when running the program without it. Note that by using ```.default_value(false)```, if the optional argument isn’t used, it's value is automatically set to false. @@ -144,6 +149,7 @@ program.add_argument("-b") program.add_argument("-c") .nargs(2) + .default_value(std::vector{0.0f, 0.0f}) .action([](const std::string& value) { return std::stof(value); }); program.parse_args({ "./main", "-abc", "3.14", "2.718" }); @@ -151,6 +157,20 @@ program.parse_args({ "./main", "-abc", "3.14", "2.718" }); auto a = program.get("-a"); // true auto b = program.get("-b"); // true auto c = program.get>("-c"); // {3.14f, 2.718f} + +/// Some code that prints parsed arguments +``` + +```bash +$ ./main -ac 3.14 2.718 +a = true +b = false +c = {3.14, 2.718} + +$ ./main -cb +a = false +b = true +c = {0.0, 0.0} ``` Here's what's happening: @@ -206,14 +226,24 @@ program.add_argument("-c") program.add_argument("--files") .nargs(3); -program.parse_args({ "./test.exe", "1", "-abc", "3.14", "2.718", "2", "--files", - "a.txt", "b.txt", "c.txt", "3" }); +program.parse_args(argc, argv); auto numbers = program.get>("numbers"); // {1, 2, 3} auto a = program.get("-a"); // true auto b = program.get("-b"); // true auto c = program.get>("-c"); // {3.14f, 2.718f} auto files = program.get>("--files"); // {"a.txt", "b.txt", "c.txt"} + +/// Some code that prints parsed arguments +``` + +```bash +./main 1 -abc 3.14 2.718 2 --files a.txt b.txt c.txt 3 +numbers = {1, 2, 3} +a = true +b = true +c = {3.14, 2.718} +d = {"a.txt", "b.txt", "c.txt"} ``` ### Restricting the set of values for an argument @@ -234,6 +264,12 @@ program.add_argument("input") program.parse_args({ "./test", "fez" }); auto input = program.get("input"); // baz +std::cout << input << std::endl; +``` + +```bash +$ ./main fex +baz ``` ## Contributing