diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2020-04-08 14:51:57 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2020-04-27 11:38:53 +0300 |
commit | 720c5a33b6a49cf328fdd7611f49153cf8f60247 (patch) | |
tree | 9725f3d1f42ec90fde84520f49647edea013ce5e /cli-examples/features | |
parent | 3183f3bb927a90783ae0aeaf190a0919377aabe4 (diff) |
Separate tests and examples into individual packages
Also make cli module to be explicitly enabled via the config.cli configuration
variable.
Diffstat (limited to 'cli-examples/features')
-rw-r--r-- | cli-examples/features/.gitignore | 1 | ||||
-rw-r--r-- | cli-examples/features/README | 20 | ||||
-rw-r--r-- | cli-examples/features/buildfile | 8 | ||||
-rw-r--r-- | cli-examples/features/driver.cxx | 61 | ||||
-rw-r--r-- | cli-examples/features/options.cli | 39 |
5 files changed, 129 insertions, 0 deletions
diff --git a/cli-examples/features/.gitignore b/cli-examples/features/.gitignore new file mode 100644 index 0000000..c6e608b --- /dev/null +++ b/cli-examples/features/.gitignore @@ -0,0 +1 @@ +options.?xx diff --git a/cli-examples/features/README b/cli-examples/features/README new file mode 100644 index 0000000..9416320 --- /dev/null +++ b/cli-examples/features/README @@ -0,0 +1,20 @@ +This example shows how to use various features of the CLI language. + +The example consists of the following files: + +options.cli + Command line interface description in the CLI language. + +options.hxx +options.ixx +options.cxx + Command line interface implementation in C++. These files are generated + by the CLI compiler from hello.cli using the following command line: + + cli options.cli + +driver.cxx + Driver for the example. It first instantiates the option class which parses + the command line. The driver then examines and prints the option values. + +To run the example you can try various command lines suggested in options.cli. diff --git a/cli-examples/features/buildfile b/cli-examples/features/buildfile new file mode 100644 index 0000000..5051eae --- /dev/null +++ b/cli-examples/features/buildfile @@ -0,0 +1,8 @@ +# file : features/buildfile +# license : MIT; see accompanying LICENSE file + +exe{driver}: {hxx cxx}{* -options} cli.cxx{options} doc{README} + +cxx.poptions =+ "-I$out_base" + +cli.cxx{options}: cli{options} diff --git a/cli-examples/features/driver.cxx b/cli-examples/features/driver.cxx new file mode 100644 index 0000000..c14b5c7 --- /dev/null +++ b/cli-examples/features/driver.cxx @@ -0,0 +1,61 @@ +// file : features/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// license : MIT; see accompanying LICENSE file + +#include <iostream> +#include <iterator> +#include <algorithm> + +#include "options.hxx" + +using namespace std; + +int +main (int argc, char* argv[]) +{ + try + { + features::options o (argc, argv); + + // --out-dir | -o + // + if (!o.out_dir ().empty ()) + cerr << "output dir: " << o.out_dir () << endl; + + // --first-name & --last-name + // + cerr << "first name: " << o.first_name () << endl + << "last name : " << o.last_name () << endl; + + // --vector | -v & --set | -s + // + if (!o.vector ().empty ()) + { + copy (o.vector ().begin (), o.vector ().end (), + ostream_iterator<int> (cerr, " ")); + cerr << endl; + } + + if (!o.set ().empty ()) + { + copy (o.set ().begin (), o.set ().end (), + ostream_iterator<int> (cerr, " ")); + cerr << endl; + } + + // --map | -m + // + typedef map<std::string, std::string> str_map; + const str_map& m = o.map (); + str_map::const_iterator i (m.find ("a")); + + if (i != m.end ()) + cerr << "value for the 'a' key: " << i->second << endl; + + } + catch (const cli::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/cli-examples/features/options.cli b/cli-examples/features/options.cli new file mode 100644 index 0000000..ea055b3 --- /dev/null +++ b/cli-examples/features/options.cli @@ -0,0 +1,39 @@ +include <set>; +include <map>; +include <vector>; +include <string>; + +// We can place the options classes into namespaces which mapped to C++ +// namespaces. +// +namespace features +{ + class options + { + // We can have several aliases for the same option. The first one is used + // to derive the accessor name. + // + std::string --out-dir | -o; + + // We can use both assignment and constructor notations to provide the + // default option value. + // + std::string --first-name = "John"; + std::string --last-name ("Mr John Doe", 8, 3); + + // We can use containers to to collect option value. If we have a command + // line like this: -v 1 -v 2 -v 1 -s 1 -s 2 -s 1 then the vector returned + // by the vector() accessor will contain three elements: 1, 2, and 1 while + // the set returned by the set() accessor will contain two elements: 1 and + // 2. + // + std::vector<int> --vector | -v; + std::set<int> --set | -s; + + // We can also use maps. In this case the option value is expected to have + // two parts: the key and the value, separated by '='. For example: -m a=A + // -m =B -m c= -m d (same as -m d=). + // + std::map<std::string, std::string> --map | -m; + }; +} |