فهرست منبع

chore: do a little re-arrangement

Sam Jaffe 2 سال پیش
والد
کامیت
11479d1552
3فایلهای تغییر یافته به همراه54 افزوده شده و 49 حذف شده
  1. 38 38
      README.md
  2. 15 10
      include/program_args/arguments.h
  3. 1 1
      include/program_args/arguments_impl.hpp

+ 38 - 38
README.md

@@ -5,7 +5,6 @@ A tool for the processing of command-line arguments to a C++ program using a flu
 
 ## Usage
 
-
 ### Arguments
 Positional arguments are provided with their zero-based indices. Arguments can be declared as optional, but you cannot include required arguments with a higher index than any optional positional arg.
 
@@ -17,7 +16,44 @@ In either case, options with an arity greater than one are not allowed.
 
 Abbreviated options support key-value concatenation, such as how you can do `-I/path/to/my/include/dir` in gcc/clang.
 
-#### Snippets
+Singular option storage cannot be repeated
+```c++
+std::string directory = option("logdir");
+
+$ ./a.out --logdir /var/log --logdir /usr/local/var/log
+Error in program argument handling: Repeated option not allowed for argument logdir
+```
+
+Abbreviations are not automatically generated
+```c++
+std::string directory = option("logdir");
+
+$ ./a.out -l /var/log
+Error in program argument handling: Unknown argument provided: -l
+```
+
+Pairs/Tuples don't get to use increased arity
+```c++
+std::pair<int, int> bounds = options("bounds");
+std::cout << args() << std::endl;
+
+$ ./a.out --bounds 1920 1080
+[ "1080" ]
+```
+
+### Flags
+Flags are a sub-class of options who do not have a follow-on value, but instead change the state of the given object through being called.
+
+Flags are supported for the types of `bool` and `int` only.  
+With boolean flags, in addition to the `--name` argument created and the abbreviated form, a `--no-name` argument will be registered that sets the option value to false (in case the default is set to true).  
+Integer flags cannot have default values, and do not have inverted forms. Instead, it is possible to repeat an integer flag endlessly, incrementing it by one with each appearance.
+
+Abbreviated flags have the additional feature that they can be concatenated, and integer flags can be number-repeated.  
+For example, suppose that both `-v` and `-k` are valid flags, representing `int verbosity` and `bool dry_run` respectively. Then the following are all valid input tokens: `-vk`, `-vv`, `-vvvk`, and `-v5`. The argument `-v5k` will still generate a parse error, however.
+
+## Examples
+
+### Git
 Let's take a look at a partial example of how we might design the interfact for git using this tool.
 
 For conveniece sake, we'll start with a helper object.
@@ -100,39 +136,3 @@ TYPED_MAIN(Git const &git, Pull const &pull) {
 
 PROGRAM_ARGS_MAIN(Git)
 ```
-
-Singular option storage cannot be repeated
-```c++
-std::string directory = option("logdir");
-
-$ ./a.out --logdir /var/log --logdir /usr/local/var/log
-Error in program argument handling: Repeated option not allowed for argument logdir
-```
-
-Abbreviations are not automatically generated
-```c++
-std::string directory = option("logdir");
-
-$ ./a.out -l /var/log
-Error in program argument handling: Unknown argument provided: -l
-```
-
-Pairs/Tuples don't get to use increased arity
-```c++
-std::pair<int, int> bounds = options("bounds");
-std::cout << args() << std::endl;
-
-$ ./a.out --bounds 1920 1080
-[ "1080" ]
-```
-
-### Flags
-Flags are a sub-class of options who do not have a follow-on value, but instead change the state of the given object through being called.
-
-Flags are supported for the types of `bool` and `int` only.  
-With boolean flags, in addition to the `--name` argument created and the abbreviated form, a `--no-name` argument will be registered that sets the option value to false (in case the default is set to true).  
-Integer flags cannot have default values, and do not have inverted forms. Instead, it is possible to repeat an integer flag endlessly, incrementing it by one with each appearance.
-
-Abbreviated flags have the additional feature that they can be concatenated, and integer flags can be number-repeated.  
-For example, suppose that both `-v` and `-k` are valid flags, representing `int verbosity` and `bool dry_run` respectively. Then the following are all valid input tokens: `-vk`, `-vv`, `-vvvk`, and `-v5`. The argument `-v5k` will still generate a parse error, however.
-

+ 15 - 10
include/program_args/arguments.h

@@ -40,7 +40,8 @@ public:
       : Arguments(argc, argv, (void const *)nullptr) {}
 
   std::vector<std::string> args() const {
-    return {arguments.begin() + argument_names.size(), arguments.end()};
+    size_t const offset = std::min(arguments.size(), argument_names.size());
+    return {arguments.begin() + offset, arguments.end()};
   }
 
 protected:
@@ -175,25 +176,29 @@ private:
   using make_action_t = std::function<std::shared_ptr<void>(Impl const &)>;
   using action_hook_t =
       std::function<make_action_t(size_t, char const * const *)>;
+  constexpr static size_t const no_optional_args{~0ul};
+
   // Metadata variables
   bool primed_{false};
   detail::Any parent_;
   std::string rest_name;
-  std::map<option_id, std::string> argument_descriptions;
+
+  std::map<std::string, std::string> argument_descriptions;
   std::map<size_t, option_id> argument_names;
-  std::map<option_id, std::string> option_descriptions;
-  std::map<std::string, option_id> option_names;
-  std::set<option_id> flag_names;
-  std::map<std::string, action_hook_t> actions;
-  std::string action_name_{""};
-  make_action_t make_action_{nullptr};
+  size_t optional_from{no_optional_args};
+
   std::map<std::string, std::string> action_descriptions;
+  std::map<std::string, action_hook_t> actions;
+
+  std::map<std::string, std::string> option_descriptions;
+  std::map<std::string, option_id> option_names;
+  std::set<std::string> flag_names;
 
   // Data/Output variables
   std::string program;
-  constexpr static size_t const no_optional_args{~0ul};
-  size_t optional_from{no_optional_args};
   std::vector<std::string> arguments;
+  std::string action_name_{""};
+  make_action_t make_action_{nullptr};
   std::map<std::string, std::vector<std::string>> options;
   std::map<std::string, int> flags;
 };

+ 1 - 1
include/program_args/arguments_impl.hpp

@@ -171,7 +171,7 @@ auto Arguments<Impl>::Rest::operator=(T && value) {
 
 template <typename Impl>
 Arguments<Impl>::Rest::operator std::vector<std::string>() const {
-  return (*this) ? self->args() : std::vector<std::string>();
+  return self->args();
 }
 
 template <typename Impl> Arguments<Impl>::Rest::operator bool() const {