Skip to content

Commit

Permalink
Merge branch 'master' of github.com:FlorianRappl/CmdParser
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Apr 21, 2015
2 parents 1f85bce + e942a9f commit 16135b8
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Simple C++ command line parser

This project supplies a simple, single-header, command-line parser. It is very lightweight and relies on templates. The easiest way is to use it as a dependency via *biicode*. The parser requires C++11 and works fine on GCC (v4.8.2 or later, some earlier versions should work as well), ICC (v14 or later) and MSVC (v18 or later).
[![Build Status](https://webapi.biicode.com/v1/badges/FlorianRappl/FlorianRappl/CmdParser/master)](https://www.biicode.com/FlorianRappl/CmdParser)

This project supplies a simple, single-header, command-line parser. It is very lightweight and relies on templates. The easiest way is to use it is as a dependency via *biicode*. The parser requires C++11 and works fine on gcc (v4.8.2 or later, some earlier versions should work as well), icc (v14 or later), clang and msvc (v18 or later).

## Using the parser

Using the parser is straight forward. Only include the header (*cmdparser.h*) in your application source file, most likely the one that contains the `main` method. Pass the command line arguments to the parser:

int main(int argc, char** argv) {
cli::Parser parser(argc, argv);
/* ... */
}
```cpp
int main(int argc, char** argv) {
cli::Parser parser(argc, argv);
/* ... */
}
```
In the following two sections we'll have a look at setting up the parser and using it.
Expand All @@ -30,41 +34,47 @@ The third parameter is the fourth parameter for optional arguments.
Let's look at an example:
void configure_parser(cli::Parser& parser) {
parser.set_optional<std::string>("o", "output", "data", "Strings are naturally included.");
parser.set_optional<int>("n", "number", 8, "Integers in all forms, e.g., unsigned int, long long, ..., are possible.");
parser.set_optional<double>("b", "beta", 11.0, "Also floating point values are possible.");
parser.set_optional<bool>("a", "all", false, "Boolean arguments are simply switched when encountered, i.e. false to true if provided.");
parser.set_required<std::vector<short>>("v", "values", "By using a vector it is possible to receive a multitude of inputs.");
}
```cpp
void configure_parser(cli::Parser& parser) {
parser.set_optional<std::string>("o", "output", "data", "Strings are naturally included.");
parser.set_optional<int>("n", "number", 8, "Integers in all forms, e.g., unsigned int, long long, ..., are possible.");
parser.set_optional<double>("b", "beta", 11.0, "Also floating point values are possible.");
parser.set_optional<bool>("a", "all", false, "Boolean arguments are simply switched when encountered, i.e. false to true if provided.");
parser.set_required<std::vector<short>>("v", "values", "By using a vector it is possible to receive a multitude of inputs.");
}
```

Usually it makes sense to pack the Parser's setup in a function. But of course this is not required. The shorthand is not limited to a single character. It could also be the same as the longhand alternative.

### Getting values

Getting values is possible via the `get` method. This is also a template. We need to specify the type of argument. This has to be the same type as defined earlier. It also has to be a valid argument (shorthand) name. At the moment only shorthands are considered here. For instance we could do the following:

//auto will be int
auto number = parser.get<int>("n");
```cpp
//auto will be int
auto number = parser.get<int>("n");

//auto will be std::string
auto output = parser.get<std::string>("o");
//auto will be std::string
auto output = parser.get<std::string>("o");

//auto will be bool
auto all = parser.get<bool>("a");
//auto will be bool
auto all = parser.get<bool>("a");

//auto will be std::vector<short>
auto values = parser.get<std::vector<short>>("v");
//auto will be std::vector<short>
auto values = parser.get<std::vector<short>>("v");
```

However, before we can access these values we also need to check if the provided user input was valid. On construction the `Parser` does not examine the input. The parser waits for setup and a potential call to the `run` method. The `run` method runs a boolean value to indicate if the provided command line arguments match the requirements.

What we usually want is something like:

void parse_and_exit(cli::Parser& parser) {
if (parser.parse() == false) {
exit(1);
}
```cpp
void parse_and_exit(cli::Parser& parser) {
if (parser.parse() == false) {
exit(1);
}
}
```
Writing this function seems to be redundant. Hence the parser includes it already:
Expand All @@ -78,12 +88,14 @@ The parser comes with a pre-defined command that has the shorthand `-h` and the
Finally our `main` method may look as follows:
int main(int argc, char** argv) {
cli::Parser parser(argc, argv);
configure_parser(parser);
cmd.run_and_exit_if_error();
/* ... */
}
```cpp
int main(int argc, char** argv) {
cli::Parser parser(argc, argv);
configure_parser(parser);
cmd.run_and_exit_if_error();
/* ... */
}
```

This passes the arguments to the parser, configures the parser and checks for potential errors. In case of any errors the program is exited immediately.

Expand All @@ -101,4 +113,4 @@ This program is free software: you can redistribute it and/or modify it under th

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

0 comments on commit 16135b8

Please sign in to comment.