Commit Graph

79 Commits

Author SHA1 Message Date
ololuki
e84ab5f67c
Fix overflow error in integer_parser. (#417)
Fixes #290.
Checking for overflow should be done before integer overflows.
There are two checks:
(result > limit / base) is used for limits greater than rounded up to base,
e.g. for 65535 it will activate for 65540 and higher.
(result * base > limit - digit) is used for limit+1 to limit+n below
next base rounded number, e.g. 65536 up to 65539.
2024-02-01 16:51:32 +11:00
Jarryd Beck
7bf29108d5 Update catch2 to latest 2.x 2024-01-15 21:04:41 +11:00
Alexander Galanin
ddc695ebac
Don't split by list delimiter in positional lists (#398) 2023-06-15 07:34:26 +10:00
Nathaniel Brough
58daccc945
feat(fuzzer): Adds fuzz tests (#386) 2023-02-06 07:30:51 +11:00
Jarryd Beck
4a3d4dbfe0 Fixes #362.
Fix comparing iterators from a different container.
2023-02-01 06:56:03 +11:00
Jarryd Beck
32afbc6526 Fix default bool values
Fixes #382. Keep boolean values when parsing into a reference.
2022-12-15 07:08:03 +11:00
Sourabh Mehta
1dcb44e79a
Fix an additional space in help generated (#377) 2022-11-10 07:24:19 +11:00
Ryan Leary
2e3c6991d3
add . as valid char in option names (#358) 2022-09-29 07:11:32 +10:00
jarro2783
a10bd5233b
Fix values attached to short options (#360)
Fixes #357.
2022-08-13 07:51:41 +10:00
Eyal Rozenberg
e976f964c3
Fix issue #240: Multiple long option names / aliases (#349)
* Fixes #240: Multiple long option names / aliases

* We now use a vector of long option names instead of a single name
* When specifying an option, you can provide multiple names separated by commas, at most one of which may have a length of 1 (not necessarily the first specified name). The length-1 name is the single-hyphen switch (the "short name").
* Hashing uses the first long name
* Option help currently only uses the first long name.
2022-07-14 16:42:18 +10:00
Eyal Rozenberg
628dc9202b
Fixes #345, fixes #346: Exception code tweaks (#347)
* Fixes #345, fixes #346, regards #340: Put exceptions in a sub-namespace and renamed them accordingly.

* Also fixed some "loose ends" regarding namespace indentation which were missing in PR #350.

* Dropped `required_option_missing` as it is unused.
2022-07-12 19:45:58 +10:00
Long Deng
c59e0c132c
Solve arguments() showing empty keys when only short-only option exists (#318)
* Solve `arguments()` showing empty keys when there is only a short option.
2022-05-11 17:44:25 +10:00
Yannic Staudt
779c429b0e
Added instructions to use cxxopts with tipi to INSTALL (#325) 2022-03-22 22:01:25 +11:00
Jarryd Beck
4e208b95b7 Add a program function
Fixes 323. Adds a function to retrieve the program name.
2022-01-14 16:26:48 +11:00
Jarryd Beck
c74846a891 Fix short options adding into unmatched
Fixes #312.
2021-10-21 08:06:53 +11:00
Jarryd Beck
c641241a87 Fixes to #204. 2021-10-09 17:02:27 +11:00
Baojun Wang
4b7fccb5f2 Allow empty string to be valid positional arguments
Fixes #204.
2021-10-09 17:02:27 +11:00
RonxBulld
97a4d5511f
Support options like -j5. (#286)
* Support option value being attached after the option without a space in between. e.g. -j5
2021-05-06 08:46:40 +10:00
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