|
| 1 | +# GetOptC++ |
| 2 | +GetOptC++ is a C++11 header file that parses command line arguments. It's inspired mostly by Golang's `flag` package, although you shouldn't expect the same quality from here. |
| 3 | +It's a monolithic header file, so the only thing you really need to use GetOptC++ is the `getoptc++.hpp` file in this repository. That's it. Really. |
| 4 | + |
| 5 | +# Usage |
| 6 | +Simply download `getoptc++.hpp`, and add it into your project. Include the file, and you are off to the races! |
| 7 | + |
| 8 | +By default, GetOptC++ operates in `STRICT` mode, meaning only commands like these are valid: |
| 9 | +``` |
| 10 | +./my-executable -a 0 --bee="Hello!" -c "hmm" --delta=90 OPTION1 OPTION2 OPTION3 OPTION4 |
| 11 | +``` |
| 12 | +Commands in the following form will be rejected: |
| 13 | +``` |
| 14 | +./my-executable -a=0 --bee "Hello!" -c="hmm" --delta 90 |
| 15 | +``` |
| 16 | + |
| 17 | +GetOptC++ throws errors, but most of the time, all errors are related to the parsing of the user's flags. Hence, it might be wise to test your application thoroughly if you are using GetOptC++, so that you can catch specific errors related to the user's flags. Other than standard library errors, GetOptC++ has the following errors: |
| 18 | +- `no_argv` |
| 19 | +- `impossible_case_reached` |
| 20 | + |
| 21 | +The errors are pretty self-explanatory. Known standard library errors thrown: |
| 22 | +- `std::out_of_range` - Happens when the user places many flags that require furthur input, but does not provide them |
| 23 | +- `std::invalid_argument` - Happens when a user places a string into an input that requires a number |
| 24 | + |
| 25 | +If there are more errors, please open an issue to inform us. |
| 26 | + |
| 27 | +## Examples |
| 28 | +```cpp |
| 29 | +#include "getoptc++.hpp" |
| 30 | +#include <iostream> |
| 31 | + |
| 32 | +// Example main function to use with getoptc++ |
| 33 | +int main(int argc, char** argv) { |
| 34 | + GetOpt go(argc, argv); //main arguments argc and argv. |
| 35 | + //A third argument, isFromSystem, also exists. That will |
| 36 | + //set whether argv comes from a caller-generated function |
| 37 | + //or system-generated function. Specifically, it controls |
| 38 | + //whether getoptc++ skips the first argument, which is |
| 39 | + //usually the executable of the program. |
| 40 | + //GetOpt is a type alias of __impl_GetOpt<int, char, double, std::string> |
| 41 | + //You can also do: |
| 42 | + //GetOpt go; |
| 43 | + //go.initialize(argc, argv); |
| 44 | + //They are equivalent statements. |
| 45 | + |
| 46 | + //The flagBoolean function is usually called like this. |
| 47 | + //The string argument is separated by commas, and they |
| 48 | + //state the different names the flag can have. |
| 49 | + //Note that the last name that is parsed will have precedence |
| 50 | + //over all the previous names. This means that calling |
| 51 | + //-b --boolean --alias at the same time will give --alias |
| 52 | + //the precedence. |
| 53 | + bool& a_bool_variable = *go.flagBoolean("-b,--boolean,--alias"); |
| 54 | + |
| 55 | + //By providing a second argument, the caller can control |
| 56 | + //the default value that the flag will contain. This is also |
| 57 | + //the value of the int should there be no such flag in existance. |
| 58 | + //If not set, the default values are: 0 or "". |
| 59 | + int& an_int_variable = *go.flagInt("-i,--integer", 10); |
| 60 | + double& a_double_variable = *go.flagFloating("-f,--floating,--float,--decimal", 0.0); |
| 61 | + std::string& a_str_variable = *go.flagString("-s,--string", ""); |
| 62 | + |
| 63 | + //Once you are done setting the flags, you call: |
| 64 | + go.readArgs(AS_IS); |
| 65 | + //This function returns a std::vector<std::string>, which contins the |
| 66 | + //flags that getoptc++ failed to parse. It is ok to ignore it. |
| 67 | + |
| 68 | + //This function gets the options parsed by the previous call to go.readArgs |
| 69 | + //The type it returns is std::vector<std:string> |
| 70 | + auto options = go.getOptions(); |
| 71 | + |
| 72 | + std::cout << "a_bool_variable: " << std::boolalpha << a_bool_variable << std::noboolalpha << std::endl; |
| 73 | + std::cout << "an_int_variable: " << an_int_variable << std::endl; |
| 74 | + std::cout << "a_double_variable: " << a_double_variable << std::endl; |
| 75 | + std::cout << "a_str_variable: " << a_str_variable << std::endl; |
| 76 | + |
| 77 | + std::cout << "Options: "; |
| 78 | + for (auto&& it : options) std::cout << it << " "; |
| 79 | + std::cout << std::endl; |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
| 83 | +``` |
| 84 | +and a version without comments because I know I write too much: |
| 85 | +```cpp |
| 86 | +#include "getoptc++.hpp" |
| 87 | +#include <iostream> |
| 88 | +
|
| 89 | +int main(int argc, char** argv) { |
| 90 | + GetOpt go(argc, argv); |
| 91 | +
|
| 92 | + bool& a_bool_variable = *go.flagBoolean("-b,--boolean,--alias"); |
| 93 | + int& an_int_variable = *go.flagInt("-i,--integer", 10); |
| 94 | + double& a_double_variable = *go.flagFloating("-f,--floating,--float,--decimal", 0.0); |
| 95 | + std::string& a_str_variable = *go.flagString("-s,--string", ""); |
| 96 | +
|
| 97 | + go.readArgs(AS_IS); |
| 98 | +
|
| 99 | + auto options = go.getOptions(); |
| 100 | +
|
| 101 | + std::cout << "a_bool_variable: " << std::boolalpha << a_bool_variable << std::noboolalpha << std::endl; |
| 102 | + std::cout << "an_int_variable: " << an_int_variable << std::endl; |
| 103 | + std::cout << "a_double_variable: " << a_double_variable << std::endl; |
| 104 | + std::cout << "a_str_variable: " << a_str_variable << std::endl; |
| 105 | +
|
| 106 | + std::cout << "Options: "; |
| 107 | + for (auto&& it : options) std::cout << it << " "; |
| 108 | + std::cout << std::endl; |
| 109 | +
|
| 110 | + return 0; |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +Trying it with the flags: `-bi 100 option another_one? "quotes work too" --string="string"` will yield the output: |
| 115 | +``` |
| 116 | +a_bool_variable: true |
| 117 | +an_int_variable: 100 |
| 118 | +a_double_variable: 0 |
| 119 | +a_str_variable: string |
| 120 | +Options: option another_one? quotes work too |
| 121 | +``` |
| 122 | + |
| 123 | +# LICENSE |
| 124 | +Check `LICENSE.md`. |
| 125 | + |
| 126 | +# CONTRIBUTING |
| 127 | +Sure! Fork the repository, modify it, and then submit a pull request! The aim of this project is to be as quick to use as possible, so that developers won't need to build a giant library just to use a single feature. Hence, if possible, all contributions should stick to the theme of only using header (`.h` or `.hpp`) files! |
0 commit comments