diff options
Diffstat (limited to 'cli-examples/hello')
-rw-r--r-- | cli-examples/hello/.gitignore | 1 | ||||
-rw-r--r-- | cli-examples/hello/README | 26 | ||||
-rw-r--r-- | cli-examples/hello/buildfile | 9 | ||||
-rw-r--r-- | cli-examples/hello/driver.cxx | 58 | ||||
-rw-r--r-- | cli-examples/hello/hello.cli | 18 |
5 files changed, 112 insertions, 0 deletions
diff --git a/cli-examples/hello/.gitignore b/cli-examples/hello/.gitignore new file mode 100644 index 0000000..d73130a --- /dev/null +++ b/cli-examples/hello/.gitignore @@ -0,0 +1 @@ +hello.?xx diff --git a/cli-examples/hello/README b/cli-examples/hello/README new file mode 100644 index 0000000..dd14b01 --- /dev/null +++ b/cli-examples/hello/README @@ -0,0 +1,26 @@ +This is a "Hello, world!" example that shows how to implement a very basic +command line interface using CLI. + +The example consists of the following files: + +hello.cli + Command line interface description in the CLI language. + +hello.hxx +hello.ixx +hello.cxx + Command line interface implementation in C++. These files are generated + by the CLI compiler from hello.cli using the following command line: + + cli hello.cli + +driver.cxx + Driver for the example. It first instantiates the option class which parses + the command line. The driver then examines the options and prints the + greeting string for each name passed as an argument. + +To run the example you can try the following command lines: + +$ ./driver --help +$ ./driver John Jane +$ ./driver --greeting Hi --exclamations 3 John Jane diff --git a/cli-examples/hello/buildfile b/cli-examples/hello/buildfile new file mode 100644 index 0000000..8197d9e --- /dev/null +++ b/cli-examples/hello/buildfile @@ -0,0 +1,9 @@ +# file : hello/buildfile +# license : MIT; see accompanying LICENSE file + +exe{driver}: {hxx cxx}{* -hello} cli.cxx{hello} doc{README} +exe{driver}: test.arguments = --greeting Hi John Jane + +cxx.poptions =+ "-I$out_base" + +cli.cxx{hello}: cli{hello} diff --git a/cli-examples/hello/driver.cxx b/cli-examples/hello/driver.cxx new file mode 100644 index 0000000..bc37564 --- /dev/null +++ b/cli-examples/hello/driver.cxx @@ -0,0 +1,58 @@ +// file : hello/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// license : MIT; see accompanying LICENSE file + +#include <iostream> + +#include "hello.hxx" + +using namespace std; + +void +usage (ostream& os) +{ + os << "usage: driver [options] <names>" << endl + << "options:" << endl; + options::print_usage (os); +} + +int +main (int argc, char* argv[]) +{ + try + { + int end; // End of options. + options o (argc, argv, end); + + if (o.help ()) + { + usage (cout); + return 0; + } + + if (end == argc) + { + cerr << "no names provided" << endl; + usage (cerr); + return 1; + } + + // Print the greetings. + // + for (int i = end; i < argc; i++) + { + cout << o.greeting () << ", " << argv[i]; + + for (unsigned int j = 0; j < o.exclamations (); j++) + cout << '!'; + + cout << endl; + } + } + catch (const cli::exception& e) + { + cerr << e << endl; + usage (cerr); + return 1; + } +} diff --git a/cli-examples/hello/hello.cli b/cli-examples/hello/hello.cli new file mode 100644 index 0000000..b75e1b8 --- /dev/null +++ b/cli-examples/hello/hello.cli @@ -0,0 +1,18 @@ +include <string>; + +class options +{ + bool --help {"Print usage information and exit."}; + + std::string --greeting = "Hello" + { + "<text>", + "Use <text> as a greeting phrase instead of the default \"Hello\"." + }; + + unsigned int --exclamations = 1 + { + "<num>", + "Print <num> exclamation marks instead of 1 by default." + }; +}; |