https://github.com/djoezeke/myargs
A Straight forward Command-line arguments parsing library for modern C/C++ that would make your day.
https://github.com/djoezeke/myargs
argument-parser c cli command-line console cplusplus flag options parsing subcommands
Last synced: about 1 month ago
JSON representation
A Straight forward Command-line arguments parsing library for modern C/C++ that would make your day.
- Host: GitHub
- URL: https://github.com/djoezeke/myargs
- Owner: djoezeke
- License: mit
- Created: 2025-03-07T01:30:43.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-03-08T01:58:14.000Z (about 2 months ago)
- Last Synced: 2025-03-08T02:24:49.185Z (about 2 months ago)
- Topics: argument-parser, c, cli, command-line, console, cplusplus, flag, options, parsing, subcommands
- Language: C
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# MyArgs | Modern Argument Parser
## 📖 Description
A lightweight header-only library for parsing command-line arguments, including support for flags and keyword arguments in an elegant manner.
## ✨ Features
- Parse keyword arguments
- Parse positional arguments
- Parse flags
- Set default values for arguments
- Print help messagesMyArgs 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) |### 📝 Supported Syntax
```
--help --verbose --input=file.txt
-hvi=file.txt
-h --verbose -i file.txt
--long value
```## 🛠️ Installation
To use MyArgs in your project, simply include the `myargs.h` header file in your source files.
```cpp
#include "myargs.h"
```## 🚀 Usage
```c
#include "myargs.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;
}
```### Example Help
```sh
$ ./sample --help
Welcome to MyArgs
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: , required]
-v,--verbose : A flag to toggle verbose [implicit: "true", default: false]
--help : print help [implicit: "true", default: false]
```## License 📄
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Contributing 🤝
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
## Acknowledgements 🙏
MyArgs is inspired by Python Argparse and aims to provide a simple and lightweight solution for C/C++ projects.