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/README | 11 ++ examples/examples-8.0.sln | 26 ++++ examples/examples-9.0.sln | 26 ++++ 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 ++++++ examples/hello/README | 26 ++++ examples/hello/driver.cxx | 6 +- examples/hello/hello-8.0.vcproj | 237 ++++++++++++++++++++++++++++++++++ examples/hello/hello-9.0.vcproj | 233 +++++++++++++++++++++++++++++++++ examples/makefile | 2 +- 14 files changed, 1219 insertions(+), 3 deletions(-) create mode 100644 examples/README create mode 100644 examples/examples-8.0.sln create mode 100644 examples/examples-9.0.sln 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 create mode 100644 examples/hello/README create mode 100644 examples/hello/hello-8.0.vcproj create mode 100644 examples/hello/hello-9.0.vcproj (limited to 'examples') diff --git a/examples/README b/examples/README new file mode 100644 index 0000000..d333f11 --- /dev/null +++ b/examples/README @@ -0,0 +1,11 @@ +This directory contains a number of examples that show how to use the CLI +language and compiler to implement command line interface parsing in C++. +The following list gives an overview of each example. See the README files +in example directories for more information on each example. + +hello + A simple "Hello, world!" example that shows how to implement a very basic + command line interface using CLI. + +features + Shows how to use various features of the CLI language. diff --git a/examples/examples-8.0.sln b/examples/examples-8.0.sln new file mode 100644 index 0000000..8b7e077 --- /dev/null +++ b/examples/examples-8.0.sln @@ -0,0 +1,26 @@ + +Microsoft Visual Studio Solution File, Format Version 9.00 +# Visual Studio 2005 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hello", "hello\hello-8.0.vcproj", "{4813826B-CC80-4D8F-8E4C-D89DB83B6479}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "features", "features\features-8.0.vcproj", "{FA7FD071-4FF6-4EB3-96E0-95366AB2CA6F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4813826B-CC80-4D8F-8E4C-D89DB83B6479}.Debug|Win32.ActiveCfg = Debug|Win32 + {4813826B-CC80-4D8F-8E4C-D89DB83B6479}.Debug|Win32.Build.0 = Debug|Win32 + {4813826B-CC80-4D8F-8E4C-D89DB83B6479}.Release|Win32.ActiveCfg = Release|Win32 + {4813826B-CC80-4D8F-8E4C-D89DB83B6479}.Release|Win32.Build.0 = Release|Win32 + {FA7FD071-4FF6-4EB3-96E0-95366AB2CA6F}.Debug|Win32.ActiveCfg = Debug|Win32 + {FA7FD071-4FF6-4EB3-96E0-95366AB2CA6F}.Debug|Win32.Build.0 = Debug|Win32 + {FA7FD071-4FF6-4EB3-96E0-95366AB2CA6F}.Release|Win32.ActiveCfg = Release|Win32 + {FA7FD071-4FF6-4EB3-96E0-95366AB2CA6F}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/examples/examples-9.0.sln b/examples/examples-9.0.sln new file mode 100644 index 0000000..8a2d2b5 --- /dev/null +++ b/examples/examples-9.0.sln @@ -0,0 +1,26 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hello", "hello\hello-9.0.vcproj", "{C955CE91-C424-44A8-AAC5-64D50CDADABA}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "features", "features\features-9.0.vcproj", "{A2A8DB0D-FA16-4F85-9775-8ADBDD06AE90}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C955CE91-C424-44A8-AAC5-64D50CDADABA}.Debug|Win32.ActiveCfg = Debug|Win32 + {C955CE91-C424-44A8-AAC5-64D50CDADABA}.Debug|Win32.Build.0 = Debug|Win32 + {C955CE91-C424-44A8-AAC5-64D50CDADABA}.Release|Win32.ActiveCfg = Release|Win32 + {C955CE91-C424-44A8-AAC5-64D50CDADABA}.Release|Win32.Build.0 = Release|Win32 + {A2A8DB0D-FA16-4F85-9775-8ADBDD06AE90}.Debug|Win32.ActiveCfg = Debug|Win32 + {A2A8DB0D-FA16-4F85-9775-8ADBDD06AE90}.Debug|Win32.Build.0 = Debug|Win32 + {A2A8DB0D-FA16-4F85-9775-8ADBDD06AE90}.Release|Win32.ActiveCfg = Release|Win32 + {A2A8DB0D-FA16-4F85-9775-8ADBDD06AE90}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal 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; + }; +} diff --git a/examples/hello/README b/examples/hello/README new file mode 100644 index 0000000..a1fe976 --- /dev/null +++ b/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 + Options class definition in the CLI language. + +hello.hxx +hello.ixx +hello.cxx + Options class 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 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/examples/hello/driver.cxx b/examples/hello/driver.cxx index 6641d4b..b08e6be 100644 --- a/examples/hello/driver.cxx +++ b/examples/hello/driver.cxx @@ -14,8 +14,8 @@ usage () { cerr << "usage: driver " << endl << " [--help]" << endl - << " [--greeting|-g ]" << endl - << " [--exclamations|-e ]" << endl; + << " [--greeting ]" << endl + << " [--exclamations ]" << endl; } int @@ -39,6 +39,8 @@ main (int argc, char* argv[]) return 1; } + // Print the greetings. + // for (int i = end; i < argc; i++) { cout << o.greeting () << ", " << argv[i]; diff --git a/examples/hello/hello-8.0.vcproj b/examples/hello/hello-8.0.vcproj new file mode 100644 index 0000000..6415d8c --- /dev/null +++ b/examples/hello/hello-8.0.vcproj @@ -0,0 +1,237 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/hello/hello-9.0.vcproj b/examples/hello/hello-9.0.vcproj new file mode 100644 index 0000000..3ca9838 --- /dev/null +++ b/examples/hello/hello-9.0.vcproj @@ -0,0 +1,233 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/makefile b/examples/makefile index 31a72b7..0180e9a 100644 --- a/examples/makefile +++ b/examples/makefile @@ -5,7 +5,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make -examples := hello +examples := hello features default := $(out_base)/ clean := $(out_base)/.clean -- cgit v1.1