Make handling command line options in C++ easy with a sane, simple API. A replacement for getopt_long.
This project is maintained by ryngonzalez
Make handling command line options in C++ easy with a sane, simple API. A replacement for getopt_long.
To use with your code, simply include option_handler.h
in your .cpp
, .hpp
, or .h
file.
On the command line you can allow your program to take in long or short options.
./myprogram --mode FASTMODE --stocks GOOG AAPL AMZN --statistics
This is equivalent:
./myprogram -m FASTMODE -s GOOG AAPL AMZN -t
Instantiate an option handler by passing in the command line input (argv
and argc
):
OptionHandler::Handler h = OptionHandler::Handler(argc, argv);
Add options with add_option
:
// Follows h.add_option(short_name, long_name, arg_type, multiple);
// For example:
h.add_option('m', "mode", OptionHandler::REQUIRED, false);
And options has these arguments:
short_name
is a char
long_name
is a std::string
arg_type
is one of
OptionHandler::NONE
OptionHandler::REQUIRED
OptionHandler::OPTIONAL
multiple
is a bool indicating whether an argument can take multiple arguments
multiple
is set to false
the last argument passed to the option is set as it's only argumentmultiple
is set to true
all arguments passed to the option are saved in a vector<string>
All accessors are called by passing in the desired option's long_name
.
To check whether an option has been used, call get_option
:
// get_option returns whether an option was set
bool exists = h.get_option("exists"),
help = h.get_option("help");
To get the last argument passed to an option, call get_argument
:
// get_argument returns a string passed to an option
std::string song = h.get_argument("song"),
movie = h.get_argument("movie");
To get a vector<string>
of all arguments passed to an option, call get_arguments
:
// get_arguments returns a vector of arguments (strings) passed to an option
std::vector<std::string> albums = h.get_arguments("albums"),
films = h.get_arguments("films");
Copyright (©) 2012 Ryan Gonzalez ryan@ionizedmedia.com & Haoran Ning hning@umich.edu
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.