Skip to content

Commit e2a2247

Browse files
committed
Update README and examples
1 parent c3b8826 commit e2a2247

File tree

3 files changed

+141
-4
lines changed

3 files changed

+141
-4
lines changed

README.md

+127-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,130 @@
11
Args
22
====
33

4-
Simple and typesafe commandline parsing for C++14.
4+
Simple and typesafe commandline parsing for C++14.
5+
6+
Quickstart
7+
----------
8+
9+
Simply provide a class with fields that is to be filled by command-line arguments:
10+
11+
```cpp
12+
struct hello
13+
{
14+
int count;
15+
std::string name;
16+
17+
hello() : count(1)
18+
{}
19+
20+
template<class F>
21+
void parse(F f)
22+
{
23+
f(count, "--count", "-C", args::help("Number of greetings."));
24+
f(name, "--name", "-N", args::help("The person to greet."), args::required());
25+
}
26+
27+
void run()
28+
{
29+
for(int i=0;i<count;i++) printf("%s\n", name.c_str());
30+
}
31+
};
32+
33+
int main(int argc, char const *argv[]) {
34+
args::parse<hello>(argc, argv);
35+
}
36+
```
37+
38+
The command then could be run like this:
39+
40+
```
41+
$ hello --name Paul --count 3
42+
Paul
43+
Paul
44+
Paul
45+
46+
```
47+
48+
The args library will auto-generate help info:
49+
50+
```
51+
$ hello --help
52+
Usage: hello [options...]
53+
54+
Simple program that greets NAME for a total of COUNT times.
55+
56+
Options:
57+
58+
-h, --help Show help
59+
--count, -C [integer] Number of greetings.
60+
--name, -N [string] The person to greet.
61+
```
62+
63+
In addition, nested commands can be created:
64+
65+
```cpp
66+
struct cli : args::group<cli>
67+
{};
68+
69+
struct initdb : cli::command<initdb>
70+
{
71+
initdb() {}
72+
static const char* help()
73+
{
74+
return "Initialize database";
75+
}
76+
void run()
77+
{
78+
printf("Initialize database\n");
79+
}
80+
};
81+
82+
struct dropdb : cli::command<dropdb>
83+
{
84+
dropdb() {}
85+
static const char* help()
86+
{
87+
return "Delete database";
88+
}
89+
void run()
90+
{
91+
printf("Delete database\n");
92+
}
93+
};
94+
95+
int main(int argc, char const *argv[]) {
96+
args::parse<cli>(argc, argv);
97+
}
98+
99+
```
100+
101+
So each subcommand can be ran like this:
102+
103+
```
104+
$ cli initdb
105+
Initialize database
106+
```
107+
108+
```
109+
$ cli dropdb
110+
Delete database
111+
```
112+
113+
In addition help is generated for subcommands as well:
114+
115+
```
116+
$ cli --help
117+
Usage: cli [options...] [command]
118+
119+
Command-line interface to manage a database
120+
121+
Options:
122+
123+
-h, --help Show help
124+
125+
Commands:
126+
127+
dropdb Delete database
128+
initdb Initialize database
129+
130+
```

examples/basic.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
struct hello
44
{
5+
static const char* help()
6+
{
7+
return "Simple program that greets NAME for a total of COUNT times.";
8+
}
59
int count;
610
std::string name;
711

@@ -21,7 +25,8 @@ struct hello
2125
}
2226
};
2327

24-
int main(int argc, char const *argv[]) {
28+
int main(int argc, char const *argv[])
29+
{
2530
args::parse<hello>(argc, argv);
2631
}
2732

examples/groups.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#include <args.hpp>
22

33
struct cli : args::group<cli>
4-
{};
4+
{
5+
static const char* help()
6+
{
7+
return "Command-line interface to manage a database";
8+
}
9+
};
510

611
struct initdb : cli::command<initdb>
712
{
@@ -29,7 +34,8 @@ struct dropdb : cli::command<dropdb>
2934
}
3035
};
3136

32-
int main(int argc, char const *argv[]) {
37+
int main(int argc, char const *argv[])
38+
{
3339
args::parse<cli>(argc, argv);
3440
}
3541

0 commit comments

Comments
 (0)