From 0426335af0e7577148903be7a2534c4ced06cd3b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 3 Aug 2021 14:11:17 +0200 Subject: Add support for tracking argument/option position The scanner interface now provides the position() function that returns a monotonically-increasing number which, if stored, can later be used to determine the relative position of the arguments. There is also now a parser implementation for std::pair which parses the value T into the first half of the pair and stores the option position in the second half. Together, this can be used to establish the relative position of different options, for example: class options { std::vector> --config-id; std::vector> --config-name; }; cli::argv_scanner scan (argc, argv); options ops (scan); // Iterate over --config-id and --config-name options in the order // specified by the user. // auto ii (ops.config_id ().begin ()); auto ni (ops.config_name ().begin ()); for (size_t i (0), n (scan.position ()); i != n; ++i) { if (ii != ops.config_id ().end () && ii->second == i) { // Handle *ii. ++ii; } if (ni != ops.config_name ().end () && ni->second == i) { // Handle *ni. ++ni; } } --- cli/cli/runtime-inline.cxx | 63 +++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 20 deletions(-) (limited to 'cli/cli/runtime-inline.cxx') diff --git a/cli/cli/runtime-inline.cxx b/cli/cli/runtime-inline.cxx index 8f0e84c..e3dce1b 100644 --- a/cli/cli/runtime-inline.cxx +++ b/cli/cli/runtime-inline.cxx @@ -232,14 +232,29 @@ generate_runtime_inline (context& ctx) << "//" << endl; os << inl << "argv_scanner::" << endl - << "argv_scanner (int& argc, char** argv, bool erase)" << endl - << ": i_ (1), argc_ (argc), argv_ (argv), erase_ (erase)" + << "argv_scanner (int& argc," << endl + << "char** argv," << endl + << "bool erase," << endl + << "std::size_t sp)" << endl + << ": start_position_ (sp + 1)," << endl + << " i_ (1)," << endl + << " argc_ (argc)," << endl + << " argv_ (argv)," << endl + << " erase_ (erase)" << "{" << "}"; os << inl << "argv_scanner::" << endl - << "argv_scanner (int start, int& argc, char** argv, bool erase)" << endl - << ": i_ (start), argc_ (argc), argv_ (argv), erase_ (erase)" + << "argv_scanner (int start," << endl + << "int& argc," << endl + << "char** argv," << endl + << "bool erase," << endl + << "std::size_t sp)" << endl + << ": start_position_ (sp + static_cast (start))," << endl + << " i_ (start)," << endl + << " argc_ (argc)," << endl + << " argv_ (argv)," << endl + << " erase_ (erase)" << "{" << "}"; @@ -257,9 +272,10 @@ generate_runtime_inline (context& ctx) << "//" << endl; os << inl << "vector_scanner::" << endl - << "vector_scanner (const std::vector& v, " << - "std::size_t i)" << endl - << ": v_ (v), i_ (i)" + << "vector_scanner (const std::vector& v," << endl + << "std::size_t i," << endl + << "std::size_t sp)" << endl + << ": start_position_ (sp), v_ (v), i_ (i)" << "{" << "}"; @@ -270,9 +286,10 @@ generate_runtime_inline (context& ctx) << "}"; os << inl << "void vector_scanner::" << endl - << "reset (std::size_t i)" + << "reset (std::size_t i, std::size_t sp)" << "{" << "i_ = i;" + << "start_position_ = sp;" << "}"; } @@ -289,8 +306,9 @@ generate_runtime_inline (context& ctx) << "argv_file_scanner (int& argc," << endl << "char** argv," << endl << "const std::string& option," << endl - << "bool erase)" << endl - << ": argv_scanner (argc, argv, erase)," << endl + << "bool erase," << endl + << "std::size_t sp)" << endl + << ": argv_scanner (argc, argv, erase, sp)," << endl << " option_ (option)," << endl << " options_ (&option_info_)," << endl << " options_count_ (1)," << endl @@ -308,8 +326,9 @@ generate_runtime_inline (context& ctx) << "int& argc," << endl << "char** argv," << endl << "const std::string& option," << endl - << "bool erase)" << endl - << ": argv_scanner (start, argc, argv, erase)," << endl + << "bool erase," << endl + << "std::size_t sp)" << endl + << ": argv_scanner (start, argc, argv, erase, sp)," << endl << " option_ (option)," << endl << " options_ (&option_info_)," << endl << " options_count_ (1)," << endl @@ -324,8 +343,9 @@ generate_runtime_inline (context& ctx) os << inl << "argv_file_scanner::" << endl << "argv_file_scanner (const std::string& file," << endl - << "const std::string& option)" << endl - << ": argv_scanner (0, zero_argc_, 0)," << endl + << "const std::string& option," << endl + << "std::size_t sp)" << endl + << ": argv_scanner (0, zero_argc_, 0, sp)," << endl << " option_ (option)," << endl << " options_ (&option_info_)," << endl << " options_count_ (1)," << endl @@ -345,8 +365,9 @@ generate_runtime_inline (context& ctx) << "char** argv," << endl << "const option_info* options," << endl << "std::size_t options_count," << endl - << "bool erase)" << endl - << ": argv_scanner (argc, argv, erase)," << endl + << "bool erase," << endl + << "std::size_t sp)" << endl + << ": argv_scanner (argc, argv, erase, sp)," << endl << " options_ (options)," << endl << " options_count_ (options_count)," << endl << " i_ (1)"; @@ -362,8 +383,9 @@ generate_runtime_inline (context& ctx) << "char** argv," << endl << "const option_info* options," << endl << "std::size_t options_count," << endl - << "bool erase)" << endl - << ": argv_scanner (start, argc, argv, erase)," << endl + << "bool erase," << endl + << "std::size_t sp)" << endl + << ": argv_scanner (start, argc, argv, erase, sp)," << endl << " options_ (options)," << endl << " options_count_ (options_count)," << endl << " i_ (1)"; @@ -376,8 +398,9 @@ generate_runtime_inline (context& ctx) os << inl << "argv_file_scanner::" << endl << "argv_file_scanner (const std::string& file," << endl << "const option_info* options," << endl - << "std::size_t options_count)" << endl - << ": argv_scanner (0, zero_argc_, 0)," << endl + << "std::size_t options_count," << endl + << "std::size_t sp)" << endl + << ": argv_scanner (0, zero_argc_, 0, sp)," << endl << " options_ (options)," << endl << " options_count_ (options_count)," << endl << " i_ (1)"; -- cgit v1.1