Option++  2.0
C++ library for reading command-line options
Option++

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:

myprogram -o file.txt

With short options, an argument may be written immediately after the option name:

mycompiler -DMY_DEFINE

'--' 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++:

1 #include <iostream>
2 #include <string>
3 
4 #include <optionpp/optionpp.hpp>
5 
6 struct Settings {
7  bool show_help {false};
8  bool show_version {false};
9  bool verbose {false};
10  std::string output_file;
11 };
12 
13 using optionpp::parser;
16 
17 void print_version() {
18  std::cout << "My Program 1.0" << std::endl;
19 }
20 
21 int main(int argc, char* argv[]) {
22  Settings settings;
23  parser opt_parser;
24 
25  // Set up options
26  opt_parser["help"].short_name('?')
27  .description("Show help information")
28  .bind_bool(&settings.show_help);
29  opt_parser["version"]
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);
38 
39  parser_result result;
40 
41  // Parse options
42  try {
43  result = opt_parser.parse(argc, argv);
44  } catch(const optionpp::error& e) {
45  std::cerr << "Error: " << e.what() << std::endl;
46  return 1;
47  }
48 
49  // Show help/version info
50  if (settings.show_help) {
51  print_version();
52  std::cout << "This program does important stuff.\n\n"
53  << "My Program accepts the following options:\n"
54  << opt_parser << std::endl;
55  return 0;
56  } else if (settings.show_version) {
57  print_version();
58  return 0;
59  }
60 
61  if (!settings.output_file.empty()) {
62  if (settings.verbose)
63  std::cout << "Writing output to '"
64  << settings.output_file
65  << "'" << std::endl;
66 
67  // Write output...
68  } else {
69  if (settings.verbose)
70  std::cout << "Writing output to standard out" << std::endl;
71 
72  // Write output...
73  }
74 
75  // Iterate over non-option arguments
76  for (const auto& entry : non_option_const_iterator(result)) {
77  if (!entry.is_option)
78  std::cout << "Received argument '"
79  << entry.original_text
80  << "'" << std::endl;
81  }
82 
83  return 0;
84 }

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.

optionpp.hpp
Main include file for library users.
optionpp::error
Base class for library exceptions.
Definition: error.hpp:36
optionpp::parser_result
Holds data that was parsed from the program command line.
Definition: parser_result.hpp:152
optionpp::parser
Parses program options.
Definition: parser.hpp:87
optionpp::non_option_const_iterator
result_iterator< const parser_result, const parsed_entry *, const parsed_entry &, false > non_option_const_iterator
const_iterator over non-option entries in a parser_result.
Definition: result_iterator.hpp:184