From f8edfd22cb45b554a573d2722900196758e9e958 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 10 Dec 2009 09:47:29 +0200 Subject: Scanner-based parsing with support for element erasing Also implement argv_file_scanner which provides support for reading command line arguments from the argv array as well as files specified with command line options. New examples: file. New tests: ctor, erase, file. --- examples/file/README | 38 +++++++ examples/file/driver.cxx | 36 +++++++ examples/file/file-8.0.vcproj | 237 ++++++++++++++++++++++++++++++++++++++++++ examples/file/file-9.0.vcproj | 233 +++++++++++++++++++++++++++++++++++++++++ examples/file/makefile | 74 +++++++++++++ examples/file/options.cli | 7 ++ examples/file/test.ops | 7 ++ 7 files changed, 632 insertions(+) create mode 100644 examples/file/README create mode 100644 examples/file/driver.cxx create mode 100644 examples/file/file-8.0.vcproj create mode 100644 examples/file/file-9.0.vcproj create mode 100644 examples/file/makefile create mode 100644 examples/file/options.cli create mode 100644 examples/file/test.ops (limited to 'examples/file') diff --git a/examples/file/README b/examples/file/README new file mode 100644 index 0000000..289fc64 --- /dev/null +++ b/examples/file/README @@ -0,0 +1,38 @@ +This example shows how to allow the users of your application to supply +options in files in addition to the command line. + +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 options.cli using the following command line: + + cli --generate-file-scanner hello.cli + + We use the --generate-file-scanner CLI compiler option to include the + argv_file_scanner scanner implementation which provides support for + reading options from files in addition to the command line. + +driver.cxx + Driver for the example. It first creates the argv_file_scanner object + and indicates that the values for the --options-file option should be + recognized as files containing additional options. It then passes this + scanner object to the option class which parses the command line. The + driver then prints the option values. + +test.ops + Sample options file. + +To run this example you can try the following command line: + +$ ./driver --verbose 2 --val 1 --options-file test.ops --val 4 + +The output will be: + +verbosity: 5 +values: 1 2 3 4 diff --git a/examples/file/driver.cxx b/examples/file/driver.cxx new file mode 100644 index 0000000..df7a67c --- /dev/null +++ b/examples/file/driver.cxx @@ -0,0 +1,36 @@ +// file : examples/file/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 + { + cli::argv_file_scanner s (argc, argv, "--options-file"); + options o (s); + + cout << "verbosity: " << o.verbose () << endl + << "values: "; + + copy (o.val ().begin (), + o.val ().end (), + ostream_iterator (cout, " ")); + + cerr << endl; + } + catch (const cli::exception& e) + { + cerr << e << endl; + return 1; + } +} diff --git a/examples/file/file-8.0.vcproj b/examples/file/file-8.0.vcproj new file mode 100644 index 0000000..0b1000b --- /dev/null +++ b/examples/file/file-8.0.vcproj @@ -0,0 +1,237 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/file/file-9.0.vcproj b/examples/file/file-9.0.vcproj new file mode 100644 index 0000000..c2a78e5 --- /dev/null +++ b/examples/file/file-9.0.vcproj @@ -0,0 +1,233 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/file/makefile b/examples/file/makefile new file mode 100644 index 0000000..04c45a7 --- /dev/null +++ b/examples/file/makefile @@ -0,0 +1,74 @@ +# file : examples/file/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 +install := $(out_base)/.install +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): cli_options := --generate-file-scanner +$(gen): $(out_root)/cli/cli + +$(call include-dep,$(dep),$(obj),$(gen)) + +# Convenience alias for default target. +# +$(out_base)/: $(driver) + +# Install +# +$(install): path := $(subst $(src_root)/,,$(src_base)) +$(install): + $(call install-data,$(src_base)/driver.cxx,$(install_doc_dir)/cli/$(path)/driver.cxx) + $(call install-data,$(src_base)/options.cli,$(install_doc_dir)/cli/$(path)/options.cli) + $(call install-data,$(src_base)/README,$(install_doc_dir)/cli/$(path)/README) + $(call install-data,$(src_base)/test.ops,$(install_doc_dir)/cli/$(path)/test.ops) + +# 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/file/options.cli b/examples/file/options.cli new file mode 100644 index 0000000..3e6db5a --- /dev/null +++ b/examples/file/options.cli @@ -0,0 +1,7 @@ +include ; + +class options +{ + int --verbose; + std::vector --val; +}; diff --git a/examples/file/test.ops b/examples/file/test.ops new file mode 100644 index 0000000..47097fb --- /dev/null +++ b/examples/file/test.ops @@ -0,0 +1,7 @@ +# Sample options file. Empty lines and lines starting with '#' are +# ignored. +# +--verbose 5 + +--val 2 +--val 3 -- cgit v1.1