From 004062737657986294163672ae67d16348e203f6 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 14 Oct 2020 20:43:20 +0300 Subject: Make changes required for test --- migrate/migrate-options.cxx | 853 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 853 insertions(+) create mode 100644 migrate/migrate-options.cxx (limited to 'migrate/migrate-options.cxx') diff --git a/migrate/migrate-options.cxx b/migrate/migrate-options.cxx new file mode 100644 index 0000000..c4a3cb1 --- /dev/null +++ b/migrate/migrate-options.cxx @@ -0,0 +1,853 @@ +// -*- C++ -*- +// +// This file was generated by CLI, a command line interface +// compiler for C++. +// + +// Begin prologue. +// +// +// End prologue. + +#include + +#include +#include +#include +#include +#include +#include + +namespace cli +{ + // unknown_option + // + unknown_option:: + ~unknown_option () throw () + { + } + + void unknown_option:: + print (::std::ostream& os) const + { + os << "unknown option '" << option ().c_str () << "'"; + } + + const char* unknown_option:: + what () const throw () + { + return "unknown option"; + } + + // unknown_argument + // + unknown_argument:: + ~unknown_argument () throw () + { + } + + void unknown_argument:: + print (::std::ostream& os) const + { + os << "unknown argument '" << argument ().c_str () << "'"; + } + + const char* unknown_argument:: + what () const throw () + { + return "unknown argument"; + } + + // missing_value + // + missing_value:: + ~missing_value () throw () + { + } + + void missing_value:: + print (::std::ostream& os) const + { + os << "missing value for option '" << option ().c_str () << "'"; + } + + const char* missing_value:: + what () const throw () + { + return "missing option value"; + } + + // invalid_value + // + invalid_value:: + ~invalid_value () throw () + { + } + + void invalid_value:: + print (::std::ostream& os) const + { + os << "invalid value '" << value ().c_str () << "' for option '" + << option ().c_str () << "'"; + + if (!message ().empty ()) + os << ": " << message ().c_str (); + } + + const char* invalid_value:: + what () const throw () + { + return "invalid option value"; + } + + // eos_reached + // + void eos_reached:: + print (::std::ostream& os) const + { + os << what (); + } + + const char* eos_reached:: + what () const throw () + { + return "end of argument stream reached"; + } + + // scanner + // + scanner:: + ~scanner () + { + } + + // argv_scanner + // + bool argv_scanner:: + more () + { + return i_ < argc_; + } + + const char* argv_scanner:: + peek () + { + if (i_ < argc_) + return argv_[i_]; + else + throw eos_reached (); + } + + const char* argv_scanner:: + next () + { + if (i_ < argc_) + { + const char* r (argv_[i_]); + + if (erase_) + { + for (int i (i_ + 1); i < argc_; ++i) + argv_[i - 1] = argv_[i]; + + --argc_; + argv_[argc_] = 0; + } + else + ++i_; + + return r; + } + else + throw eos_reached (); + } + + void argv_scanner:: + skip () + { + if (i_ < argc_) + ++i_; + else + throw eos_reached (); + } + + template + struct parser + { + static void + parse (X& x, bool& xs, scanner& s) + { + using namespace std; + + const char* o (s.next ()); + if (s.more ()) + { + string v (s.next ()); + istringstream is (v); + if (!(is >> x && is.peek () == istringstream::traits_type::eof ())) + throw invalid_value (o, v); + } + else + throw missing_value (o); + + xs = true; + } + }; + + template <> + struct parser + { + static void + parse (bool& x, scanner& s) + { + s.next (); + x = true; + } + }; + + template <> + struct parser + { + static void + parse (std::string& x, bool& xs, scanner& s) + { + const char* o (s.next ()); + + if (s.more ()) + x = s.next (); + else + throw missing_value (o); + + xs = true; + } + }; + + template + struct parser > + { + static void + parse (std::vector& c, bool& xs, scanner& s) + { + X x; + bool dummy; + parser::parse (x, dummy, s); + c.push_back (x); + xs = true; + } + }; + + template + struct parser > + { + static void + parse (std::set& c, bool& xs, scanner& s) + { + X x; + bool dummy; + parser::parse (x, dummy, s); + c.insert (x); + xs = true; + } + }; + + template + struct parser > + { + static void + parse (std::map& m, bool& xs, scanner& s) + { + const char* o (s.next ()); + + if (s.more ()) + { + 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 (o), + 0 + }; + + bool dummy; + if (!kstr.empty ()) + { + av[1] = const_cast (kstr.c_str ()); + argv_scanner s (0, ac, av); + parser::parse (k, dummy, s); + } + + if (!vstr.empty ()) + { + av[1] = const_cast (vstr.c_str ()); + argv_scanner s (0, ac, av); + parser::parse (v, dummy, s); + } + + m[k] = v; + } + else + throw missing_value (o); + + xs = true; + } + }; + + template + void + thunk (X& x, scanner& s) + { + parser::parse (x.*M, s); + } + + template + void + thunk (X& x, scanner& s) + { + parser::parse (x.*M, x.*S, s); + } +} + +#include +#include + +// options +// + +options:: +options () +: recreate_ (), + drop_ (), + db_user_ (), + db_user_specified_ (false), + db_password_ (), + db_password_specified_ (false), + db_name_ (), + db_name_specified_ (false), + db_host_ (), + db_host_specified_ (false), + db_port_ (0), + db_port_specified_ (false), + pager_ (), + pager_specified_ (false), + pager_option_ (), + pager_option_specified_ (false), + help_ (), + version_ () +{ +} + +options:: +options (int& argc, + char** argv, + bool erase, + ::cli::unknown_mode opt, + ::cli::unknown_mode arg) +: recreate_ (), + drop_ (), + db_user_ (), + db_user_specified_ (false), + db_password_ (), + db_password_specified_ (false), + db_name_ (), + db_name_specified_ (false), + db_host_ (), + db_host_specified_ (false), + db_port_ (0), + db_port_specified_ (false), + pager_ (), + pager_specified_ (false), + pager_option_ (), + pager_option_specified_ (false), + help_ (), + version_ () +{ + ::cli::argv_scanner s (argc, argv, erase); + _parse (s, opt, arg); +} + +options:: +options (int start, + int& argc, + char** argv, + bool erase, + ::cli::unknown_mode opt, + ::cli::unknown_mode arg) +: recreate_ (), + drop_ (), + db_user_ (), + db_user_specified_ (false), + db_password_ (), + db_password_specified_ (false), + db_name_ (), + db_name_specified_ (false), + db_host_ (), + db_host_specified_ (false), + db_port_ (0), + db_port_specified_ (false), + pager_ (), + pager_specified_ (false), + pager_option_ (), + pager_option_specified_ (false), + help_ (), + version_ () +{ + ::cli::argv_scanner s (start, argc, argv, erase); + _parse (s, opt, arg); +} + +options:: +options (int& argc, + char** argv, + int& end, + bool erase, + ::cli::unknown_mode opt, + ::cli::unknown_mode arg) +: recreate_ (), + drop_ (), + db_user_ (), + db_user_specified_ (false), + db_password_ (), + db_password_specified_ (false), + db_name_ (), + db_name_specified_ (false), + db_host_ (), + db_host_specified_ (false), + db_port_ (0), + db_port_specified_ (false), + pager_ (), + pager_specified_ (false), + pager_option_ (), + pager_option_specified_ (false), + help_ (), + version_ () +{ + ::cli::argv_scanner s (argc, argv, erase); + _parse (s, opt, arg); + end = s.end (); +} + +options:: +options (int start, + int& argc, + char** argv, + int& end, + bool erase, + ::cli::unknown_mode opt, + ::cli::unknown_mode arg) +: recreate_ (), + drop_ (), + db_user_ (), + db_user_specified_ (false), + db_password_ (), + db_password_specified_ (false), + db_name_ (), + db_name_specified_ (false), + db_host_ (), + db_host_specified_ (false), + db_port_ (0), + db_port_specified_ (false), + pager_ (), + pager_specified_ (false), + pager_option_ (), + pager_option_specified_ (false), + help_ (), + version_ () +{ + ::cli::argv_scanner s (start, argc, argv, erase); + _parse (s, opt, arg); + end = s.end (); +} + +options:: +options (::cli::scanner& s, + ::cli::unknown_mode opt, + ::cli::unknown_mode arg) +: recreate_ (), + drop_ (), + db_user_ (), + db_user_specified_ (false), + db_password_ (), + db_password_specified_ (false), + db_name_ (), + db_name_specified_ (false), + db_host_ (), + db_host_specified_ (false), + db_port_ (0), + db_port_specified_ (false), + pager_ (), + pager_specified_ (false), + pager_option_ (), + pager_option_specified_ (false), + help_ (), + version_ () +{ + _parse (s, opt, arg); +} + +::cli::usage_para options:: +print_usage (::std::ostream& os, ::cli::usage_para p) +{ + CLI_POTENTIALLY_UNUSED (os); + + if (p != ::cli::usage_para::none) + os << ::std::endl; + + os << "\033[1mOPTIONS\033[0m" << ::std::endl; + + os << std::endl + << "\033[1m--recreate\033[0m Recreate the database schema (all the existing data will be" << ::std::endl + << " lost)." << ::std::endl; + + os << std::endl + << "\033[1m--drop\033[0m Drop the database schema (all the existing data will be" << ::std::endl + << " lost)." << ::std::endl; + + os << std::endl + << "\033[1m--db-user\033[0m|\033[1m-u\033[0m \033[4muser\033[0m Database user name. If not specified, then operating system" << ::std::endl + << " (login) name is used." << ::std::endl; + + os << std::endl + << "\033[1m--db-password\033[0m \033[4mpass\033[0m Database password. If not specified, then login without" << ::std::endl + << " password is expected to work." << ::std::endl; + + os << std::endl + << "\033[1m--db-name\033[0m|\033[1m-n\033[0m \033[4mname\033[0m Database name. If not specified, then it is implicitly" << ::std::endl + << " derived by prefixing the schema name with \033[1mbrep_\033[0m." << ::std::endl; + + os << std::endl + << "\033[1m--db-host\033[0m|\033[1m-h\033[0m \033[4mhost\033[0m Database host name, address, or socket. If not specified," << ::std::endl + << " then connect to \033[1mlocalhost\033[0m using the operating system-default" << ::std::endl + << " mechanism (Unix-domain socket, etc)." << ::std::endl; + + os << std::endl + << "\033[1m--db-port\033[0m|\033[1m-p\033[0m \033[4mport\033[0m Database port number. If not specified, the default port is" << ::std::endl + << " used." << ::std::endl; + + os << std::endl + << "\033[1m--pager\033[0m \033[4mpath\033[0m The pager program to be used to show long text. Commonly" << ::std::endl + << " used pager programs are \033[1mless\033[0m and \033[1mmore\033[0m. You can also specify" << ::std::endl + << " additional options that should be passed to the pager" << ::std::endl + << " program with \033[1m--pager-option\033[0m. If an empty string is specified" << ::std::endl + << " as the pager program, then no pager will be used. If the" << ::std::endl + << " pager program is not explicitly specified, then" << ::std::endl + << " \033[1mbrep-migrate\033[0m will try to use \033[1mless\033[0m. If it is not available," << ::std::endl + << " then no pager will be used." << ::std::endl; + + os << std::endl + << "\033[1m--pager-option\033[0m \033[4mopt\033[0m Additional option to be passed to the pager program. See" << ::std::endl + << " \033[1m--pager\033[0m for more information on the pager program. Repeat" << ::std::endl + << " this option to specify multiple pager options." << ::std::endl; + + os << std::endl + << "\033[1m--help\033[0m Print usage information and exit." << ::std::endl; + + os << std::endl + << "\033[1m--version\033[0m Print version and exit." << ::std::endl; + + p = ::cli::usage_para::option; + + return p; +} + +typedef +std::map +_cli_options_map; + +static _cli_options_map _cli_options_map_; + +struct _cli_options_map_init +{ + _cli_options_map_init () + { + _cli_options_map_["--recreate"] = + &::cli::thunk< options, bool, &options::recreate_ >; + _cli_options_map_["--drop"] = + &::cli::thunk< options, bool, &options::drop_ >; + _cli_options_map_["--db-user"] = + &::cli::thunk< options, std::string, &options::db_user_, + &options::db_user_specified_ >; + _cli_options_map_["-u"] = + &::cli::thunk< options, std::string, &options::db_user_, + &options::db_user_specified_ >; + _cli_options_map_["--db-password"] = + &::cli::thunk< options, std::string, &options::db_password_, + &options::db_password_specified_ >; + _cli_options_map_["--db-name"] = + &::cli::thunk< options, std::string, &options::db_name_, + &options::db_name_specified_ >; + _cli_options_map_["-n"] = + &::cli::thunk< options, std::string, &options::db_name_, + &options::db_name_specified_ >; + _cli_options_map_["--db-host"] = + &::cli::thunk< options, std::string, &options::db_host_, + &options::db_host_specified_ >; + _cli_options_map_["-h"] = + &::cli::thunk< options, std::string, &options::db_host_, + &options::db_host_specified_ >; + _cli_options_map_["--db-port"] = + &::cli::thunk< options, std::uint16_t, &options::db_port_, + &options::db_port_specified_ >; + _cli_options_map_["-p"] = + &::cli::thunk< options, std::uint16_t, &options::db_port_, + &options::db_port_specified_ >; + _cli_options_map_["--pager"] = + &::cli::thunk< options, std::string, &options::pager_, + &options::pager_specified_ >; + _cli_options_map_["--pager-option"] = + &::cli::thunk< options, std::vector, &options::pager_option_, + &options::pager_option_specified_ >; + _cli_options_map_["--help"] = + &::cli::thunk< options, bool, &options::help_ >; + _cli_options_map_["--version"] = + &::cli::thunk< options, bool, &options::version_ >; + } +}; + +static _cli_options_map_init _cli_options_map_init_; + +bool options:: +_parse (const char* o, ::cli::scanner& s) +{ + _cli_options_map::const_iterator i (_cli_options_map_.find (o)); + + if (i != _cli_options_map_.end ()) + { + (*(i->second)) (*this, s); + return true; + } + + return false; +} + +bool options:: +_parse (::cli::scanner& s, + ::cli::unknown_mode opt_mode, + ::cli::unknown_mode arg_mode) +{ + // Can't skip combined flags (--no-combined-flags). + // + assert (opt_mode != ::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; + s.skip (); + r = true; + continue; + } + + 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 (co.c_str ()), + const_cast (v) + }; + + ::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 ::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 + }; + + ::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 ::cli::unknown_mode::skip: + { + s.skip (); + r = true; + continue; + } + case ::cli::unknown_mode::stop: + { + break; + } + case ::cli::unknown_mode::fail: + { + throw ::cli::unknown_option (o); + } + } + + break; + } + } + + switch (arg_mode) + { + case ::cli::unknown_mode::skip: + { + s.skip (); + r = true; + continue; + } + case ::cli::unknown_mode::stop: + { + break; + } + case ::cli::unknown_mode::fail: + { + throw ::cli::unknown_argument (o); + } + } + + break; + } + + return r; +} + +::cli::usage_para +print_usage (::std::ostream& os, ::cli::usage_para p) +{ + CLI_POTENTIALLY_UNUSED (os); + + if (p != ::cli::usage_para::none) + os << ::std::endl; + + os << "\033[1mSYNOPSIS\033[0m" << ::std::endl + << ::std::endl + << "\033[1mbrep-migrate --help\033[0m" << ::std::endl + << "\033[1mbrep-migrate --version\033[0m" << ::std::endl + << "\033[1mbrep-migrate\033[0m [\033[4moptions\033[0m] \033[4mschema\033[0m\033[0m" << ::std::endl + << ::std::endl + << "\033[1mDESCRIPTION\033[0m" << ::std::endl + << ::std::endl + << "In its default mode \033[1mbrep-migrate\033[0m creates the database schema if it doesn't" << ::std::endl + << "already exist. Otherwise, it migrates the existing schema and data to the" << ::std::endl + << "current version, if needed. The valid schema names are \033[1mpackage\033[0m and \033[1mbuild\033[0m." << ::std::endl + << ::std::endl + << "If the \033[1m--recreate\033[0m option is specified, then \033[1mbrep-migrate\033[0m instead recreates the" << ::std::endl + << "database schema. That is, it drops all the existing tables (and their data) and" << ::std::endl + << "then creates them from scratch." << ::std::endl + << ::std::endl + << "If the \033[1m--drop\033[0m option is specified, then \033[1mbrep-migrate\033[0m drops all the existing" << ::std::endl + << "tables (and their data)." << ::std::endl + << ::std::endl + << "The \033[1m--recreate\033[0m and \033[1m--drop\033[0m options are mutually exclusive. When specified, they" << ::std::endl + << "will cause \033[1mbrep-migrate\033[0m to fail if the database schema requires migration. In" << ::std::endl + << "this case you can either migrate the database first or drop the entire database" << ::std::endl + << "using, for example, \033[1mpsql(1)\033[0m." << ::std::endl; + + p = ::options::print_usage (os, ::cli::usage_para::text); + + if (p != ::cli::usage_para::none) + os << ::std::endl; + + os << "\033[1mEXIT STATUS\033[0m" << ::std::endl + << ::std::endl + << "\033[1m0\033[0m" << ::std::endl + << " Success." << ::std::endl + << "\033[1m1\033[0m" << ::std::endl + << " Fatal error." << ::std::endl + << "\033[1m2\033[0m" << ::std::endl + << " An instance of \033[1mbrep-migrate\033[0m or some other \033[1mbrep\033[0m utility is already running." << ::std::endl + << " Try again." << ::std::endl + << "\033[1m3\033[0m" << ::std::endl + << " Recoverable database error. Try again." << ::std::endl; + + p = ::cli::usage_para::text; + + return p; +} + +// Begin epilogue. +// +// +// End epilogue. + -- cgit v1.1