diff options
author | Taylor C. Richberger <Taywee@gmx.com> | 2016-05-08 21:43:42 -0600 |
---|---|---|
committer | Taylor C. Richberger <Taywee@gmx.com> | 2016-05-08 21:43:42 -0600 |
commit | 980488f117364b76a7a1f14ed93a4aa750c72ae0 (patch) | |
tree | 2901fea02c9634228cae2bde51f899d18ccf9bf9 /args.hxx | |
parent | small improvement (diff) | |
parent | improve documentation (diff) | |
download | args.hxx-980488f117364b76a7a1f14ed93a4aa750c72ae0.tar.xz |
Merge branch 'master' of ssh.gitgud.io:Taywee/args
Diffstat (limited to 'args.hxx')
-rw-r--r-- | args.hxx | 69 |
1 files changed, 53 insertions, 16 deletions
@@ -151,6 +151,50 @@ namespace args } }; + /** A simple unified option type for unified initializer lists + */ + struct EitherOpt + { + 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) + { + std::unordered_set<std::string> longOpts; + for (const EitherOpt &opt: opts) + { + if (!opt.isShort) + { + longOpts.insert(opt.longOpt); + } + } + return longOpts; + } + + /** Get just the short options from an initializer list of EitherOpts + */ + static std::unordered_set<char> GetShort(std::initializer_list<EitherOpt> opts) + { + std::unordered_set<char> shortOpts; + for (const EitherOpt &opt: opts) + { + if (opt.isShort) + { + shortOpts.insert(opt.shortOpt); + } + } + return shortOpts; + } + }; + + + /** A class of "matchers", specifying short and long options that can possibly be matched * * This is supposed to be constructed and then passed in, not used directly from user code. @@ -163,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) : @@ -171,29 +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(const std::initializer_list<char> &shortIn, const std::initializer_list<std::string> &longIn) : - shortOpts(std::begin(shortIn), std::end(shortIn)), longOpts(std::begin(longIn), std::end(longIn)) - {} - - /** Specify short opts only as initializer lists - */ - Matcher(const std::initializer_list<char> &shortIn) : - shortOpts(std::begin(shortIn), std::end(shortIn)) - {} - - /** Specify long opts only as initializer lists + /** 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(const std::initializer_list<std::string> &longIn) : - longOpts(std::begin(longIn), std::end(longIn)) - {} + Matcher(std::initializer_list<EitherOpt> in) : + shortOpts(EitherOpt::GetShort(in)), longOpts(EitherOpt::GetLong(in)) {} Matcher(Matcher &&other) : shortOpts(std::move(other.shortOpts)), longOpts(std::move(other.longOpts)) {} |