Modern C/C++ Argument Parser.
Table of Contents
Argparser
is a lightweight and feature-rich command-line argument parsing library for C/C++.
It offers a modern and intuitive interface allowing for straightforward argument configuration and parsing.
- Parse keyword arguments
- Parse positional arguments
- Parse flags
- Set default values for arguments
- Print help messages
Argparser distinguishes 3 different types of arguments:
Type | Functin | Description |
---|---|---|
arg |
add_arg | named positional arguments (e.g. file) |
flag |
add_flag | a boolean argument that is by default false (e.g. --verbose) |
kwarg |
add_kwarg | keyworded-arguments that require a key and a value, (e.g. --variable=5) |
--help --verbose --input=file.txt
-hvi=file.txt
-h --verbose -i file.txt
--long value
Compiler | Min Version |
---|---|
GNU G++ | 13 |
Clang | 17 |
To use Argparser in your project, simply include the Argparser.h
header file in your source files.
#include "Argparser.h"
#include "Argparser.h"
int main(int argc, char *argv[])
{
ArgumentParser parser;
init_parser(&parser, NULL, NULL, NULL, NULL, 1);
add_flag(&parser, 'v', "verbose", "Enable verbose mode");
add_flag(&parser, 's', "store", "Save file Name");
add_kwarg(&parser, 'c', "count", 0, NULL, "Number of times");
parse_args(&parser, argc, argv);
int verbose = get_flag(&parser, "verbose");
int store = get_flag(&parser, "store");
int help = get_flag(&parser, "help");
const char *count = get_kwarg(&parser, "count");
if (help)
print_help(&parser, 1, 1, 1, 1);
if (count)
printf("Count: %s\n", count);
if (store)
printf("Store: %d\n", store);
if (verbose)
printf("Verbose: %d\n", verbose);
free_parser(&parser);
return 0;
}
$ ./sample --help
Welcome to Argparser
Usage: ./sample file.txt -v [FILE] [-h | -v ] [--output FILE] [options...]
file.txt : Output path [required]
-v : Verbose Output [default: false]
Options:
-k : An implicit int parameter [implicit: "3", required]
-a,--alpha : An optional float parameter with default value [default: 0.6]
-b,--beta : An optional float parameter with std::optional return [default: none]
-n,--numbers : An int vector, comma separated [required]
--files : multiple arguments [required]
-c,--color : An Enum input [allowed: <red, blue, green>, required]
-v,--verbose : A flag to toggle verbose [implicit: "true", default: false]
--help : print help [implicit: "true", default: false]
- π¬ Join the Discussions: Share your insights, provide feedback, or ask questions.
- π Report Issues: Submit bugs found or log feature requests for the
argparser
project. - π‘ Submit Pull Requests: Review open PRs, and submit your own PRs.
Contributing Guidelines
- Fork the Repository: Start by forking the project repository to your github account.
- Clone Locally: Clone the forked repository to your local machine using a git client.
git clone --recursive https://github.com/djoezeke/argparser
- Create a New Branch: Always work on a new branch, giving it a descriptive name.
git checkout -b new-feature-x
- Make Your Changes: Develop and test your changes locally.
- Commit Your Changes: Commit with a clear message describing your updates.
git commit -m 'Implemented new feature x.'
- Push to github: Push the changes to your forked repository.
git push origin new-feature-x
- Submit a Pull Request: Create a PR against the original project repository. Clearly describe the changes and their motivations.
- Review: Once your PR is reviewed and approved, it will be merged into the main branch. Congratulations on your contribution!
Argparser
is inspired by Python Argparse and aims to provide a simple and lightweight solution for C/C++ projects.
This project is protected under the MIT License. For more details, refer to the LICENSE file.