From bf4902bde443cd5eb98d698630fb9829af3116c8 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Sun, 31 Mar 2019 11:46:21 -0400 Subject: [PATCH] Update README.md --- README.md | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 12f8a91..9d850c8 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,25 @@ std::string input = program.get("input"); // "rocket.msh" std::string output = program.get("output"); // "thrust_profile.csv" ``` +### Construct Objects from Arguments with ```.action``` + +```cpp +argparse::ArgumentParser program("json_test"); + +program.add_argument("config") + .action([](const std::string& value) { + // read a JSON file + std::ifstream stream(value); + nlohmann::json config_json; + stream >> config_json; + return config_json; + }); + +program.parse_args({"./test", "config.json"}); + +nlohmann::json config = program.get("config"); +``` + ### Optional Arguments ```cpp @@ -126,22 +145,3 @@ 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"} ``` - -### Construct class objects from arguments - -```cpp -argparse::ArgumentParser program("json_test"); - -program.add_argument("config") - .action([](const std::string& value) { - // read a JSON file - std::ifstream stream(value); - nlohmann::json config_json; - stream >> config_json; - return config_json; - }); - -program.parse_args({"./test", "config.json"}); - -nlohmann::json config = program.get("config"); -```