aboutsummaryrefslogtreecommitdiff
path: root/bdep/new-parsers.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-03-13 10:34:57 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2018-03-13 10:34:57 +0200
commit6be9c7746f92aa721782a4d0eaff5f901fc528cd (patch)
treead072fd130d3bb64acee0be183698daa328e8966 /bdep/new-parsers.cxx
parent5dcbecfd8b83f516c067780214f06321f03d1cce (diff)
Setup command line infrastructure for new command
Diffstat (limited to 'bdep/new-parsers.cxx')
-rw-r--r--bdep/new-parsers.cxx114
1 files changed, 114 insertions, 0 deletions
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 <bdep/new-parsers.hxx>
+
+#include <bdep/new-options.hxx> // 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 <typename O>
+ 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<string> 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<type>::
+ 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<cmd_new_exe_options> (o, v, i);
+ }
+ else if (l == "lib")
+ {
+ r.type = type::lib;
+ r.lib_opt = parse_options<cmd_new_lib_options> (o, v, i);
+ }
+ else if (l == "bare")
+ {
+ r.type = type::bare;
+ r.bare_opt = parse_options<cmd_new_bare_options> (o, v, i);
+ }
+ else
+ throw invalid_value (o, l);
+
+ xs = true;
+ }
+
+ void parser<lang>::
+ 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<cmd_new_c_options> (o, v, i);
+ }
+ else if (l == "c++")
+ {
+ r.lang = lang::cxx;
+ r.cxx_opt = parse_options<cmd_new_cxx_options> (o, v, i);
+ }
+ else
+ throw invalid_value (o, l);
+
+ xs = true;
+ }
+ }
+}