diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | args.hxx | 32 | ||||
-rw-r--r-- | test.cxx | 104 |
3 files changed, 130 insertions, 7 deletions
@@ -29,6 +29,7 @@ # test and temp files test* +!test.cxx *.pyc *.swp *.swo @@ -10,9 +10,22 @@ #include <string> #include <tuple> #include <vector> +#include <cstdlib> namespace args { + size_t Glyphs(const std::string &string) + { + size_t length = 0; + for (const char c: string) + { + if ((c & 0xc0) != 0x80) + { + ++length; + } + } + return length; + } // Wrap a string into a vector of string lines std::vector<std::string> Wrap(const std::string &in, const size_t width, size_t firstlinewidth = 0) { @@ -37,26 +50,31 @@ namespace args std::istringstream stream(in); std::vector<std::string> output; std::ostringstream line; + size_t linesize = 0; while (stream) { std::string item; stream >> item; - if ((size_t(line.tellp()) + 1 + item.size()) > currentwidth) + size_t itemsize = Glyphs(item); + if ((linesize + 1 + itemsize) > currentwidth) { - if (line.tellp() > 0) + if (linesize > 0) { output.push_back(line.str()); line.str(std::string()); + linesize = 0; currentwidth = width; } } - if (line.tellp() > 0) + if (linesize) { + ++linesize; line << " "; } line << item; + linesize += itemsize; } - if (line.tellp() > 0) + if (linesize > 0) { output.push_back(line.str()); } @@ -77,7 +95,6 @@ namespace args break; default: - string[i] = std::toupper(string[i], loc); break; } } @@ -648,12 +665,13 @@ namespace args const std::vector<std::string> info(Wrap(std::get<1>(description), width - helpindent)); help << std::string(flagindent, ' ') << flags; auto infoit = std::begin(info); - if ((flagindent + flags.size() + gutter) > helpindent) + const size_t flagssize = Glyphs(flags); + if ((flagindent + flagssize + gutter) > helpindent) { help << '\n'; } else if (infoit != std::end(info)) { - help << std::string(helpindent - (flagindent + flags.size()), ' ') << *infoit << '\n'; + help << std::string(helpindent - (flagindent + flagssize), ' ') << *infoit << '\n'; ++infoit; } for (; infoit != std::end(info); ++infoit) diff --git a/test.cxx b/test.cxx new file mode 100644 index 0000000..43b2b8f --- /dev/null +++ b/test.cxx @@ -0,0 +1,104 @@ +/* Copyright © 2016 Taylor C. Richberger <taywee@gmx.com> + * This code is released under the license described in the LICENSE file + */ + +#include <iostream> +#include <list> +#include <clocale> + +#include <args.hxx> + +int main(int argc, char **argv) +{ + setlocale(LC_ALL, ""); + args::ArgumentParser parser("This is a test program. It has a lot of really cool things about it, and it also has a really really long description line that is going to necessitate some line breaks.", "As you saw above, this previously mentioned program has a lot of long things, and therefore necessitates some line breaking"); + args::Group mutexgroup(parser, "Group test", args::Group::Validators::AllOrNone); + args::Flag aflag(mutexgroup, "a", "This is flag a", args::Matcher({'a'})); + args::Flag bflag(mutexgroup, "b", "This is flag b", args::Matcher({'b'})); + args::Flag cflag(mutexgroup, "c", "This is flag c", args::Matcher({'c'})); + args::HelpFlag help(parser, "help", "Display this help menu", args::Matcher({'h'}, {"help"})); + args::Flag fooflag(parser, "foo", "This is a foo flag", args::Matcher({'f', 'F'}, {"Foo", "foo"})); + args::ArgFlag<std::string> bararg(parser, "bar", "This is a bar flag", args::Matcher({'b', 'B'}, {"Bar", "bar"})); + args::ArgFlag<double> baz(parser, "baz", "This is a baz flag", args::Matcher({'z', 'Z'}, {"Baz", "baz"})); + args::ArgFlagList<double> list(parser, "flaglist", "This is a list flag", args::Matcher({'l'}, {"list"})); + args::PosArg<double> pos(parser, "Position", "This is a position arg"); + args::PosArg<double> pos2(parser, "Position", "This is a position arg"); + args::PosArg<double> pos3(parser, "Position", "This is a position arg"); + args::PosArg<double> pos4(parser, "Pösitiön", "This is a position arg"); + args::PosArg<double> pos5(parser, "Position", "This is a position arg"); + args::PosArg<double> pos6(parser, "Position", "This is a position arg"); + args::PosArgList<double> poslist1(parser, "Position list", "This is a position list arg"); + args::PosArgList<double> poslist2(parser, "Position list", "This is a position list arg"); + args::PosArgList<double> poslist3(parser, "Position list", "This is a position list arg"); + args::PosArg<double> pos7(parser, "Position", "This is a position arg"); + args::PosArg<double> pos8(parser, "Position", "This is a position arg"); + args::PosArgList<double> poslist4(parser, "Position list", "This is a position list arg"); + args::PosArg<double> pos9(parser, "Position", "This is a position arg"); + args::PosArgList<double> poslist(parser, "Position list", "This is a position list arg"); + args::Counter counter(parser, "counter", "This is a counter flag", args::Matcher({'c'})); + try + { + parser.ParseCLI(argc, argv); + } + catch (args::Help) + { + std::cout << parser.Help(); + return 0; + } + catch (args::ParseError e) + { + std::cerr << e.what() << std::endl; + std::cerr << parser.Help(); + return 1; + } + if (aflag) + { + std::cout << "A flag found" << std::endl; + } + if (bflag) + { + std::cout << "B flag found" << std::endl; + } + if (cflag) + { + std::cout << "C flag found" << std::endl; + } + if (fooflag) + { + std::cout << "Foo flag found" << std::endl; + } + if (bararg) + { + std::cout << "Bar arg found: " << bararg.Value() << std::endl; + } + if (baz) + { + std::cout << "Baz arg found: " << baz.Value() << std::endl; + } + if (counter) + { + std::cout << "counter found: " << counter.Count() << std::endl; + } + if (list) + { + std::cout << "list found: " << std::endl; + for (const auto &item: list.Values()) + { + std::cout << "- " << item << std::endl; + } + } + if (pos) + { + std::cout << "pos found: " << pos.Value() << std::endl; + } + if (poslist) + { + std::cout << "poslist found: " << std::endl; + for (const auto &item: poslist.Values()) + { + std::cout << "- " << item << std::endl; + } + } + + return 0; +} |