🛠️🐜 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.

224 lines
6.3 KiB

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