-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.cpp
107 lines (85 loc) · 2.81 KB
/
options.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "options/optionparser.h"
using namespace option;
enum optionIndex {
OPT_HELP, OPT_DEBUG, OPT_VERSION, OPT_NUM, OPT_STRING, OPT_MULTIARGS,
OPT_REQ, OPT_SINGLE
};
const option::Descriptor usage[] =
{
{ 0, 0, 0, option::Arg::Dummy, "USAGE: example [options]\n\n Options:" },
{ OPT_HELP, "h", "help", option::Arg::None, " --help Print usage and exit." },
{ OPT_DEBUG, "d", "debug", option::Arg::None, " --debug, -s Set optional debug mode." },
{ OPT_VERSION, "v", "version", option::Arg::None, " --version, -v Print version." },
{ OPT_NUM, "n", "number", option::Arg::Numeric, " --number, -n Set a number (must be numeric)." },
{ OPT_STRING, "s", "string", option::Arg::String, " --string, -s Set a string (any string)." },
{ OPT_MULTIARGS, "m", "multi", option::Arg::String | option::Arg::Multiple,
" --multi, -m Add string to options." },
{ 0, 0, 0, option::Arg::Dummy, "------- Separator ------------------------------" },
{ OPT_REQ, "r", "required", option::Arg::String | option::Arg::Required,
" --required, -r Set required option." },
{ OPT_SINGLE, "o", "option", option::Arg::None, " --flag, -f Enable this option"},
{ 0, 0, 0, option::Arg::Dummy,"\nExamples:\n"
" example -h \n"
" example -r \"musthave\" -d -v -n 123 -s \"hello world\" -m=test -m=\"test 2\" -m \"test 3\"\n"
" example \n" },
{ 0, 0, 0, option::Arg::End, 0 } // terminating
};
int main(int argc, const char** argv)
{
option::Options opts((option::Descriptor*)usage);
if (!opts.Parse(&argv[1], argc)) // skip first argument (module path)
{
#ifdef _WIN32
OutputDebugStringA(opts.error_msg());
OutputDebugStringA(opts.cstr());
#endif
printf("* option error: %s\n", opts.error_msg());
opts.print();
return -1;
}
// optionally load commandline overrides from file
// opts.ParseFile("test.cmdline");
if (opts[OPT_DEBUG])
{
printf("debug enabled\n");
}
if (opts[OPT_VERSION])
{
printf("options version 0.99a\n");
}
if (opts[OPT_NUM])
{
int num = 0;
opts.GetArgument(OPT_NUM,num);
printf("input number: %d\n", num);
}
if (opts[OPT_STRING])
{
printf("input string: %s\n", opts.GetValue(OPT_STRING));
}
if (opts[OPT_MULTIARGS])
{
auto arg = opts.GetArgument(OPT_MULTIARGS);
printf("input multi args:\n");
while (arg != nullptr)
{
printf("\t%s\n",arg->value);
arg = arg->pNext;
}
}
if (opts[OPT_REQ])
{
printf("required input: %s", opts.GetValue(OPT_REQ));
}
printf("\n");
}
#ifdef _WIN32
#include <Windows.h>
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPWSTR lpCmdLine, int /*nCmdShow*/)
{
int argc = 0;
char** argv = option::CommandLineToArgvWin(nullptr, &argc);
main(argc, (const char**)(argv)); // skipp first argument
option::free(argv);
}
#endif