| 1 | Argengine |
| 2 | ========= |
| 3 | |
| 4 | Looking for a simple CLI argument parser for your C++ project? `Argengine` might be for you. |
| 5 | |
| 6 | # Features |
| 7 | |
| 8 | * Automatic help generation |
| 9 | * Based on simple lambda callbacks |
| 10 | * Builds with CMake |
| 11 | * Designed for modern C++ |
| 12 | * Extensive unit tests |
| 13 | * Single .hpp/.cpp |
| 14 | * Very easy to use |
| 15 | |
| 16 | # Design Choices |
| 17 | |
| 18 | * Argument values are passed as strings. The client is responsible for possible data conversions. |
| 19 | * Most of the data is passed by value for a cleaner API. It is assumed that argument parsing is not the performance bottleneck of your application |
| 20 | * The PIMPL idiom is used to maintain API/ABI compatibility |
| 21 | |
| 22 | # Installation |
| 23 | |
| 24 | ## Use from sources |
| 25 | |
| 26 | Just add `src/argengine.hpp` and `src/argengine.cpp` to your project and start using it! |
| 27 | |
| 28 | ## Use as a CMake subproject |
| 29 | |
| 30 | Copy contents of `Argengine` under your main project (or clone as a Git submodule). |
| 31 | |
| 32 | In your `CMakeLists.txt`: |
| 33 | |
| 34 | ``` |
| 35 | add_subdirectory(Argengine EXCLUDE_FROM_ALL) |
| 36 | include_directories(Argengine/src) |
| 37 | ``` |
| 38 | |
| 39 | Link to the library: |
| 40 | |
| 41 | ``` |
| 42 | target_link_libraries(${YOUR_TARGET_NAME} Argengine_static) |
| 43 | ``` |
| 44 | |
| 45 | In your code: |
| 46 | |
| 47 | ``` |
| 48 | #include "argengine.hpp" |
| 49 | ``` |
| 50 | |
| 51 | ## Use as a library |
| 52 | |
| 53 | Build and install: |
| 54 | |
| 55 | `$ mkdir build && cd build` |
| 56 | |
| 57 | `$ cmake ..` |
| 58 | |
| 59 | `$ make` |
| 60 | |
| 61 | `$ sudo make install` |
| 62 | |
| 63 | Link to `libArgengine_static.a` or `libArgengine.so`. |
| 64 | |
| 65 | # Usage In A Nutshell |
| 66 | |
| 67 | The basic principle is that for each option a lambda callback is added. |
| 68 | |
| 69 | A valueless callback: |
| 70 | |
| 71 | ``` |
| 72 | ... |
| 73 | |
| 74 | juzzlin::Argengine ae(argc, argv); |
| 75 | ae.addOption({"-f", "--foo"}, [] { |
| 76 | // Do something |
| 77 | }); |
| 78 | ae.parse(); |
| 79 | |
| 80 | ... |
| 81 | ``` |
| 82 | |
| 83 | A single-value callback: |
| 84 | |
| 85 | ``` |
| 86 | ... |
| 87 | |
| 88 | juzzlin::Argengine ae(argc, argv); |
| 89 | ae.addOption({"-f", "--foo"}, [] (std::string value) { |
| 90 | // Do something with value |
| 91 | }); |
| 92 | ae.parse(); |
| 93 | |
| 94 | ... |
| 95 | ``` |
| 96 | |
| 97 | There can be as many option variants as liked, usually the short and long versions, e.g `-f` and `--foo`. |
| 98 | |
| 99 | `Argengine` doesn't care about the naming of the options and they can be anything: `-f`, `a`, `/c`, `foo`, `--foo` ... |
| 100 | |
| 101 | Positional arguments (for example a file name for a text editor after other options) can be received with a single callback: |
| 102 | |
| 103 | ``` |
| 104 | ... |
| 105 | |
| 106 | juzzlin::Argengine ae(argc, argv); |
| 107 | ae.setPositionalArgumentCallback([] (std::vector<std::string> args) { |
| 108 | // Do something with arguments |
| 109 | }); |
| 110 | ae.parse(); |
| 111 | |
| 112 | ... |
| 113 | ``` |
| 114 | |
| 115 | If the callback for positional arguments is set, then no errors about `unknown options` will occur as all additional options will be taken as positional arguments. |
| 116 | |
| 117 | # Help |
| 118 | |
| 119 | By default, `Argengine` will create a simple help that is shown with `-h` or `--help`. |
| 120 | |
| 121 | Without any additional options a possible output will look like this: |
| 122 | |
| 123 | ``` |
| 124 | Usage: ./ex1 [OPTIONS] |
| 125 | |
| 126 | Options: |
| 127 | |
| 128 | -h, --help Show this help. |
| 129 | ``` |
| 130 | |
| 131 | The help can be manually printed with `Argengine::printHelp()`. |
| 132 | |
| 133 | The default help can be disabled by constructing `Argengine` with `Argengine::Argengine(argc, argv, false)`. |
| 134 | |
| 135 | ## Custom help |
| 136 | |
| 137 | A custom help can be added with: |
| 138 | |
| 139 | `void Argengine::addHelp(OptionVariants optionVariants, ValuelessCallback callback)` |
| 140 | |
| 141 | You'd still very likely want to call `Argengine::printHelp()` in the callback and just add some stuff around it. |
| 142 | |
| 143 | ## Sorting order of options |
| 144 | |
| 145 | The sorting order of options can be selected with: |
| 146 | |
| 147 | `void Argengine::setHelpSorting(HelpSorting helpSorting)` |
| 148 | |
| 149 | ## Custom help text |
| 150 | |
| 151 | The text printed before options can be set with: |
| 152 | |
| 153 | `void Argengine::setHelpText(std::string helpText)` |
| 154 | |
| 155 | # Examples |
| 156 | |
| 157 | ## Valueless options: The simplest possible example |
| 158 | |
| 159 | Valueless options are options without any value, so they are just flags. The lambda callback is of the form `[] {}`. |
| 160 | |
| 161 | ``` |
| 162 | #include "argengine.hpp" |
| 163 | #include <iostream> |
| 164 | |
| 165 | int main(int argc, char ** argv) |
| 166 | { |
| 167 | juzzlin::Argengine ae(argc, argv); |
| 168 | ae.addOption({"-f", "--foo"}, [] { |
| 169 | std::cout << "'-f' or '--foo' given!" << std::endl; |
| 170 | }); |
| 171 | ae.parse(); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | ``` |
| 176 | |
| 177 | ## Single-value options: The simplest possible example |
| 178 | |
| 179 | Single-value options are options that can have only one value. |
| 180 | |
| 181 | As an example, for option `-f` The following formats are allowed: `-f 42`, `-f42`, `-f=42`. |
| 182 | |
| 183 | Preferably there should be either space or '`=`'. The spaceless format is accepted if not ambiguous. |
| 184 | |
| 185 | The lambda callback is of the form `[] (std::string value) {}`. |
| 186 | |
| 187 | ``` |
| 188 | #include "argengine.hpp" |
| 189 | #include <iostream> |
| 190 | |
| 191 | int main(int argc, char ** argv) |
| 192 | { |
| 193 | juzzlin::Argengine ae(argc, argv); |
| 194 | ae.addOption({"-f", "--foo"}, [] (std::string value) { |
| 195 | std::cout << "Value for '-f' or '--foo': " << value << std::endl; |
| 196 | }); |
| 197 | ae.parse(); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | ``` |
| 202 | |
| 203 | ## General: Marking an option **required** |
| 204 | |
| 205 | In order to mark an option mandatory, there's an overload that accepts `bool required` right after the callback: |
| 206 | |
| 207 | ``` |
| 208 | ... |
| 209 | |
| 210 | juzzlin::Argengine ae(argc, argv); |
| 211 | ae.addOption({"-f", "--foo"}, [] { |
| 212 | // Do something |
| 213 | }, |
| 214 | true); // Required |
| 215 | |
| 216 | ... |
| 217 | ``` |
| 218 | |
| 219 | ## General: Adding option documentation for the auto-generated help |
| 220 | |
| 221 | One can add option documentation for the auto-generated help. |
| 222 | |
| 223 | Consider this example code: |
| 224 | |
| 225 | ``` |
| 226 | ... |
| 227 | |
| 228 | juzzlin::Argengine ae(argc, argv); |
| 229 | ae.addOption( |
| 230 | { "-p" }, [](std::string value) { |
| 231 | std::cout << value.size() << std::endl; |
| 232 | }, |
| 233 | true, "Print length of given text. This option is required.", "TEXT"); |
| 234 | |
| 235 | ... |
| 236 | ``` |
| 237 | |
| 238 | Here we have added a documentation string `Print length of given text. This option is required.` and also a variable name `TEXT` for option `-p`. |
| 239 | |
| 240 | Thus, this will generate a help content like this: |
| 241 | |
| 242 | ``` |
| 243 | Options: |
| 244 | |
| 245 | -h, --help Show this help. |
| 246 | -p [TEXT] Print length of given text. This option is required. |
| 247 | |
| 248 | ``` |
| 249 | |
| 250 | ## General: Setting two or more options as conflicting |
| 251 | |
| 252 | Sometimes we want to prevent the user from giving a certain set of arguments. |
| 253 | |
| 254 | Consider this example code: |
| 255 | |
| 256 | ``` |
| 257 | ... |
| 258 | |
| 259 | juzzlin::Argengine ae(argc, argv); |
| 260 | ae.addOption( |
| 261 | { "foo" }, [] { |
| 262 | std::cout << "Foo enabled!" << std::endl; |
| 263 | }); |
| 264 | |
| 265 | ae.addOption( |
| 266 | { "bar" }, [] { |
| 267 | std::cout << "Bar enabled!" << std::endl; |
| 268 | }); |
| 269 | |
| 270 | // Set "foo" and "bar" as conflicting options. |
| 271 | ae.addConflictingOptions({ "foo", "bar" }); |
| 272 | ... |
| 273 | ``` |
| 274 | |
| 275 | Now, if we give both `foo` and `bar` to the application, we'll get an error like this: |
| 276 | |
| 277 | |
| 278 | ``` |
| 279 | Argengine: Conflicting options: 'bar', 'foo'. These options cannot coexist. |
| 280 | ``` |
| 281 | |
| 282 | ## General: Adding option groups |
| 283 | |
| 284 | Option groups will make the given options depend on each other. |
| 285 | |
| 286 | Consider this example code: |
| 287 | |
| 288 | ``` |
| 289 | ... |
| 290 | |
| 291 | juzzlin::Argengine ae(argc, argv); |
| 292 | ae.addOption( |
| 293 | { "foo" }, [] { |
| 294 | std::cout << "Foo enabled!" << std::endl; |
| 295 | }); |
| 296 | |
| 297 | ae.addOption( |
| 298 | { "bar" }, [] { |
| 299 | std::cout << "Bar enabled!" << std::endl; |
| 300 | }); |
| 301 | |
| 302 | // Add "foo" and "bar" as an option group. |
| 303 | ae.addOptionGroup({ "foo", "bar" }); |
| 304 | ... |
| 305 | ``` |
| 306 | |
| 307 | Now, if we give only `foo` to the application, we'll get an error like this: |
| 308 | |
| 309 | |
| 310 | ``` |
| 311 | Argengine: These options must coexist: 'bar', 'foo'. Missing options: 'bar'. |
| 312 | ``` |
| 313 | |
| 314 | ## General: Error handling |
| 315 | |
| 316 | For error handling there are two options: exceptions or error value. |
| 317 | |
| 318 | `Argengine::parse()` will throw on error, `Argengine::parse(Error & error)` will set and error. |
| 319 | |
| 320 | Example of handling exceptions: |
| 321 | |
| 322 | ``` |
| 323 | ... |
| 324 | |
| 325 | try { |
| 326 | ae.parse(); |
| 327 | } catch(std::runtime_error & e) { |
| 328 | std::cerr << e.what() << std::endl; |
| 329 | ae.printHelp(); |
| 330 | return EXIT_FAILURE; |
| 331 | } |
| 332 | |
| 333 | ... |
| 334 | ``` |
| 335 | |
| 336 | Example of handling error values: |
| 337 | |
| 338 | ``` |
| 339 | ... |
| 340 | Argengine::Error error; |
| 341 | ae.parse(error); |
| 342 | |
| 343 | if (error.code != Argengine::Error::Code::Ok) { |
| 344 | std::cerr << error.message << std::endl |
| 345 | << std::endl; |
| 346 | ae.printHelp(); |
| 347 | return EXIT_FAILURE; |
| 348 | } |
| 349 | |
| 350 | ... |
| 351 | ``` |
| 352 | |
| 353 | # Requirements |
| 354 | |
| 355 | C++17 |
| 356 | |
| 357 | # Licence |
| 358 | |
| 359 | MIT |
| 360 | |
| 361 | # Projects currently using Argengine |
| 362 | |
| 363 | * https://github.com/juzzlin/DustRacing2D |
| 364 | |
| 365 | * https://github.com/juzzlin/Heimer |