aboutsummaryrefslogtreecommitdiff
path: root/libbutl/builtin-options.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'libbutl/builtin-options.cxx')
-rw-r--r--libbutl/builtin-options.cxx422
1 files changed, 382 insertions, 40 deletions
diff --git a/libbutl/builtin-options.cxx b/libbutl/builtin-options.cxx
index 5a243e5..98a47cf 100644
--- a/libbutl/builtin-options.cxx
+++ b/libbutl/builtin-options.cxx
@@ -18,6 +18,7 @@
#include <utility>
#include <ostream>
#include <sstream>
+#include <cstring>
namespace butl
{
@@ -26,7 +27,7 @@ namespace butl
// unknown_option
//
unknown_option::
- ~unknown_option () throw ()
+ ~unknown_option () noexcept
{
}
@@ -37,7 +38,7 @@ namespace butl
}
const char* unknown_option::
- what () const throw ()
+ what () const noexcept
{
return "unknown option";
}
@@ -45,7 +46,7 @@ namespace butl
// unknown_argument
//
unknown_argument::
- ~unknown_argument () throw ()
+ ~unknown_argument () noexcept
{
}
@@ -56,7 +57,7 @@ namespace butl
}
const char* unknown_argument::
- what () const throw ()
+ what () const noexcept
{
return "unknown argument";
}
@@ -64,7 +65,7 @@ namespace butl
// missing_value
//
missing_value::
- ~missing_value () throw ()
+ ~missing_value () noexcept
{
}
@@ -75,7 +76,7 @@ namespace butl
}
const char* missing_value::
- what () const throw ()
+ what () const noexcept
{
return "missing option value";
}
@@ -83,7 +84,7 @@ namespace butl
// invalid_value
//
invalid_value::
- ~invalid_value () throw ()
+ ~invalid_value () noexcept
{
}
@@ -98,7 +99,7 @@ namespace butl
}
const char* invalid_value::
- what () const throw ()
+ what () const noexcept
{
return "invalid option value";
}
@@ -112,7 +113,7 @@ namespace butl
}
const char* eos_reached::
- what () const throw ()
+ what () const noexcept
{
return "end of argument stream reached";
}
@@ -252,10 +253,31 @@ namespace butl
struct parser<bool>
{
static void
- parse (bool& x, scanner& s)
+ parse (bool& x, bool& xs, scanner& s)
{
- s.next ();
- x = true;
+ const char* o (s.next ());
+
+ if (s.more ())
+ {
+ const char* v (s.next ());
+
+ if (std::strcmp (v, "1") == 0 ||
+ std::strcmp (v, "true") == 0 ||
+ std::strcmp (v, "TRUE") == 0 ||
+ std::strcmp (v, "True") == 0)
+ x = true;
+ else if (std::strcmp (v, "0") == 0 ||
+ std::strcmp (v, "false") == 0 ||
+ std::strcmp (v, "FALSE") == 0 ||
+ std::strcmp (v, "False") == 0)
+ x = false;
+ else
+ throw invalid_value (o, v);
+ }
+ else
+ throw missing_value (o);
+
+ xs = true;
}
};
@@ -365,6 +387,56 @@ namespace butl
}
};
+ template <typename K, typename V, typename C>
+ struct parser<std::multimap<K, V, C> >
+ {
+ static void
+ parse (std::multimap<K, V, C>& m, bool& xs, scanner& s)
+ {
+ const char* o (s.next ());
+
+ if (s.more ())
+ {
+ std::size_t pos (s.position ());
+ std::string ov (s.next ());
+ std::string::size_type p = ov.find ('=');
+
+ K k = K ();
+ V v = V ();
+ std::string kstr (ov, 0, p);
+ std::string vstr (ov, (p != std::string::npos ? p + 1 : ov.size ()));
+
+ int ac (2);
+ char* av[] =
+ {
+ const_cast<char*> (o),
+ 0
+ };
+
+ bool dummy;
+ if (!kstr.empty ())
+ {
+ av[1] = const_cast<char*> (kstr.c_str ());
+ argv_scanner s (0, ac, av, false, pos);
+ parser<K>::parse (k, dummy, s);
+ }
+
+ if (!vstr.empty ())
+ {
+ av[1] = const_cast<char*> (vstr.c_str ());
+ argv_scanner s (0, ac, av, false, pos);
+ parser<V>::parse (v, dummy, s);
+ }
+
+ m.insert (typename std::multimap<K, V, C>::value_type (k, v));
+ }
+ else
+ throw missing_value (o);
+
+ xs = true;
+ }
+ };
+
template <typename X, typename T, T X::*M>
void
thunk (X& x, scanner& s)
@@ -372,6 +444,14 @@ namespace butl
parser<T>::parse (x.*M, s);
}
+ template <typename X, bool X::*M>
+ void
+ thunk (X& x, scanner& s)
+ {
+ s.next ();
+ x.*M = true;
+ }
+
template <typename X, typename T, T X::*M, bool X::*S>
void
thunk (X& x, scanner& s)
@@ -382,7 +462,6 @@ namespace butl
}
#include <map>
-#include <cstring>
namespace butl
{
@@ -733,15 +812,15 @@ namespace butl
_cli_cp_options_map_init ()
{
_cli_cp_options_map_["--recursive"] =
- &::butl::cli::thunk< cp_options, bool, &cp_options::recursive_ >;
+ &::butl::cli::thunk< cp_options, &cp_options::recursive_ >;
_cli_cp_options_map_["-R"] =
- &::butl::cli::thunk< cp_options, bool, &cp_options::recursive_ >;
+ &::butl::cli::thunk< cp_options, &cp_options::recursive_ >;
_cli_cp_options_map_["-r"] =
- &::butl::cli::thunk< cp_options, bool, &cp_options::recursive_ >;
+ &::butl::cli::thunk< cp_options, &cp_options::recursive_ >;
_cli_cp_options_map_["--preserve"] =
- &::butl::cli::thunk< cp_options, bool, &cp_options::preserve_ >;
+ &::butl::cli::thunk< cp_options, &cp_options::preserve_ >;
_cli_cp_options_map_["-p"] =
- &::butl::cli::thunk< cp_options, bool, &cp_options::preserve_ >;
+ &::butl::cli::thunk< cp_options, &cp_options::preserve_ >;
}
};
@@ -1007,9 +1086,9 @@ namespace butl
_cli_date_options_map_init ()
{
_cli_date_options_map_["--utc"] =
- &::butl::cli::thunk< date_options, bool, &date_options::utc_ >;
+ &::butl::cli::thunk< date_options, &date_options::utc_ >;
_cli_date_options_map_["-u"] =
- &::butl::cli::thunk< date_options, bool, &date_options::utc_ >;
+ &::butl::cli::thunk< date_options, &date_options::utc_ >;
}
};
@@ -1192,6 +1271,269 @@ namespace butl
return r;
}
+ // find_options
+ //
+
+ find_options::
+ find_options ()
+ {
+ }
+
+ bool find_options::
+ parse (int& argc,
+ char** argv,
+ bool erase,
+ ::butl::cli::unknown_mode opt,
+ ::butl::cli::unknown_mode arg)
+ {
+ ::butl::cli::argv_scanner s (argc, argv, erase);
+ bool r = _parse (s, opt, arg);
+ return r;
+ }
+
+ bool find_options::
+ parse (int start,
+ int& argc,
+ char** argv,
+ bool erase,
+ ::butl::cli::unknown_mode opt,
+ ::butl::cli::unknown_mode arg)
+ {
+ ::butl::cli::argv_scanner s (start, argc, argv, erase);
+ bool r = _parse (s, opt, arg);
+ return r;
+ }
+
+ bool find_options::
+ parse (int& argc,
+ char** argv,
+ int& end,
+ bool erase,
+ ::butl::cli::unknown_mode opt,
+ ::butl::cli::unknown_mode arg)
+ {
+ ::butl::cli::argv_scanner s (argc, argv, erase);
+ bool r = _parse (s, opt, arg);
+ end = s.end ();
+ return r;
+ }
+
+ bool find_options::
+ parse (int start,
+ int& argc,
+ char** argv,
+ int& end,
+ bool erase,
+ ::butl::cli::unknown_mode opt,
+ ::butl::cli::unknown_mode arg)
+ {
+ ::butl::cli::argv_scanner s (start, argc, argv, erase);
+ bool r = _parse (s, opt, arg);
+ end = s.end ();
+ return r;
+ }
+
+ bool find_options::
+ parse (::butl::cli::scanner& s,
+ ::butl::cli::unknown_mode opt,
+ ::butl::cli::unknown_mode arg)
+ {
+ bool r = _parse (s, opt, arg);
+ return r;
+ }
+
+ typedef
+ std::map<std::string, void (*) (find_options&, ::butl::cli::scanner&)>
+ _cli_find_options_map;
+
+ static _cli_find_options_map _cli_find_options_map_;
+
+ struct _cli_find_options_map_init
+ {
+ _cli_find_options_map_init ()
+ {
+ }
+ };
+
+ static _cli_find_options_map_init _cli_find_options_map_init_;
+
+ bool find_options::
+ _parse (const char* o, ::butl::cli::scanner& s)
+ {
+ _cli_find_options_map::const_iterator i (_cli_find_options_map_.find (o));
+
+ if (i != _cli_find_options_map_.end ())
+ {
+ (*(i->second)) (*this, s);
+ return true;
+ }
+
+ return false;
+ }
+
+ bool find_options::
+ _parse (::butl::cli::scanner& s,
+ ::butl::cli::unknown_mode opt_mode,
+ ::butl::cli::unknown_mode arg_mode)
+ {
+ // Can't skip combined flags (--no-combined-flags).
+ //
+ assert (opt_mode != ::butl::cli::unknown_mode::skip);
+
+ bool r = false;
+ bool opt = true;
+
+ while (s.more ())
+ {
+ const char* o = s.peek ();
+
+ if (std::strcmp (o, "--") == 0)
+ {
+ opt = false;
+ }
+
+ if (opt)
+ {
+ if (_parse (o, s))
+ {
+ r = true;
+ continue;
+ }
+
+ if (std::strncmp (o, "-", 1) == 0 && o[1] != '\0')
+ {
+ // Handle combined option values.
+ //
+ std::string co;
+ if (const char* v = std::strchr (o, '='))
+ {
+ co.assign (o, 0, v - o);
+ ++v;
+
+ int ac (2);
+ char* av[] =
+ {
+ const_cast<char*> (co.c_str ()),
+ const_cast<char*> (v)
+ };
+
+ ::butl::cli::argv_scanner ns (0, ac, av);
+
+ if (_parse (co.c_str (), ns))
+ {
+ // Parsed the option but not its value?
+ //
+ if (ns.end () != 2)
+ throw ::butl::cli::invalid_value (co, v);
+
+ s.next ();
+ r = true;
+ continue;
+ }
+ else
+ {
+ // Set the unknown option and fall through.
+ //
+ o = co.c_str ();
+ }
+ }
+
+ // Handle combined flags.
+ //
+ char cf[3];
+ {
+ const char* p = o + 1;
+ for (; *p != '\0'; ++p)
+ {
+ if (!((*p >= 'a' && *p <= 'z') ||
+ (*p >= 'A' && *p <= 'Z') ||
+ (*p >= '0' && *p <= '9')))
+ break;
+ }
+
+ if (*p == '\0')
+ {
+ for (p = o + 1; *p != '\0'; ++p)
+ {
+ std::strcpy (cf, "-");
+ cf[1] = *p;
+ cf[2] = '\0';
+
+ int ac (1);
+ char* av[] =
+ {
+ cf
+ };
+
+ ::butl::cli::argv_scanner ns (0, ac, av);
+
+ if (!_parse (cf, ns))
+ break;
+ }
+
+ if (*p == '\0')
+ {
+ // All handled.
+ //
+ s.next ();
+ r = true;
+ continue;
+ }
+ else
+ {
+ // Set the unknown option and fall through.
+ //
+ o = cf;
+ }
+ }
+ }
+
+ switch (opt_mode)
+ {
+ case ::butl::cli::unknown_mode::skip:
+ {
+ s.skip ();
+ r = true;
+ continue;
+ }
+ case ::butl::cli::unknown_mode::stop:
+ {
+ break;
+ }
+ case ::butl::cli::unknown_mode::fail:
+ {
+ throw ::butl::cli::unknown_option (o);
+ }
+ }
+
+ break;
+ }
+ }
+
+ switch (arg_mode)
+ {
+ case ::butl::cli::unknown_mode::skip:
+ {
+ s.skip ();
+ r = true;
+ continue;
+ }
+ case ::butl::cli::unknown_mode::stop:
+ {
+ break;
+ }
+ case ::butl::cli::unknown_mode::fail:
+ {
+ throw ::butl::cli::unknown_argument (o);
+ }
+ }
+
+ break;
+ }
+
+ return r;
+ }
+
// ln_options
//
@@ -1275,9 +1617,9 @@ namespace butl
_cli_ln_options_map_init ()
{
_cli_ln_options_map_["--symbolic"] =
- &::butl::cli::thunk< ln_options, bool, &ln_options::symbolic_ >;
+ &::butl::cli::thunk< ln_options, &ln_options::symbolic_ >;
_cli_ln_options_map_["-s"] =
- &::butl::cli::thunk< ln_options, bool, &ln_options::symbolic_ >;
+ &::butl::cli::thunk< ln_options, &ln_options::symbolic_ >;
}
};
@@ -1543,9 +1885,9 @@ namespace butl
_cli_mkdir_options_map_init ()
{
_cli_mkdir_options_map_["--parents"] =
- &::butl::cli::thunk< mkdir_options, bool, &mkdir_options::parents_ >;
+ &::butl::cli::thunk< mkdir_options, &mkdir_options::parents_ >;
_cli_mkdir_options_map_["-p"] =
- &::butl::cli::thunk< mkdir_options, bool, &mkdir_options::parents_ >;
+ &::butl::cli::thunk< mkdir_options, &mkdir_options::parents_ >;
}
};
@@ -1811,9 +2153,9 @@ namespace butl
_cli_mv_options_map_init ()
{
_cli_mv_options_map_["--force"] =
- &::butl::cli::thunk< mv_options, bool, &mv_options::force_ >;
+ &::butl::cli::thunk< mv_options, &mv_options::force_ >;
_cli_mv_options_map_["-f"] =
- &::butl::cli::thunk< mv_options, bool, &mv_options::force_ >;
+ &::butl::cli::thunk< mv_options, &mv_options::force_ >;
}
};
@@ -2080,13 +2422,13 @@ namespace butl
_cli_rm_options_map_init ()
{
_cli_rm_options_map_["--recursive"] =
- &::butl::cli::thunk< rm_options, bool, &rm_options::recursive_ >;
+ &::butl::cli::thunk< rm_options, &rm_options::recursive_ >;
_cli_rm_options_map_["-r"] =
- &::butl::cli::thunk< rm_options, bool, &rm_options::recursive_ >;
+ &::butl::cli::thunk< rm_options, &rm_options::recursive_ >;
_cli_rm_options_map_["--force"] =
- &::butl::cli::thunk< rm_options, bool, &rm_options::force_ >;
+ &::butl::cli::thunk< rm_options, &rm_options::force_ >;
_cli_rm_options_map_["-f"] =
- &::butl::cli::thunk< rm_options, bool, &rm_options::force_ >;
+ &::butl::cli::thunk< rm_options, &rm_options::force_ >;
}
};
@@ -2352,9 +2694,9 @@ namespace butl
_cli_rmdir_options_map_init ()
{
_cli_rmdir_options_map_["--force"] =
- &::butl::cli::thunk< rmdir_options, bool, &rmdir_options::force_ >;
+ &::butl::cli::thunk< rmdir_options, &rmdir_options::force_ >;
_cli_rmdir_options_map_["-f"] =
- &::butl::cli::thunk< rmdir_options, bool, &rmdir_options::force_ >;
+ &::butl::cli::thunk< rmdir_options, &rmdir_options::force_ >;
}
};
@@ -2623,13 +2965,13 @@ namespace butl
_cli_sed_options_map_init ()
{
_cli_sed_options_map_["--quiet"] =
- &::butl::cli::thunk< sed_options, bool, &sed_options::quiet_ >;
+ &::butl::cli::thunk< sed_options, &sed_options::quiet_ >;
_cli_sed_options_map_["-n"] =
- &::butl::cli::thunk< sed_options, bool, &sed_options::quiet_ >;
+ &::butl::cli::thunk< sed_options, &sed_options::quiet_ >;
_cli_sed_options_map_["--in-place"] =
- &::butl::cli::thunk< sed_options, bool, &sed_options::in_place_ >;
+ &::butl::cli::thunk< sed_options, &sed_options::in_place_ >;
_cli_sed_options_map_["-i"] =
- &::butl::cli::thunk< sed_options, bool, &sed_options::in_place_ >;
+ &::butl::cli::thunk< sed_options, &sed_options::in_place_ >;
_cli_sed_options_map_["--expression"] =
&::butl::cli::thunk< sed_options, std::vector<std::string>, &sed_options::expression_,
&sed_options::expression_specified_ >;
@@ -3165,13 +3507,13 @@ namespace butl
_cli_test_options_map_init ()
{
_cli_test_options_map_["--file"] =
- &::butl::cli::thunk< test_options, bool, &test_options::file_ >;
+ &::butl::cli::thunk< test_options, &test_options::file_ >;
_cli_test_options_map_["-f"] =
- &::butl::cli::thunk< test_options, bool, &test_options::file_ >;
+ &::butl::cli::thunk< test_options, &test_options::file_ >;
_cli_test_options_map_["--directory"] =
- &::butl::cli::thunk< test_options, bool, &test_options::directory_ >;
+ &::butl::cli::thunk< test_options, &test_options::directory_ >;
_cli_test_options_map_["-d"] =
- &::butl::cli::thunk< test_options, bool, &test_options::directory_ >;
+ &::butl::cli::thunk< test_options, &test_options::directory_ >;
}
};