Commit Graph

61 Commits

Author SHA1 Message Date
jpr89
c55726ee29
Cmake Revamp (#270)
* Cmake Revamp

I needed to do a variety of things to ensure cxxopts worked well in my own project.

I created a new cmake module to abstract a lot of the logic in the main CMakelists.txt, I think it really assists in the readability of the project. Consequently a lot of logic is now written in functions.

I made a lot of the project options off by default unless the project is being built standalone. As a frequent consumer of cmake libraries this is a huge issue. Since examples, tests, installation, etc. aren't things I expect/desired by default when using libraries.

Co-authored-by: Juan Ramos <juanr0911@gmail.com>
2021-01-17 12:11:02 +11:00
Daniel Lemire
12e496da3d
Making sure that the library can compile without warnings even when crazy pedantic flags are set (#238)
Makes some fixes to satisfy various strict warnings.
2020-10-06 17:06:33 +11:00
jarro2783
3ef9fddc7b
Fix passing a const array to parse (#258)
Fixes #257. The input array is not modified, so we can declare this as
`char const* const*`.
2020-10-03 20:54:06 +10:00
Jarryd Beck
fedf9d7b57 Refactor parser
Major refactor of the parsing code organisation to improve encapsulation
and not modify the input arguments. The returned result no longer has
pointers into the original option specification.
2020-10-01 17:12:03 +10:00
Kjetil Andresen
15e8a74e95
Support 'const char**' arguments in Options::parse (#250)
`cxxopts` doesn't modify the contents of the argv strings. This changes
the parse function to take a reference to a `const char**`.
2020-08-11 08:01:29 +10:00
Eyal Rozenberg
5f43f4cbfe
Fixes #245: Mention the option name when throwing on "no value" (#246)
* Fixes #245:

* Added a new exception type: `option_has_no_value_exception`; throwing it when an option has no value we can cast with `as()`, instead of an `std::domain_error`.
* The `OptionValue` type now holds a pointer to the long option name (in its corresponding key within ParseResults's `m_results` field.
2020-07-23 17:27:04 +10:00
Jarryd Beck
23f56e2614 Fix conversion warning in test 2019-11-28 08:32:54 +11:00
Jarryd Beck
fce82fb035 Fix duplicate default option
Fixes #197. Don't parse default options twice when there is a short and
long option.
2019-08-08 08:21:52 +10:00
Bartek
3c73d91c0b add_options variant with initializer list (#189)
Allow adding options with a single initializer list.
2019-06-28 08:39:37 +10:00
Jean-Baptiste Bayle
6b6af4f561 Add a method to remove the implicit value of an option (#178) 2019-06-18 17:49:15 +10:00
Christian Lang
7b14d5f60b Fix parsing of std::vector and add test, example and documentation (#182)
Improve parsing into std::vector so that a single argument can take a list.
2019-06-18 08:14:18 +10:00
Jarryd Beck
e17c6b0827 Fix integer parsing again 2019-06-14 18:20:22 +10:00
Jean-Baptiste Bayle
bd20573829 Parse 0 and 1 into booleans (#177)
* Parse 1 as "true" and 0 as "false" for boolean options.
2019-05-28 17:25:54 +10:00
Jarryd Beck
d58271c5fd Allow invalid short option syntax
Fixes #171. Allows invalid syntax for short options to be ignored.
2019-05-28 08:21:36 +10:00
Jarryd Beck
d31492e2cd Catch exception using reference 2019-05-02 17:32:48 +10:00
Jarryd Beck
5da5d67111 Throw exception on invalid positional argument
Fixes #169. It seems reasonable to throw an exception when an attempt is
made to parse into positional parameters that don't exist.
2019-05-01 18:34:29 +10:00
Jean Guyomarc'h
51f9a94c91 Fix invalid exception type in catch (#149)
g++ 8.2.0 refuses to compile the test suite because of polymorphic types
that are caught by value. They shall be caught by reference instead.
2018-11-20 08:06:18 +11:00
Jarryd Beck
84feb4bd87 Throw in ParseResult::as when option isn't present
Fixes #124.
2018-11-19 17:45:51 +11:00
Jarryd Beck
b528324107 Fix option matching 2018-11-08 08:18:54 +11:00
Jarryd Beck
7c468aaf68 Allow iterator inputs to parse_positional 2018-08-07 08:00:41 +10:00
Jarryd Beck
d47101a101 Allow leading zeroes in integers
Fixes #101. Allows leading zeroes in the integer parser.
2018-05-07 18:46:26 +10:00
Frank Schoenmann
ca6e9f70eb Allow unrecognised options. (#105)
Allows unrecognised options to be left alone by the parser without throwing an exception.
2018-04-12 08:15:07 +10:00
Jarryd Beck
cc4914f065 Revert "Added const to argv type for better interoperability. (#99)"
This reverts commit 0f819a5cab.
2018-04-10 08:48:08 +10:00
Jesús González
0f819a5cab Added const to argv type for better interoperability. (#99)
* Added const to argv type for better interoperability.
2018-03-09 08:07:29 +11:00
Jarryd Beck
76bd60dc17 Add support for std::optional
Fixes #93. This adds C++17 only support for `std::optional` values for
command line parameters.
2018-01-31 18:25:52 +11:00
Jarryd Beck
e792760ab9 Changes default values so that they aren't counted
Fixes #96. Default values of options not specified on the command line
had a `count` of 1. It would be better if they had a count of 0 because
they were not actually specified, so that count is only for options
given by the user.
2018-01-30 18:55:49 +11:00
Jesús González
76717cb3dd Add default "default" value to boolean options (#94)
* Add default "default" value of "false" to boolean options, therefore allowing to call result["boolOpt"].as<bool>() without throwing an exception.
2018-01-17 07:55:45 +11:00
Jarryd Beck
2ca68adeaf Fix positional arguments overload
Fixes #83.

Adds an overload for positional arguments taking a
`std::initializer_list`. When using an `initializer_list` with one
argument, the function call is ambiguous matching both `std::string`
and `std::vector<std::string>`.
2017-11-28 18:01:41 +11:00
Jarryd Beck
abe9ebd6b4 Fix handling of implicit values
Fixes #84. Implicit values are not handled very well. For example:

    --foo false true
    --foo --bar

In the first, `false` is an argument to `--foo`, and then `true` is a
positional argument. In the second, because of the hyphen in `--bar`, `--foo`
is parsed with its implicit value. This seems inconsistent and unintuitive.

Better is that implicit values *never* consume the next parameter to be
completely consistent. This means that values with an implicit parameter
*must* be specified using the `--option=value` form.
2017-11-28 08:43:55 +11:00
Jarryd Beck
0a49b82072 add a test for broken boolean options
The parsing for boolean options was broken by 6c9bae4a07 which added implicit
and default values, and the ability to parse boolean strings. Having an option
after the boolean tried to parse that into the boolean instead of as a
positional parameter.

See #84 for the bug report.
2017-11-27 08:36:34 +11:00
Jarryd Beck
8893afe13c Merge branch '2_0' 2017-11-15 18:04:20 +11:00
Jarryd Beck
6c9bae4a07 Parse strings for booleans
Fixes #54. Allow default and implicit values for booleans with multiple
boolean strings matched as values.
2017-11-15 17:51:38 +11:00
Jarryd Beck
d74a2c65a5 Add ability to iterate through parsed options
Fixes #74.
2017-11-13 08:10:22 +11:00
DevCodeOne
d7b930846c Fix some strange issues in integer_parser (#80)
* Prevent malformed numbers from being parsed as correct numbers.

Fixes #78. If you passed a string for example "test" it would get parsed to 1400.
The problem was that the parser did not throw an exception when an incorrect char was encountered.
Also a number without 0x in front with hexadecimal digits in it got parsed.
The number was treated as a hexadecimal number but it was still calculated with base 10.
So now before the current char is used, it is checked if it is valid in the current base.
Furthermore the number 0x0 was not a valid number, it now is a special case in the `integer_pattern`.

* Fixed `integer_pattern` so it works correctly under clang. Added testcase for invalid integers and for 0x0 being a valid number.
2017-11-02 08:14:21 +11:00
Jarryd Beck
b1f0cb806c fix default 2017-10-27 12:31:18 +11:00
Jarryd Beck
65beaeb2e9 test that fails 2017-10-27 08:47:08 +11:00
Jarryd Beck
acbce8e363 all tests pass 2017-10-27 08:38:29 +11:00
Jarryd Beck
8010e06952 Move parse result to an immutable object
This is far from ideal, but it's the first step in moving the parse
result to an immutable object so that the parser can be reused.
2017-10-18 18:37:58 +11:00
Jarryd Beck
70ceea57e3 Cleanup uses of inline 2017-10-18 08:07:10 +11:00
Hannah Rittich
2aed1ce41b Added parsing of general types. (#63)
Adds generic parsing of types using `operator>>`, in particular allows
parsing of floats and doubles.
2017-08-16 17:55:06 +10:00
Jarryd Beck
5e9f822598 Improve integer parsing
Fixes #39. Closes #40. This is an overhaul of the way that integer
arguments are parsed. Instead of using std::istream, which allows,
for example, negative integers for unsigned types, we use our own
parser.

This allows us to do proper range checking depending on the type,
and to correctly check for negative values passed to unsigned types.

This also allows the handling of base 16 numbers.
2017-07-25 08:12:29 +10:00
Shivakar Vulli
f931fd4279 Allow spaces in option specification. (#58)
Fixes #57.

Allows spaces after the comma between the short and long option
specification.
2017-07-16 15:11:03 +10:00
Mario Werner
11faadeba7 Cmake overhaul (#53)
* remove biicode support

The company/startup died in 2015 and the open source project seems
to be dead as well.

* define compiler warning flags globally instead of locally for every binary

* add support for cmake's find_package() (refs #52)

This rewrite of the main CMakeLists.txt cleans up the way ICU flags
are attached to the library target and adds the cmake helper files
(cxxopts-config.cmake, cxxopts-config-version.cmake,
cxxopts-targets.cmake) which are needed for exporting the cxxopts
target. Cmake's find_package command uses these files when the
library is consumed by another project.

Additionally, two new tests have been added which build the example
application via add_subdirectory and find_package.

* removed target_sources from interface library

Adding target_sources to interface libraries and exporting them is
not supported in CMake 3.1 and 3.2. Furthermore, since it is a header,
it is not needed at all.

* use the *_LDFLAGS instead of *_LIBARIES when linking ICU

The LIBRARIES variable seems to contain only the name and not the
full path.
2017-06-23 18:38:42 +10:00
Jarryd Beck
34aec8e87c Enable warnings and fix them
Fixes #50. This enables -Wall -Wextra -Wshadow and sets -Werror so that
the build fails if there are any warnings. All warnings that came up are
also fixed
2017-06-01 17:12:17 +10:00
Jarryd Beck
464a8eb65d Add required options helper
Fixes #44. Adds a helper function for checking required options.
2017-05-31 18:10:46 +10:00
Jarryd Beck
f3582c4864 missing file 2017-05-31 17:57:27 +10:00
Jarryd Beck
9d16b98edd Support only short option
Fixes #47. Adds support for options that only have a short option.
2017-05-31 17:40:14 +10:00
Jarryd Beck
cca5472def failing test 2017-05-17 07:38:30 +10:00
Jarryd Beck
848880d931 Fix arguments after -- without declared positional
Fixes #36. This fixes handling arguments passed after `--` when no
positional arguments have been declared, or when the positional
arguments have all been used up. The bug was that the extra arguments
were lost. Now they are retained in the modified argv.
2017-05-06 14:16:00 +10:00
Mario Badr
e005d07656 Use cmake interface library
Fixes #33.

This PR uses cmake's interface library feature:

  An INTERFACE library target does not directly create build output,
  though it may have properties set on it and it may be installed,
  exported and imported.

This makes it easier to include the header only library in a cmake
project. After using add_subdirectory on the cxxopts directory, one
simply needs to include cxxopts in their target_link_libraries, which
will allow the user's target to inherit the properties of the cxxopts
header library (see changes to example and test).
2016-12-03 07:50:14 +11:00