-
Notifications
You must be signed in to change notification settings - Fork 36
/
demo.cpp
133 lines (115 loc) · 3.63 KB
/
demo.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Using AnyOption to parse command line arguments and an options file
*
* Compile:
* $ g++ demo.cpp anyoption.cpp -o demo
*
* Test Command Line Options:
*
* $ ./ demo
* usage:
*
* -h --help Prints this help
* -s --size <size> Image Size
* -z --zip Compress Image
* -c Convert Image
* --name <image_name> Image Name
*
* $ ./demo -c --zip -s 42 --name foo.jpg
* size = 42
* name = foo.jpg
* c = flag set
* zip = flag set
*
* Test options from a resource file:
*
* $ cat options.txt
* # sample options file
* c
* zip
* size : 42
* name : foo.jpg
* title : FOO
*
* $ ./demo -c
* size = 42
* name = foo.jpg
* title = FOO
* c = flag set
*
*/
#include "anyoption.h"
void example(int argc, char *argv[]);
int main(int argc, char *argv[]) {
example(argc, argv);
return 0;
}
void example(int argc, char *argv[]) {
/* 1. CREATE AN OBJECT */
AnyOption *opt = new AnyOption();
/* 2. SET PREFERENCES */
// opt->noPOSIX(); /* do not check for POSIX style character options */
// opt->setVerbose(); /* print warnings about unknown options */
// opt->autoUsagePrint(true); /* print usage for bad options */
/* 3. SET THE USAGE/HELP */
opt->addUsage("usage: ");
opt->addUsage("");
opt->addUsage(" -h --help Prints this help ");
opt->addUsage(" -s --size <size> Image Size ");
opt->addUsage(" -z --zip Compress Image ");
opt->addUsage(" -c Convert Image ");
opt->addUsage(" --name <image_name> Image Name ");
opt->addUsage("");
/* 4. SET THE OPTION STRINGS/CHARACTERS */
/* by default all options will be checked on the command line and from
* option/resource file */
opt->setFlag(
"help",
'h'); /* a flag (takes no argument), supporting long and short form */
opt->setOption(
"size",
's'); /* an option (takes an argument), supporting long and short form */
opt->setOption(
"name"); /* an option (takes an argument), supporting only long form */
opt->setFlag(
'c'); /* a flag (takes no argument), supporting only short form */
/* for options that will be checked only on the command and line not in
* option/resource file */
opt->setCommandFlag(
"zip",
'z'); /* a flag (takes no argument), supporting long and short form */
/* for options that will be checked only from the option/resource file */
opt->setFileOption(
"title"); /* an option (takes an argument), supporting only long form */
/* 5. PROCESS THE COMMANDLINE AND RESOURCE FILE */
/* read options from a option/resource file with ':' separated options or
* flags, one per line */
opt->processFile("./options.txt");
/* go through the command line and get the options */
opt->processCommandArgs(argc, argv);
if (!opt->hasOptions()) { /* print usage if no options */
opt->printUsage();
delete opt;
return;
}
/* 6. GET THE VALUES */
if (opt->getFlag("help") || opt->getFlag('h'))
opt->printUsage();
if (opt->getValue('s') != NULL || opt->getValue("size") != NULL)
cout << "size = " << opt->getValue('s') << endl;
if (opt->getValue("name") != NULL)
cout << "name = " << opt->getValue("name") << endl;
if (opt->getValue("title") != NULL)
cout << "title = " << opt->getValue("title") << endl;
if (opt->getFlag('c'))
cout << "c = flag set " << endl;
if (opt->getFlag('z') || opt->getFlag("zip"))
cout << "zip = flag set " << endl;
cout << endl;
/* 7. GET THE ACTUAL ARGUMENTS AFTER THE OPTIONS */
for (int i = 0; i < opt->getArgc(); i++) {
cout << "arg = " << opt->getArgv(i) << endl;
}
/* 8. DONE */
delete opt;
}