aboutsummaryrefslogtreecommitdiff
path: root/args.hxx
diff options
context:
space:
mode:
authorTaylor C. Richberger <tcr@absolute-performance.com>2016-05-07 12:23:58 -0600
committerTaylor C. Richberger <tcr@absolute-performance.com>2016-05-07 12:23:58 -0600
commit5ebc21cc2f6c762073317d5e18e944b778908728 (patch)
treee32527a324140ef3b64c438a0d8a680833009887 /args.hxx
parentfix ADL. Add unified initializer list (diff)
downloadargs.hxx-5ebc21cc2f6c762073317d5e18e944b778908728.tar.xz
Improve use and documentation
Diffstat (limited to 'args.hxx')
-rw-r--r--args.hxx71
1 files changed, 39 insertions, 32 deletions
diff --git a/args.hxx b/args.hxx
index f079f29..e388aa6 100644
--- a/args.hxx
+++ b/args.hxx
@@ -151,41 +151,49 @@ namespace args
}
};
+ /** A simple unified option type for unified initializer lists
+ */
struct EitherOpt
{
- bool isShort;
- char shortOpt;
- std::string longOpt;
- EitherOpt(const std::string &opt) : isShort(false), longOpt(opt) {}
- EitherOpt(const char *opt) : isShort(false), longOpt(opt) {}
- EitherOpt(const char opt) : isShort(true), shortOpt(opt) {}
- };
-
- std::unordered_set<std::string> GetLong(std::initializer_list<EitherOpt> opts)
- {
- std::unordered_set<std::string> longOpts;
- for (const EitherOpt &opt: opts)
+ const bool isShort;
+ const char shortOpt;
+ const std::string longOpt;
+ EitherOpt(const std::string &opt) : isShort(false), shortOpt(), longOpt(opt) {}
+ EitherOpt(const char *opt) : isShort(false), shortOpt(), longOpt(opt) {}
+ EitherOpt(const char opt) : isShort(true), shortOpt(opt), longOpt() {}
+
+ /** Get just the long options from an initializer list of EitherOpts
+ */
+ static std::unordered_set<std::string> GetLong(std::initializer_list<EitherOpt> opts)
{
- if (!opt.isShort)
+ std::unordered_set<std::string> longOpts;
+ for (const EitherOpt &opt: opts)
{
- longOpts.insert(opt.longOpt);
+ if (!opt.isShort)
+ {
+ longOpts.insert(opt.longOpt);
+ }
}
+ return longOpts;
}
- return longOpts;
- }
- std::unordered_set<char> GetShort(std::initializer_list<EitherOpt> opts)
- {
- std::unordered_set<char> shortOpts;
- for (const EitherOpt &opt: opts)
+ /** Get just the short options from an initializer list of EitherOpts
+ */
+ static std::unordered_set<char> GetShort(std::initializer_list<EitherOpt> opts)
{
- if (opt.isShort)
+ std::unordered_set<char> shortOpts;
+ for (const EitherOpt &opt: opts)
{
- shortOpts.insert(opt.shortOpt);
+ if (opt.isShort)
+ {
+ shortOpts.insert(opt.shortOpt);
+ }
}
+ return shortOpts;
}
- return shortOpts;
- }
+ };
+
+
/** A class of "matchers", specifying short and long options that can possibly be matched
*
@@ -199,6 +207,8 @@ namespace args
public:
/** Specify short and long opts separately as iterators
+ *
+ * ex: `args::Matcher(shortOpts.begin(), shortOpts.end(), longOpts.begin(), longOpts.end())`
*/
template <typename ShortIt, typename LongIt>
Matcher(ShortIt shortOptsStart, ShortIt shortOptsEnd, LongIt longOptsStart, LongIt longOptsEnd) :
@@ -207,23 +217,20 @@ namespace args
{}
/** Specify short and long opts separately as iterables
+ *
+ * ex: `args::Matcher(shortOpts, longOpts)`
*/
template <typename Short, typename Long>
Matcher(Short &&shortIn, Long &&longIn) :
shortOpts(std::begin(shortIn), std::end(shortIn)), longOpts(std::begin(longIn), std::end(longIn))
{}
- /** Specify short and long opts as initializer lists
- */
- Matcher(std::initializer_list<char> shortIn, std::initializer_list<std::string> longIn) :
- shortOpts(std::begin(shortIn), std::end(shortIn)), longOpts(std::begin(longIn), std::end(longIn))
- {}
-
/** Specify a mixed single initializer-list of both short and long opts
+ *
+ * This is the fancy one: `args::Matcher{'a'}`, or `args::Matcher{"foo"}`, or `args::Matcher{"foo", 'f', 'F', "FoO"}`
*/
Matcher(std::initializer_list<EitherOpt> in) :
- shortOpts(GetShort(in)), longOpts(GetLong(in))
- {}
+ shortOpts(EitherOpt::GetShort(in)), longOpts(EitherOpt::GetLong(in)) {}
Matcher(Matcher &&other) : shortOpts(std::move(other.shortOpts)), longOpts(std::move(other.longOpts))
{}