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. --- tests/ctor/driver.cxx | 69 +++++++++++++++++++++++++++++++++++ tests/ctor/makefile | 73 +++++++++++++++++++++++++++++++++++++ tests/ctor/test.cli | 9 +++++ tests/erase/driver.cxx | 35 ++++++++++++++++++ tests/erase/makefile | 73 +++++++++++++++++++++++++++++++++++++ tests/erase/test.cli | 10 ++++++ tests/file/base.ops | 2 ++ tests/file/driver.cxx | 29 +++++++++++++++ tests/file/empty.ops | 3 ++ tests/file/makefile | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/file/test-000.ops | 6 ++++ tests/file/test-000.std | 18 ++++++++++ tests/file/test-001.ops | 3 ++ tests/file/test-001.std | 6 ++++ tests/file/test-002.ops | 7 ++++ tests/file/test-002.std | 17 +++++++++ tests/file/test-003.std | 7 ++++ tests/file/test.cli | 8 +++++ tests/makefile | 2 +- 19 files changed, 472 insertions(+), 1 deletion(-) create mode 100644 tests/ctor/driver.cxx create mode 100644 tests/ctor/makefile create mode 100644 tests/ctor/test.cli create mode 100644 tests/erase/driver.cxx create mode 100644 tests/erase/makefile create mode 100644 tests/erase/test.cli create mode 100644 tests/file/base.ops create mode 100644 tests/file/driver.cxx create mode 100644 tests/file/empty.ops create mode 100644 tests/file/makefile create mode 100644 tests/file/test-000.ops create mode 100644 tests/file/test-000.std create mode 100644 tests/file/test-001.ops create mode 100644 tests/file/test-001.std create mode 100644 tests/file/test-002.ops create mode 100644 tests/file/test-002.std create mode 100644 tests/file/test-003.std create mode 100644 tests/file/test.cli (limited to 'tests') diff --git a/tests/ctor/driver.cxx b/tests/ctor/driver.cxx new file mode 100644 index 0000000..c923ec1 --- /dev/null +++ b/tests/ctor/driver.cxx @@ -0,0 +1,69 @@ +// file : tests/ctor/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include "test.hxx" + +int +main (int argc, char* argv[]) +{ + // Test that we can call all the c-tors unambiguously. + // + { + options o1 (argc, argv); + options o2 (argc, argv, + cli::unknown_mode::fail); + options o3 (argc, argv, + cli::unknown_mode::fail, + cli::unknown_mode::stop); + options o4 (argc, argv, true, + cli::unknown_mode::fail, + cli::unknown_mode::stop); + } + + { + options o1 (1, argc, argv); + options o2 (1, argc, argv, + cli::unknown_mode::fail); + options o3 (1, argc, argv, + cli::unknown_mode::fail, + cli::unknown_mode::stop); + options o4 (1, argc, argv, true, + cli::unknown_mode::fail, + cli::unknown_mode::stop); + } + + { + int end; + options o1 (argc, argv, end); + options o2 (argc, argv, end, + cli::unknown_mode::fail); + options o3 (argc, argv, end, + cli::unknown_mode::fail, + cli::unknown_mode::stop); + options o4 (argc, argv, end, true, + cli::unknown_mode::fail, + cli::unknown_mode::stop); + } + + { + int end; + options o1 (1, argc, argv, end); + options o2 (1, argc, argv, end, + cli::unknown_mode::fail); + options o3 (1, argc, argv, end, + cli::unknown_mode::fail, + cli::unknown_mode::stop); + options o4 (1, argc, argv, end, true, + cli::unknown_mode::fail, + cli::unknown_mode::stop); + } + + { + cli::argv_scanner s (argc, argv); + options o1 (s); + options o2 (s, cli::unknown_mode::fail); + options o3 (s, cli::unknown_mode::fail, cli::unknown_mode::stop); + } +} diff --git a/tests/ctor/makefile b/tests/ctor/makefile new file mode 100644 index 0000000..93d832a --- /dev/null +++ b/tests/ctor/makefile @@ -0,0 +1,73 @@ +# file : tests/ctor/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 + +cxx_tun := driver.cxx +cli_tun := test.cli + +# +# +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(cli_tun:.cli=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +driver := $(out_base)/driver +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Build. +# +$(driver): $(cxx_obj) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) + +genf := $(cli_tun:.cli=.hxx) $(cli_tun:.cli=.ixx) $(cli_tun:.cli=.cxx) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(out_root)/cli/cli +$(gen): cli := $(out_root)/cli/cli +$(gen): cli_options := + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Test. +# +$(test): driver := $(driver) +$(test): $(driver) + $(call message,test $$1,$$1,$(driver)) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(cli_tun:.cli=.cxx.cli.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(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/tests/ctor/test.cli b/tests/ctor/test.cli new file mode 100644 index 0000000..f85ad98 --- /dev/null +++ b/tests/ctor/test.cli @@ -0,0 +1,9 @@ +// file : tests/ctor/test.cli +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +class options +{ + bool --help; +}; diff --git a/tests/erase/driver.cxx b/tests/erase/driver.cxx new file mode 100644 index 0000000..ddb6b8d --- /dev/null +++ b/tests/erase/driver.cxx @@ -0,0 +1,35 @@ +// file : tests/erase/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +// Test argument erasing. +// + +#include +#include + +#include "test.hxx" + +using namespace std; + +int +main (int argc, char* argv[]) +{ + options o (argc, argv, true, + cli::unknown_mode::skip, + cli::unknown_mode::skip); + + assert (o.a ()); + assert (o.b () == 123); + + // We should have 'foo bar --arg -- -b 234'. + // + assert (argc == 7); + assert (argv[1] == string ("foo")); + assert (argv[2] == string ("bar")); + assert (argv[3] == string ("--arg")); + assert (argv[4] == string ("--")); + assert (argv[5] == string ("-b")); + assert (argv[6] == string ("234")); +} diff --git a/tests/erase/makefile b/tests/erase/makefile new file mode 100644 index 0000000..5a8f49a --- /dev/null +++ b/tests/erase/makefile @@ -0,0 +1,73 @@ +# file : tests/erase/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 + +cxx_tun := driver.cxx +cli_tun := test.cli + +# +# +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(cli_tun:.cli=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +driver := $(out_base)/driver +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Build. +# +$(driver): $(cxx_obj) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) + +genf := $(cli_tun:.cli=.hxx) $(cli_tun:.cli=.ixx) $(cli_tun:.cli=.cxx) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(out_root)/cli/cli +$(gen): cli := $(out_root)/cli/cli +$(gen): cli_options := + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Test. +# +$(test): driver := $(driver) +$(test): $(driver) + $(call message,test $$1,$$1 foo -a bar -b 123 --arg -- -b 234,$(driver)) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(cli_tun:.cli=.cxx.cli.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(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/tests/erase/test.cli b/tests/erase/test.cli new file mode 100644 index 0000000..57820c0 --- /dev/null +++ b/tests/erase/test.cli @@ -0,0 +1,10 @@ +// file : tests/erase/test.cli +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +class options +{ + bool -a; + int -b; +}; diff --git a/tests/file/base.ops b/tests/file/base.ops new file mode 100644 index 0000000..45d7696 --- /dev/null +++ b/tests/file/base.ops @@ -0,0 +1,2 @@ +-a 21 +-b 21 diff --git a/tests/file/driver.cxx b/tests/file/driver.cxx new file mode 100644 index 0000000..e0aec6e --- /dev/null +++ b/tests/file/driver.cxx @@ -0,0 +1,29 @@ +// file : tests/file/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +// Test argv_file_scanner. +// + +#include + +#include "test.hxx" + +using namespace std; + +int +main (int argc, char* argv[]) +{ + try + { + cli::argv_file_scanner s (argc, argv, "--file"); + + while (s.more ()) + cout << s.next () << endl; + } + catch (const cli::exception& e) + { + cout << e << endl; + } +} diff --git a/tests/file/empty.ops b/tests/file/empty.ops new file mode 100644 index 0000000..ed080fb --- /dev/null +++ b/tests/file/empty.ops @@ -0,0 +1,3 @@ +# Empty options file. +# + diff --git a/tests/file/makefile b/tests/file/makefile new file mode 100644 index 0000000..56e95c6 --- /dev/null +++ b/tests/file/makefile @@ -0,0 +1,96 @@ +# file : tests/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 + +cxx_tun := driver.cxx +cli_tun := test.cli + +tests := 000 001 002 003 + +# +# +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(cli_tun:.cli=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +driver := $(out_base)/driver +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Build. +# +$(driver): $(cxx_obj) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) + +genf := $(cli_tun:.cli=.hxx) $(cli_tun:.cli=.ixx) $(cli_tun:.cli=.cxx) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(out_root)/cli/cli +$(gen): cli := $(out_root)/cli/cli +$(gen): cli_options := --generate-file-scanner + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Test. +# +test_targets := $(addprefix $(out_base)/.test-,$(tests)) + +$(test): $(test_targets) +$(test_targets): driver := $(driver) + +.PHONY: $(out_base)/.test-% + +$(out_base)/.test-000: $(driver) $(src_base)/test-000.ops + $(call message,test $(out_base)/000,$(driver) -a 1 \ +--file $(src_base)/empty.ops -b 1 --file $(src_base)/base.ops \ +--file $(src_base)/test-000.ops b | diff -u $(src_base)/test-000.std -) + +$(out_base)/.test-001: $(driver) $(src_base)/test-001.ops + $(call message,test $(out_base)/001,$(driver) -a 1 -- --file \ +$(src_base)/test-001.ops b | diff -u $(src_base)/test-001.std -) + +$(out_base)/.test-002: $(driver) $(src_base)/test-002.ops + $(call message,test $(out_base)/002,$(driver) -a 1 --file \ +$(src_base)/test-002.ops --file $(src_base)/empty.ops b | \ +diff -u $(src_base)/test-002.std -) + +$(out_base)/.test-003: $(driver) + $(call message,test $(out_base)/003,$(driver) -a 1 --file \ +$(src_base)/base.ops --file test-003.ops b | diff -u $(src_base)/test-003.std -) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(cli_tun:.cli=.cxx.cli.clean)) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(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/tests/file/test-000.ops b/tests/file/test-000.ops new file mode 100644 index 0000000..c0462e2 --- /dev/null +++ b/tests/file/test-000.ops @@ -0,0 +1,6 @@ +-a 11 +-b 11 + -a 12 + + -b 12 +a diff --git a/tests/file/test-000.std b/tests/file/test-000.std new file mode 100644 index 0000000..392b83d --- /dev/null +++ b/tests/file/test-000.std @@ -0,0 +1,18 @@ +-a +1 +-b +1 +-a +21 +-b +21 +-a +11 +-b +11 +-a +12 +-b +12 +a +b diff --git a/tests/file/test-001.ops b/tests/file/test-001.ops new file mode 100644 index 0000000..ed080fb --- /dev/null +++ b/tests/file/test-001.ops @@ -0,0 +1,3 @@ +# Empty options file. +# + diff --git a/tests/file/test-001.std b/tests/file/test-001.std new file mode 100644 index 0000000..36fabf3 --- /dev/null +++ b/tests/file/test-001.std @@ -0,0 +1,6 @@ +-a +1 +-- +--file +/home/boris/work/cli/cli/tests/file/test-001.ops +b diff --git a/tests/file/test-002.ops b/tests/file/test-002.ops new file mode 100644 index 0000000..5c728a0 --- /dev/null +++ b/tests/file/test-002.ops @@ -0,0 +1,7 @@ +-a 11 +-b 11 +-- +--file base.ops +-a 12 +-b 12 +a diff --git a/tests/file/test-002.std b/tests/file/test-002.std new file mode 100644 index 0000000..0336afe --- /dev/null +++ b/tests/file/test-002.std @@ -0,0 +1,17 @@ +-a +1 +-a +11 +-b +11 +-- +--file +base.ops +-a +12 +-b +12 +a +--file +/home/boris/work/cli/cli/tests/file/empty.ops +b diff --git a/tests/file/test-003.std b/tests/file/test-003.std new file mode 100644 index 0000000..6e9a6ae --- /dev/null +++ b/tests/file/test-003.std @@ -0,0 +1,7 @@ +-a +1 +-a +21 +-b +21 +unable to open file 'test-003.ops' or read failure diff --git a/tests/file/test.cli b/tests/file/test.cli new file mode 100644 index 0000000..4bd40d0 --- /dev/null +++ b/tests/file/test.cli @@ -0,0 +1,8 @@ +// file : tests/file/test.cli +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +class options +{ +}; diff --git a/tests/makefile b/tests/makefile index 422b93b..5eef36e 100644 --- a/tests/makefile +++ b/tests/makefile @@ -5,7 +5,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../build/bootstrap.make -tests := lexer parser +tests := ctor erase file lexer parser default := $(out_base)/ test := $(out_base)/.test -- cgit v1.1