-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3180efc
Showing
3 changed files
with
610 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 s u d o _ | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# GetOptC++ | ||
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. | ||
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. | ||
|
||
# Usage | ||
Simply download `getoptc++.hpp`, and add it into your project. Include the file, and you are off to the races! | ||
|
||
By default, GetOptC++ operates in `STRICT` mode, meaning only commands like these are valid: | ||
``` | ||
./my-executable -a 0 --bee="Hello!" -c "hmm" --delta=90 OPTION1 OPTION2 OPTION3 OPTION4 | ||
``` | ||
Commands in the following form will be rejected: | ||
``` | ||
./my-executable -a=0 --bee "Hello!" -c="hmm" --delta 90 | ||
``` | ||
|
||
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: | ||
- `no_argv` | ||
- `impossible_case_reached` | ||
|
||
The errors are pretty self-explanatory. Known standard library errors thrown: | ||
- `std::out_of_range` - Happens when the user places many flags that require furthur input, but does not provide them | ||
- `std::invalid_argument` - Happens when a user places a string into an input that requires a number | ||
|
||
If there are more errors, please open an issue to inform us. | ||
|
||
## Examples | ||
```cpp | ||
#include "getoptc++.hpp" | ||
#include <iostream> | ||
|
||
// Example main function to use with getoptc++ | ||
int main(int argc, char** argv) { | ||
GetOpt go(argc, argv); //main arguments argc and argv. | ||
//A third argument, isFromSystem, also exists. That will | ||
//set whether argv comes from a caller-generated function | ||
//or system-generated function. Specifically, it controls | ||
//whether getoptc++ skips the first argument, which is | ||
//usually the executable of the program. | ||
//GetOpt is a type alias of __impl_GetOpt<int, char, double, std::string> | ||
//You can also do: | ||
//GetOpt go; | ||
//go.initialize(argc, argv); | ||
//They are equivalent statements. | ||
|
||
//The flagBoolean function is usually called like this. | ||
//The string argument is separated by commas, and they | ||
//state the different names the flag can have. | ||
//Note that the last name that is parsed will have precedence | ||
//over all the previous names. This means that calling | ||
//-b --boolean --alias at the same time will give --alias | ||
//the precedence. | ||
bool& a_bool_variable = *go.flagBoolean("-b,--boolean,--alias"); | ||
|
||
//By providing a second argument, the caller can control | ||
//the default value that the flag will contain. This is also | ||
//the value of the int should there be no such flag in existance. | ||
//If not set, the default values are: 0 or "". | ||
int& an_int_variable = *go.flagInt("-i,--integer", 10); | ||
double& a_double_variable = *go.flagFloating("-f,--floating,--float,--decimal", 0.0); | ||
std::string& a_str_variable = *go.flagString("-s,--string", ""); | ||
|
||
//Once you are done setting the flags, you call: | ||
go.readArgs(AS_IS); | ||
//This function returns a std::vector<std::string>, which contins the | ||
//flags that getoptc++ failed to parse. It is ok to ignore it. | ||
|
||
//This function gets the options parsed by the previous call to go.readArgs | ||
//The type it returns is std::vector<std:string> | ||
auto options = go.getOptions(); | ||
|
||
std::cout << "a_bool_variable: " << std::boolalpha << a_bool_variable << std::noboolalpha << std::endl; | ||
std::cout << "an_int_variable: " << an_int_variable << std::endl; | ||
std::cout << "a_double_variable: " << a_double_variable << std::endl; | ||
std::cout << "a_str_variable: " << a_str_variable << std::endl; | ||
|
||
std::cout << "Options: "; | ||
for (auto&& it : options) std::cout << it << " "; | ||
std::cout << std::endl; | ||
|
||
return 0; | ||
} | ||
``` | ||
and a version without comments because I know I write too much: | ||
```cpp | ||
#include "getoptc++.hpp" | ||
#include <iostream> | ||
int main(int argc, char** argv) { | ||
GetOpt go(argc, argv); | ||
bool& a_bool_variable = *go.flagBoolean("-b,--boolean,--alias"); | ||
int& an_int_variable = *go.flagInt("-i,--integer", 10); | ||
double& a_double_variable = *go.flagFloating("-f,--floating,--float,--decimal", 0.0); | ||
std::string& a_str_variable = *go.flagString("-s,--string", ""); | ||
go.readArgs(AS_IS); | ||
auto options = go.getOptions(); | ||
std::cout << "a_bool_variable: " << std::boolalpha << a_bool_variable << std::noboolalpha << std::endl; | ||
std::cout << "an_int_variable: " << an_int_variable << std::endl; | ||
std::cout << "a_double_variable: " << a_double_variable << std::endl; | ||
std::cout << "a_str_variable: " << a_str_variable << std::endl; | ||
std::cout << "Options: "; | ||
for (auto&& it : options) std::cout << it << " "; | ||
std::cout << std::endl; | ||
return 0; | ||
} | ||
``` | ||
|
||
Trying it with the flags: `-bi 100 option another_one? "quotes work too" --string="string"` will yield the output: | ||
``` | ||
a_bool_variable: true | ||
an_int_variable: 100 | ||
a_double_variable: 0 | ||
a_str_variable: string | ||
Options: option another_one? quotes work too | ||
``` | ||
|
||
# LICENSE | ||
Check `LICENSE.md`. | ||
|
||
# CONTRIBUTING | ||
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! |
Oops, something went wrong.