🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
7.4 KiB

2 years ago
2 years ago
2 years ago
  1. [![Build Status](https://travis-ci.org/jarro2783/cxxopts.svg?branch=master)](https://travis-ci.org/jarro2783/cxxopts)
  2. # Release versions
  3. Note that `master` is generally a work in progress, and you probably want to use a
  4. tagged release version.
  5. ## Version 3 breaking changes
  6. If you have used version 2, there are a couple of breaking changes in version 3
  7. that you should be aware of. If you are new to `cxxopts` you can skip this
  8. section.
  9. The parser no longer modifies its arguments, so you can pass a const `argc` and
  10. `argv` and expect them not to be changed.
  11. The `ParseResult` object no longer depends on the parser. So it can be returned
  12. from a scope outside the parser and still work. Now that the inputs are not
  13. modified, `ParseResult` stores a list of the unmatched arguments. These are
  14. retrieved like follows:
  15. ```cpp
  16. auto result = options.parse(argc, argv);
  17. result.unmatched(); // get the unmatched arguments
  18. ```
  19. # Quick start
  20. This is a lightweight C++ option parser library, supporting the standard GNU
  21. style syntax for options.
  22. Options can be given as:
  23. --long
  24. --long=argument
  25. --long argument
  26. -a
  27. -ab
  28. -abc argument
  29. where c takes an argument, but a and b do not.
  30. Additionally, anything after `--` will be parsed as a positional argument.
  31. ## Basics
  32. ```cpp
  33. #include <cxxopts.hpp>
  34. ```
  35. Create a `cxxopts::Options` instance.
  36. ```cpp
  37. cxxopts::Options options("MyProgram", "One line description of MyProgram");
  38. ```
  39. Then use `add_options`.
  40. ```cpp
  41. options.add_options()
  42. ("d,debug", "Enable debugging") // a bool parameter
  43. ("i,integer", "Int param", cxxopts::value<int>())
  44. ("f,file", "File name", cxxopts::value<std::string>())
  45. ("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false"))
  46. ;
  47. ```
  48. Options are declared with a long and an optional short option. A description
  49. must be provided. The third argument is the value, if omitted it is boolean.
  50. Any type can be given as long as it can be parsed, with operator>>.
  51. To parse the command line do:
  52. ```cpp
  53. auto result = options.parse(argc, argv);
  54. ```
  55. To retrieve an option use `result.count("option")` to get the number of times
  56. it appeared, and
  57. ```cpp
  58. result["opt"].as<type>()
  59. ```
  60. to get its value. If "opt" doesn't exist, or isn't of the right type, then an
  61. exception will be thrown.
  62. Note that the result of `options.parse` should only be used as long as the
  63. `options` object that created it is in scope.
  64. ## Unrecognised arguments
  65. You can allow unrecognised arguments to be skipped. This applies to both
  66. positional arguments that are not parsed into another option, and `--`
  67. arguments that do not match an argument that you specify. This is done by
  68. calling:
  69. ```cpp
  70. options.allow_unrecognised_options();
  71. ```
  72. and in the result object they are retrieved with:
  73. ```cpp
  74. result.unmatched()
  75. ```
  76. ## Exceptions
  77. Exceptional situations throw C++ exceptions. There are two types of
  78. exceptions: errors defining the options, and errors when parsing a list of
  79. arguments. All exceptions derive from `cxxopts::OptionException`. Errors
  80. defining options derive from `cxxopts::OptionSpecException` and errors
  81. parsing arguments derive from `cxxopts::OptionParseException`.
  82. All exceptions define a `what()` function to get a printable string
  83. explaining the error.
  84. ## Help groups
  85. Options can be placed into groups for the purposes of displaying help messages.
  86. To place options in a group, pass the group as a string to `add_options`. Then,
  87. when displaying the help, pass the groups that you would like displayed as a
  88. vector to the `help` function.
  89. ## Positional Arguments
  90. Positional arguments can be optionally parsed into one or more options.
  91. To set up positional arguments, call
  92. ```cpp
  93. options.parse_positional({"first", "second", "last"})
  94. ```
  95. where "last" should be the name of an option with a container type, and the
  96. others should have a single value.
  97. ## Default and implicit values
  98. An option can be declared with a default or an implicit value, or both.
  99. A default value is the value that an option takes when it is not specified
  100. on the command line. The following specifies a default value for an option:
  101. ```cpp
  102. cxxopts::value<std::string>()->default_value("value")
  103. ```
  104. An implicit value is the value that an option takes when it is given on the
  105. command line without an argument. The following specifies an implicit value:
  106. ```cpp
  107. cxxopts::value<std::string>()->implicit_value("implicit")
  108. ```
  109. If an option had both, then not specifying it would give the value `"value"`,
  110. writing it on the command line as `--option` would give the value `"implicit"`,
  111. and writing `--option=another` would give it the value `"another"`.
  112. Note that the default and implicit value is always stored as a string,
  113. regardless of the type that you want to store it in. It will be parsed as
  114. though it was given on the command line.
  115. Default values are not counted by `Options::count`.
  116. ## Boolean values
  117. Boolean options have a default implicit value of `"true"`, which can be
  118. overridden. The effect is that writing `-o` by itself will set option `o` to
  119. `true`. However, they can also be written with various strings using `=value`.
  120. There is no way to disambiguate positional arguments from the value following
  121. a boolean, so we have chosen that they will be positional arguments, and
  122. therefore, `-o false` does not work.
  123. ## `std::vector<T>` values
  124. Parsing of list of values in form of an `std::vector<T>` is also supported, as long as `T`
  125. can be parsed. To separate single values in a list the definition `CXXOPTS_VECTOR_DELIMITER`
  126. is used, which is ',' by default. Ensure that you use no whitespaces between values because
  127. those would be interpreted as the next command line option. Example for a command line option
  128. that can be parsed as a `std::vector<double>`:
  129. ~~~
  130. --my_list=1,-2.1,3,4.5
  131. ~~~
  132. ## Options specified multiple times
  133. The same option can be specified several times, with different arguments, which will all
  134. be recorded in order of appearance. An example:
  135. ~~~
  136. --use train --use bus --use ferry
  137. ~~~
  138. this is supported through the use of a vector of value for the option:
  139. ~~~
  140. options.add_options()
  141. ("use", "Usable means of transport", cxxopts::value<std::vector<std::string>>())
  142. ~~~
  143. ## Custom help
  144. The string after the program name on the first line of the help can be
  145. completely replaced by calling `options.custom_help`. Note that you might
  146. also want to override the positional help by calling `options.positional_help`.
  147. ## Example
  148. Putting all together:
  149. ```cpp
  150. int main(int argc, char** argv)
  151. {
  152. cxxopts::Options options("test", "A brief description");
  153. options.add_options()
  154. ("b,bar", "Param bar", cxxopts::value<std::string>())
  155. ("d,debug", "Enable debugging", cxxopts::value<bool>()->default_value("false"))
  156. ("f,foo", "Param foo", cxxopts::value<int>()->default_value("10"))
  157. ("h,help", "Print usage")
  158. ;
  159. auto result = options.parse(argc, argv);
  160. if (result.count("help"))
  161. {
  162. std::cout << options.help() << std::endl;
  163. exit(0);
  164. }
  165. bool debug = result["debug"].as<bool>();
  166. std::string bar;
  167. if (result.count("bar"))
  168. bar = result["bar"].as<std::string>();
  169. int foo = result["foo"].as<int>();
  170. return 0;
  171. }
  172. ```
  173. # Linking
  174. This is a header only library.
  175. # Requirements
  176. The only build requirement is a C++ compiler that supports C++11 features such as:
  177. * regex
  178. * constexpr
  179. * default constructors
  180. GCC >= 4.9 or clang >= 3.1 with libc++ are known to work.
  181. The following compilers are known not to work:
  182. * MSVC 2013
  183. # TODO list
  184. * Allow unrecognised options.