-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample_subcommands.cc
More file actions
84 lines (68 loc) · 2.59 KB
/
Copy pathExample_subcommands.cc
File metadata and controls
84 lines (68 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#define CL_WINDOWS_CONSOLE_COLORS 1
#define CL_ANSI_CONSOLE_COLORS 1
#include "../src/Cmdline.h"
#include <cstdio>
// Example from
// https://github.com/muellan/clipp
enum class mode {make, find, help};
static std::vector<std::string> input;
static std::string dict;
static std::string out;
static bool split = false;
static bool progr = false;
using ArgIterator = char const* const*;
using ParseResult = cl::Cmdline::ParseResult<ArgIterator>;
static ParseResult Parse(cl::Cmdline& cli, ArgIterator next, ArgIterator last)
{
auto const res = cli.Parse(next, last);
cli.PrintDiag();
if (!res) {
cli.PrintHelp();
}
return res;
}
static ParseResult ParseMakeCommand(ArgIterator next, ArgIterator last)
{
cl::Cmdline cli("finder make", "Make a new finder");
cli.Add("wordfile", "", cl::Positional::yes | cl::Arg::required | cl::Required::yes | cl::Multiple::yes, cl::Var(input));
cli.Add("dict", "", cl::Arg::required | cl::Required::yes, cl::Var(dict));
cli.Add("progress", "show progress", {}, cl::Var(progr));
return Parse(cli, next, last);
}
static ParseResult ParseFindCommand(ArgIterator next, ArgIterator last)
{
cl::Cmdline cli("finder find", "Find an existing finder");
cli.Add("infile", "", cl::Required::yes | cl::Multiple::yes | cl::Arg::required | cl::Positional::yes, cl::Var(input));
cli.Add("dict", "", cl::Arg::required | cl::Required::yes, cl::Var(dict));
cli.Add("o", "write to file instead of stdout", cl::Arg::required, cl::Var(out));
cli.Add("split|nosplit", "(do not) split output", {}, cl::Flag(split, /*inverse_prefix*/ "no"));
return Parse(cli, next, last);
}
static bool ParseCommandLine(ArgIterator next, ArgIterator last)
{
using Command = ParseResult (*)(ArgIterator, ArgIterator);
Command mode = nullptr;
cl::Cmdline cli("finder", "");
cli.Add("v|version", "show version", cl::Arg::no, [](cl::ParseContext const& /*ctx*/) { printf("version 1.0\n"); });
cli.Add("mode",
"Can be make|find|help\n"
" <make> \tMake a new finder?!\n"
" <find> \tFind an existing finder.\n"
" <help> \tShow help menu",
cl::Required::yes | cl::Positional::yes | cl::StopParsing::yes,
cl::Map(mode, { {"make", ParseMakeCommand},
{"find", ParseFindCommand},
{"help", nullptr} }));
if (auto const res = Parse(cli, next, last))
{
if (mode)
mode(res.next, last);
else
cli.PrintHelp();
}
return false;
}
int main(int argc, char* argv[])
{
ParseCommandLine(argv + 1, argv + argc);
}