3
3
This simple header-only library allows you to easily define, parse and examine
4
4
program options and positional parameters in a C++ program. :notes :
5
5
6
- Here is a super simple example of how to use ** pgm::args** :
6
+ Here is a very simple example of how to use ** pgm::args** :
7
7
8
8
``` cpp
9
9
// example1.cpp
@@ -23,7 +23,7 @@ int main(int argc, char* argv[])
23
23
24
24
if (args["--version"])
25
25
{
26
- std::cout << argv[0] << " 0.42" << std::endl;
26
+ std::cout << argv[0] << " version " << 0.42 << std::endl;
27
27
return 0;
28
28
}
29
29
if (args["--help"])
@@ -52,11 +52,39 @@ Options:
52
52
-h, --help Show this help screen and exit.
53
53
54
54
user@linux:~$ ./example1 -v
55
- ./example1 0.42
55
+ ./example1 version 0.42
56
56
57
57
user@linux:~$
58
58
```
59
59
60
+ ## Installing pgm::args
61
+
62
+ You can install ** ` pgm::args ` ** in one of the following ways:
63
+
64
+ 1 . Install binary package, if you are on Debian/Ubuntu/etc:
65
+
66
+ ``` console
67
+ $ v=0.2 p=libpgm-args-dev_${v}_all.deb
68
+ $ wget https://github.com/dimitry-ishenko-cpp/pgm-args/releases/download/v${v}/${p}
69
+ $ sudo apt install ./${p}
70
+ ```
71
+
72
+ 2 . Install from source:
73
+
74
+ ``` console
75
+ $ mkdir build
76
+ $ cd build
77
+ $ cmake ..
78
+ $ make all test
79
+ $ sudo make install
80
+ ```
81
+
82
+ 3 . Add as a sub-module to your project:
83
+
84
+ ``` console
85
+ $ git submodule add https://github.com/dimitry-ishenko-cpp/pgm-args.git pgm
86
+ ```
87
+
60
88
## Using pgm::args
61
89
62
90
### :zero : Understand the Nomenclature
@@ -71,7 +99,7 @@ user@linux:~$ foo bar baz
71
99
` foo ` is the program, and ` bar ` and ` baz ` are the _ arguments_ . Following rules
72
100
apply to program arguments:
73
101
74
- :key : Arguments beginning with the hyphen ( ` - ` ) delimiter are called _ options_ .
102
+ :key : Arguments beginning with the hyphen delimiter ` - ` are called _ options_ .
75
103
76
104
:key : There are two kinds of options:
77
105
@@ -113,7 +141,7 @@ foo --output=path
113
141
foo --output path
114
142
```
115
143
116
- _ NOTE: _ the equal sign ( ` = ` ) delimiter between the option and its value.
144
+ Note the equal sign ` = ` delimiter between the option and its value.
117
145
118
146
:key : Options typically precede other non-option arguments, which are called
119
147
_ positional parameters_ .
@@ -127,10 +155,10 @@ foo -a --bar baz -- -c --qux
127
155
```
128
156
129
157
` -a ` and ` --bar ` are treated as options, and ` baz ` , ` -c ` and ` --qux ` are treated
130
- as positional parameters (unless ` --bar ` requires a value, in which case ` baz `
131
- will be treated as an option value).
158
+ as positional parameters (unless option ` --bar ` requires a value, in which case
159
+ ` baz ` will be treated as an option value).
132
160
133
- :key : A token consisting of a single hyphen ( ` - ` ) is treated as an ordinary
161
+ :key : A token consisting of a single hyphen ` - ` is treated as an ordinary
134
162
non-option argument.
135
163
136
164
---
@@ -172,9 +200,9 @@ followed by one
172
200
character, eg:
173
201
174
202
```cpp
175
- pgm::arg{ "-a", "..." }; // (1)
176
- pgm::arg{ "-b", "..." }; // (1)
177
- pgm::arg{ "-c", "..." }; // (1)
203
+ pgm::arg{ "-a", "..." }; // (1)
204
+ pgm::arg{ "-b", "..." }; // (1)
205
+ pgm::arg{ "-c", "..." }; // (1)
178
206
```
179
207
180
208
:rose : ** ` long_name ` ** is a long option name consisting of two hyphens followed
@@ -194,22 +222,22 @@ pgm::arg{ "-v", "--version", "..." }; // (4)
194
222
[graphic](https://en.cppreference.com/w/cpp/string/byte/isgraph) characters, eg:
195
223
196
224
```cpp
197
- pgm::arg{ "source", "..." }; // (3)
198
- pgm::arg{ "x+y", "..." }; // (3)
199
- pgm::arg{ "foo/bar/baz", "..." }; // (3)
225
+ pgm::arg{ "source", "..." }; // (3)
226
+ pgm::arg{ "x+y", "..." }; // (3)
227
+ pgm::arg{ "foo/bar/baz", "..." }; // (3)
200
228
```
201
229
202
230
:rose : ** ` value_name ` ** is an option value name that can contain any
203
231
[ graphic] ( https://en.cppreference.com/w/cpp/string/byte/isgraph ) characters, eg:
204
232
205
233
``` cpp
206
- pgm::arg{ "-i", "file-name", "..." }; // (5)
207
- pgm::arg{ "-l", "log-file", "..." }; // (5)
234
+ pgm::arg{ "-i", "file-name", "..." }; // (5)
235
+ pgm::arg{ "-l", "log-file", "..." }; // (5)
208
236
209
- pgm::arg{ "--filter", "name", "..." }; // (6)
210
- pgm::arg{ "--set-time", "HH: MM ", "..." }; // (6)
237
+ pgm::arg{ "--filter", "name", "..." }; // (6)
238
+ pgm::arg{ "--set-time", "HH: MM ", "..." }; // (6)
211
239
212
- pgm::arg{ "-w", "--wait", "time", "..." }; // (7)
240
+ pgm::arg{ "-w", "--wait", "time", "..." }; // (7)
213
241
pgm::arg{ "-d", "--debug", "level", "..." }; // (7)
214
242
```
215
243
@@ -225,7 +253,7 @@ pgm::arg{ "foo/bar/baz", "Lorem ipsum dolor sit amet." }; // (3)
225
253
```
226
254
227
255
:rose : ** ` spec ` ** is an option/param specification consisting of one or more of
228
- the following flags combined using the vertical pipe ( ` | ` ) delimiter:
256
+ the following flags combined using the vertical pipe ` | ` delimiter:
229
257
230
258
| flag | option | param | meaning |
231
259
| :-----------------:| :------------------:| :------------------:| :--------|
@@ -354,7 +382,7 @@ The subscript `operator[]` returns const ref to an instance of
354
382
```
355
383
356
384
` operator bool() ` of ** ` pgm::argval ` ** is marked as explicit, so you may have
357
- to use ` static_cast ` or double logical negation ( ` !! ` ) to force boolean
385
+ to use ` static_cast ` or double logical negation ` !! ` to force boolean
358
386
context in certain situations, eg:
359
387
360
388
``` cpp
@@ -369,8 +397,8 @@ The subscript `operator[]` returns const ref to an instance of
369
397
370
398
``` cpp
371
399
std::exception_ptr ep;
372
- try { args.parse(argc, argv); }
373
- catch(...){ ep = std::current_exception(); }
400
+ try { args.parse(argc, argv); }
401
+ catch (...) { ep = std::current_exception(); }
374
402
375
403
if (args[ "--help"] ) show_usage();
376
404
else if (ep) std::rethrow_exception(ep);
@@ -467,6 +495,8 @@ Here is a more complete example of using **pgm::args**:
467
495
void show_usage(const pgm::args& args, std::string_view name);
468
496
void show_version(std::string_view name);
469
497
498
+ void transfer(std::string_view source, std::string_view dest);
499
+
470
500
int main(int argc, char* argv[])
471
501
try
472
502
{
492
522
};
493
523
494
524
std::exception_ptr ep;
495
- try{ args.parse(argc, argv); }
496
- catch(...){ ep = std::current_exception(); }
525
+ try { args.parse(argc, argv); }
526
+ catch (...) { ep = std::current_exception(); }
497
527
498
528
if (args["--help"])
499
529
show_usage(args, name);
529
559
auto dest = args["DEST"].value();
530
560
531
561
// "transfer" files
532
- for(auto const& source : sources)
562
+ for (auto const& source : sources)
533
563
{
534
- if(!quiet) std::cout << "Sending " << source << " to " << dest << std::endl;
564
+ if (!quiet) std::cout << "Sending " << source << " to " << dest << std::endl;
565
+ transfer(source, dest);
535
566
}
536
567
}
537
568
@@ -564,7 +595,12 @@ program, nothing will actually be transferred.)";
564
595
565
596
void show_version(std::string_view name)
566
597
{
567
- std::cout << name << " 0.42" << std::endl;
598
+ std::cout << name << " version " << 0.42 << std::endl;
599
+ }
600
+
601
+ void transfer(std::string_view source, std::string_view dest)
602
+ {
603
+ //
568
604
}
569
605
```
570
606
0 commit comments