|
| 1 | +# slic |
| 2 | + |
| 3 | +Simple C++20 command line argument parser. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Single header file |
| 8 | +- Zero allocations |
| 9 | +- No dependencies |
| 10 | +- Compile-time configuration and validation |
| 11 | +- Supports positional and named arguments |
| 12 | +- Variadic arguments support |
| 13 | +- Type-safe parsing of arguments |
| 14 | +- Help message generation |
| 15 | +- Error handling |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Here's a simple example of how to use `slic` to parse command line arguments: |
| 20 | + |
| 21 | +```cpp |
| 22 | +#include <slic.hpp> |
| 23 | + |
| 24 | +// Define your argument struct |
| 25 | +struct MyArgs { |
| 26 | + std::string_view input; |
| 27 | + std::string_view output; |
| 28 | + std::string_view test; |
| 29 | + int number = 0; |
| 30 | + bool version = false; |
| 31 | + slic::ArgSpan args; |
| 32 | + |
| 33 | + static constexpr std::string_view Description = "My awesome CLI tool"; |
| 34 | + static constexpr std::tuple Options = { |
| 35 | + slic::Option{"--number", "-n", &MyArgs::number, "Some number"}, |
| 36 | + slic::Option{"--version", "-v", &MyArgs::version, "Show version"}, |
| 37 | + slic::Option{"--test", "-t", &MyArgs::test, "Test argument"}, |
| 38 | + slic::Arg{"INPUT", &MyArgs::input, "Input file"}, |
| 39 | + slic::Arg{"OUTPUT", &MyArgs::output, "Output file"}, |
| 40 | + slic::VarArgs{"Extra files", &MyArgs::args} |
| 41 | + }; |
| 42 | +}; |
| 43 | + |
| 44 | +int main(int argc, char** argv) { |
| 45 | + // Create the parser |
| 46 | + slic::ArgParser<MyArgs> parser(argc, argv); |
| 47 | + |
| 48 | + // Parse the arguments and handle errors |
| 49 | + auto result = parser.parse(); |
| 50 | + if (!result) { |
| 51 | + result.print(); |
| 52 | + // you can also print help message if needed |
| 53 | + parser.printHelp(); |
| 54 | + return 1; |
| 55 | + } |
| 56 | + |
| 57 | + MyArgs const& args = parser.result(); // parser holds your struct |
| 58 | + |
| 59 | + // access members directly |
| 60 | + if (args.version) { |
| 61 | + std::cout << "My CLI Tool version 1.0.0\n"; |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + // access variadic arguments |
| 66 | + for (std::string_view arg : args.args) { |
| 67 | + std::cout << "Extra arg: " << arg << "\n"; |
| 68 | + } |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | +``` |
| 73 | +
|
| 74 | +Help message output for the above example: |
| 75 | +
|
| 76 | +``` |
| 77 | +My awesome CLI tool |
| 78 | + |
| 79 | +Usage: my_program [OPTIONS] <INPUT> <OUTPUT> [...] |
| 80 | + |
| 81 | +Arguments: |
| 82 | + INPUT: Input file |
| 83 | + OUTPUT: Output file |
| 84 | + [...]: Extra files |
| 85 | + |
| 86 | +Options: |
| 87 | + -n, --number <value>: Some number |
| 88 | + -v, --version: Show version |
| 89 | + -t, --test <value>: Test argument |
| 90 | +``` |
| 91 | +
|
| 92 | +## License |
| 93 | +
|
| 94 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
0 commit comments