Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pfultz2/args
Simple and type-safe commandline argument parser for C++14
https://github.com/pfultz2/args
argument-parser command-line-parser cplusplus cplusplus-11 cplusplus-14 cpp cpp11 cpp14
Last synced: 2 months ago
JSON representation
Simple and type-safe commandline argument parser for C++14
- Host: GitHub
- URL: https://github.com/pfultz2/args
- Owner: pfultz2
- Created: 2016-07-31T08:51:37.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-08-05T16:12:24.000Z (over 3 years ago)
- Last Synced: 2024-09-30T00:02:09.169Z (2 months ago)
- Topics: argument-parser, command-line-parser, cplusplus, cplusplus-11, cplusplus-14, cpp, cpp11, cpp14
- Language: C++
- Homepage:
- Size: 44.9 KB
- Stars: 66
- Watchers: 8
- Forks: 11
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- AwesomeCppGameDev - args - safe commandline argument parser for C++14 (C++)
README
Args
====Simple and typesafe commandline parsing for C++14.
Quickstart
----------Simply provide a class with fields that is to be filled by command-line arguments:
```cpp
struct hello
{
static const char* help()
{
return "Simple program that greets NAME for a total of COUNT times.";
}
int count;
std::string name;hello() : count(1)
{}template
void parse(F f)
{
f(count, "--count", "-C", args::help("Number of greetings."));
f(name, "--name", "-N", args::help("The person to greet."), args::required());
}void run()
{
for(int i=0;i(argc, argv);
}
```The command then could be run like this:
```
$ hello --name Paul --count 3
Paul
Paul
Paul```
The args library will auto-generate help info:
```
$ hello --help
Usage: hello [options...]Simple program that greets NAME for a total of COUNT times.
Options:
-h, --help Show help
--count, -C [integer] Number of greetings.
--name, -N [string] The person to greet.
```In addition, nested commands can be created:
```cpp
struct cli : args::group
{
static const char* help()
{
return "Command-line interface to manage a database";
}
};struct initdb : cli::command
{
initdb() {}
static const char* help()
{
return "Initialize database";
}
void run()
{
printf("Initialize database\n");
}
};struct dropdb : cli::command
{
dropdb() {}
static const char* help()
{
return "Delete database";
}
void run()
{
printf("Delete database\n");
}
};int main(int argc, char const *argv[])
{
args::parse(argc, argv);
}```
So each subcommand can be ran like this:
```
$ cli initdb
Initialize database
``````
$ cli dropdb
Delete database
```In addition help is generated for subcommands as well:
```
$ cli --help
Usage: cli [options...] [command]Command-line interface to manage a database
Options:
-h, --help Show help
Commands:
dropdb Delete database
initdb Initialize database```