C++ library for reading command-line program options. Written by Greg Kikola.
Introduction
Option++ provides an easy way to read, validate, and process command-line options. It can also generate a detailed, properly formatted help message.
There is a single-header version of the library provided, or you can build and link to the shared library.
Features
- Supports the usual Unix and GNU/Linux conventions
- Use long (
--option
) and short (-o
) option names
- Double dash by itself (
--
) indicates end of options
- Options may have mandatory or optional arguments
- Can parse arguments passed to
main
directly, or can read options from a string
- Allows you to bind variables directly to options
- Input validation for numerical arguments
- Can automatically generate a help message
- Options can be separated into groups for better organization
- Easily iterate over all parsed data
- If desired, can iterate over only non-option arguments
- Can also iterate over only option arguments
- The strings that are used as option prefixes can be customized
- Exception-based error handling
Option Format
Both long and short option names are supported. For example:
myprogram --verbose -af file.txt
Each option may be set to take an optional or mandatory argument:
myprogram --output=file.txt
The equals sign is not required:
With short options, an argument may be written immediately after the option name:
'--
' can be provided by itself as an argument to indicate that all remaining command-line arguments should not be interpreted as options:
myprogram --this-is-an-option -- --this-is-not
Example Program
Here is a simple program that demonstrates how you can use Option++:
7 bool show_help {
false};
8 bool show_version {
false};
10 std::string output_file;
17 void print_version() {
18 std::cout <<
"My Program 1.0" << std::endl;
21 int main(
int argc,
char* argv[]) {
26 opt_parser[
"help"].short_name(
'?')
27 .description(
"Show help information")
28 .bind_bool(&settings.show_help);
30 .description(
"Show program version information")
31 .bind_bool(&settings.show_version);
32 opt_parser[
"verbose"].short_name(
'v')
33 .description(
"Display additional explanations")
34 .bind_bool(&settings.verbose);
35 opt_parser[
"output"].short_name(
'o')
36 .description(
"File to write output")
37 .bind_string(&settings.output_file);
43 result = opt_parser.parse(argc, argv);
45 std::cerr <<
"Error: " << e.what() << std::endl;
50 if (settings.show_help) {
52 std::cout <<
"This program does important stuff.\n\n"
53 <<
"My Program accepts the following options:\n"
54 << opt_parser << std::endl;
56 }
else if (settings.show_version) {
61 if (!settings.output_file.empty()) {
63 std::cout <<
"Writing output to '"
64 << settings.output_file
70 std::cout <<
"Writing output to standard out" << std::endl;
78 std::cout <<
"Received argument '"
79 << entry.original_text
See the Examples page for more examples.
Copyright
Copyright © 2017-2020 Greg Kikola. License BSL-1.0: Boost Software License version 1.0.
Option++ is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.