From c16222907ea84936f43f5fdd8500a6a19be993ee Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 11 Oct 2009 14:18:55 +0200 Subject: Add another example, README files, and VC++ projects/solutions --- examples/features/README | 21 +++ examples/features/driver.cxx | 62 +++++++++ examples/features/features-8.0.vcproj | 237 ++++++++++++++++++++++++++++++++++ examples/features/features-9.0.vcproj | 233 +++++++++++++++++++++++++++++++++ examples/features/makefile | 63 +++++++++ examples/features/options.cli | 39 ++++++ 6 files changed, 655 insertions(+) create mode 100644 examples/features/README create mode 100644 examples/features/driver.cxx create mode 100644 examples/features/features-8.0.vcproj create mode 100644 examples/features/features-9.0.vcproj create mode 100644 examples/features/makefile create mode 100644 examples/features/options.cli (limited to 'examples/features') diff --git a/examples/features/README b/examples/features/README new file mode 100644 index 0000000..fc30a53 --- /dev/null +++ b/examples/features/README @@ -0,0 +1,21 @@ +This example shows how to use various features of the CLI language. + +The example consists of the following files: + +options.cli + Options class definition in the CLI language. + +options.hxx +options.ixx +options.cxx + Options class 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 options values. + +To run the example you can try various command lines suggested in the +options.cli file. diff --git a/examples/features/driver.cxx b/examples/features/driver.cxx new file mode 100644 index 0000000..339f4c1 --- /dev/null +++ b/examples/features/driver.cxx @@ -0,0 +1,62 @@ +// file : examples/features/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include +#include +#include + +#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 (cerr, " ")); + cerr << endl; + } + + if (!o.set ().empty ()) + { + copy (o.set ().begin (), o.set ().end (), + ostream_iterator (cerr, " ")); + cerr << endl; + } + + // --map | -m + // + typedef map 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/examples/features/features-8.0.vcproj b/examples/features/features-8.0.vcproj new file mode 100644 index 0000000..e7d27b8 --- /dev/null +++ b/examples/features/features-8.0.vcproj @@ -0,0 +1,237 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/features/features-9.0.vcproj b/examples/features/features-9.0.vcproj new file mode 100644 index 0000000..bf18ffc --- /dev/null +++ b/examples/features/features-9.0.vcproj @@ -0,0 +1,233 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/features/makefile b/examples/features/makefile new file mode 100644 index 0000000..c599afe --- /dev/null +++ b/examples/features/makefile @@ -0,0 +1,63 @@ +# file : examples/features/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2009 Code Synthesis Tools CC +# license : MIT; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cli := options.cli +cxx := driver.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o) $(cli:.cli=.o)) +dep := $(obj:.o=.o.d) + +driver := $(out_base)/driver +clean := $(out_base)/.clean + +# Build. +# +$(driver): $(obj) + +$(obj) $(dep): cpp_options := -I$(out_base) + +genf := $(cli:.cli=.hxx) $(cli:.cli=.ixx) $(cli:.cli=.cxx) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): cli := $(out_root)/cli/cli +$(gen): $(out_root)/cli/cli + +$(call include-dep,$(dep),$(obj),$(gen)) + +# Convenience alias for default target. +# +$(out_base)/: $(driver) + +# Clean. +# +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) \ + $(addprefix $(out_base)/,$(cli:.cli=.cxx.cli.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(gen): | $(out_base)/.gitignore +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/cxx/o-e.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(scf_root)/cli/cli-cxx.make) + +# Dependencies. +# +$(call import,$(src_root)/cli/makefile) diff --git a/examples/features/options.cli b/examples/features/options.cli new file mode 100644 index 0000000..e6a8738 --- /dev/null +++ b/examples/features/options.cli @@ -0,0 +1,39 @@ +include ; +include ; +include ; +include ; + +// 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 --vector | -v; + std::set --set | -s; + + // We can also use maps. In this case the option value is expected to have + // two part, the key and the value, separated by '='. For example: -m a=A + // -m =B -m c= -m d (same as -m d=). + // + std::map --map | -m; + }; +} -- cgit v1.1