From 6be9c7746f92aa721782a4d0eaff5f901fc528cd Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 13 Mar 2018 10:34:57 +0200 Subject: Setup command line infrastructure for new command --- bdep/new-parsers.cxx | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 bdep/new-parsers.cxx (limited to 'bdep/new-parsers.cxx') diff --git a/bdep/new-parsers.cxx b/bdep/new-parsers.cxx new file mode 100644 index 0000000..d5b8e6e --- /dev/null +++ b/bdep/new-parsers.cxx @@ -0,0 +1,114 @@ +// file : bdep/new-parsers.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include // bdep::cli namespace, cmd_new_*_options + +namespace bdep +{ + namespace cli + { + using type = cmd_new_type; + using lang = cmd_new_lang; + + // Parse comma-separated list of of options starting from the first comma + // at pos. + // + template + static O + parse_options (const char* o, const string v, size_t pos) + { + // Use vector_scanner to parse the comma-separated list as options. + // + vector os; + for (size_t i (pos), j; i != string::npos; ) + { + j = i + 1; + i = v.find (',', j); + os.push_back (string (v, j, i != string::npos ? i - j : i)); + } + + vector_scanner s (os); + + try + { + O r; + r.parse (s, + unknown_mode::fail /* unknown_option */, + unknown_mode::fail /* unknown_argument */); + return r; + } + catch (const unknown_option& e) + { + throw invalid_value (o, e.option ()); + } + catch (const unknown_argument& e) + { + throw invalid_value (o, e.argument ()); + } + } + + void parser:: + parse (type& r, bool& xs, scanner& s) + { + const char* o (s.next ()); + + if (!s.more ()) + throw missing_value (o); + + string v (s.next ()); + size_t i (v.find (',')); + string l (v, 0, i); + + if (l == "exe") + { + r.type = type::exe; + r.exe_opt = parse_options (o, v, i); + } + else if (l == "lib") + { + r.type = type::lib; + r.lib_opt = parse_options (o, v, i); + } + else if (l == "bare") + { + r.type = type::bare; + r.bare_opt = parse_options (o, v, i); + } + else + throw invalid_value (o, l); + + xs = true; + } + + void parser:: + parse (lang& r, bool& xs, scanner& s) + { + const char* o (s.next ()); + + if (!s.more ()) + throw missing_value (o); + + string v (s.next ()); + size_t i (v.find (',')); + string l (v, 0, i); + + if (l == "c") + { + r.lang = lang::c; + r.c_opt = parse_options (o, v, i); + } + else if (l == "c++") + { + r.lang = lang::cxx; + r.cxx_opt = parse_options (o, v, i); + } + else + throw invalid_value (o, l); + + xs = true; + } + } +} -- cgit v1.1