A C++ library for parsing command-line arguments. This library is designed to simplify the process of handling command-line options in your C++ programs by supporting various argument types, automatic help generation, and positional arguments.
-
Add short and long named arguments
-
Supports
int
,bool
, andstring
types -
Assign default values
-
Retrieve parsed values by type
-
Type-safe arguments: Supports arguments of type
int
,bool
, andstd::string
. -
Short and long argument names: Define arguments with both short (e.g.
-n
) and long names (e.g.--number
). -
Default values and required arguments:
- Arguments without a default value are required; if not provided, parsing will fail.
- Optional arguments can have default values using
.Default(...)
.
-
Value storage:
- Retrieve parsed values using getter methods like
GetIntValue
,GetStringValue
, andGetFlag
. - Store values directly into external variables with
StoreValue()
(for single values) andStoreValues()
(for multiple values).
- Retrieve parsed values using getter methods like
-
Multi-value arguments:
- Support for arguments that can be specified multiple times (e.g.
--param=1 --param=2
). - Specify a minimum number of required values using
MultiValue(min_count)
. Default - unlimited
- Support for arguments that can be specified multiple times (e.g.
-
Positional arguments:
- Define arguments that are matched by their position on the command line rather than by a flag.
-
Combined flags:
- Supports grouping of short flag arguments (e.g.
-ac
to enable both-a
and-c
).
- Supports grouping of short flag arguments (e.g.
-
Help functionality:
- Add a help flag (e.g.
-h
/--help
) to automatically generate a help message detailing usage, argument types, default values, and requirements.
- Add a help flag (e.g.
-
Dynamic configuration:
- The parser supports repeated parsing. It allows modifying the configuration (e.g., adding new arguments based on previous flags) and parsing again.
The project includes a suite of tests (using Google Test) to verify the functionality of the library. The tests cover various aspects such as:
- Parsing of string, integer, and flag arguments.
- Handling of default values and required arguments.
- Multi-value and positional arguments.
- Combined flags and dynamic parsing behavior.
- Automatic help message generation.
A CMakeLists.txt
file is provided for building the project and its tests. To build the project and run tests using CMake, follow these steps:
-
Create a build directory and navigate into it:
mkdir build && cd build
-
Configure the project:
cmake ..
-
Build the project:
cmake --build .
-
Run the tests:
ctest
Alternatively, you can run the test executable directly (usually located in the
build/tests
directory).
#include <lib/ArgParser.h>
#include <iostream>
#include <numeric>
struct Options {
bool sum = false;
bool mult = false;
};
using namespace ArgumentParser;
int main(int argc, char** argv) {
Options opt;
std::vector<int> values;
ArgumentParser::ArgParser parser("Program");
parser.AddIntArgument("N").MultiValue(1).Positional().StoreValues(values);
parser.AddFlag('s', "sum", "add args").StoreValue(opt.sum);
parser.AddFlag("mult", "multiply args").StoreValue(opt.mult);
parser.AddHelp('h', "help", "Program accumulate arguments");
if (!parser.Parse(argc, argv)) {
std::cout << "Wrong argument" << std::endl;
std::cout << parser.HelpDescription() << std::endl;
return 1;
}
if (parser.Help()) {
std::cout << parser.HelpDescription() << std::endl;
return 0;
}
if (opt.sum) {
std::cout << "Result: " << std::accumulate(values.begin(), values.end(), 0) << std::endl;
} else if (opt.mult) {
std::cout << "Result: " << std::accumulate(values.begin(), values.end(), 1, std::multiplies<int>()) << std::endl;
} else {
std::cout << "No one options had chosen" << std::endl;
std::cout << parser.HelpDescription();
return 1;
}
return 0;
}
g++ main.cpp lib/ArgParser.cpp -o app
MIT License