Commit Graph

43 Commits

Author SHA1 Message Date
Sean Robinson
97993666ab Add tests for ArgumentParser::get
These test the API shown in README.md, rather than the Argument::get
function that does most of the work.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-09-14 09:46:22 -07:00
Sean Robinson
b6cedf4d56 Add test case for missing argument value
While it's good to test around error conditions, the main purpose of this
test is to catch future changes in the error type and message.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-09-14 09:44:39 -07:00
Sean Robinson
67d56afef8 Add test case for missing expected positional argument
This fills a tiny gap in the positional_arguments suite.  Most existing
tests have positional arguments.  The one case without an argument uses
Argument::remaining so that ArgumentParserArgument::parse_args does not
throw, instead ArgumentParser::get<> throws std::logic_error.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-09-14 09:44:35 -07:00
Sean Robinson
2f59260e84 Add CMake project name
This silences the following warning:

  No project() command is present.  The top-level CMakeLists.txt file must
  contain a literal, direct call to the project() command.  Add a line of
  code such as

    project(ProjectName)

  near the top of the file, but after cmake_minimum_required().

  CMake is pretending there is a "project(Project)" command on the first
  line.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-09-09 13:43:21 -07:00
Sean Robinson
a8900c2019 Replace simple type-converting Argument.action with Argument.scan in tests
Argument.scan handles simple string to numeric type conversions, removing
the need to create a lambda.  Argument.action is still necessary for more
complex conversions and those are left unchanged.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-08-24 09:25:49 -07:00
Rafal Bedzkowski
5841bca894 Introduce separate const-correctness test 2021-08-03 22:12:05 +02:00
Rafal Bedzkowski
4ede429264 Const-correct ArgumentParser 2021-08-02 17:21:46 +02:00
Sean Robinson
3efd045ea9 Add ArgumentParser.is_used to discern user-supplied values from defaults
.present returns std::nullopt if the optional argument is not given by the
user -- as long as a .default_value is not defined.  With a .default_value,
.present cannot be used to determine if a value is user-provided or the
default.

.is_used fills that role and only returns true if the argument was passed
by the user.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-04-07 14:09:10 -07:00
Sean Robinson
54d3cda804 Add Argument.append method to allow repeated argument use
The default behavior with optional arguments is to allow only a single use
per invocation.  One alternative is to use .nargs, but this requires
previously knowing, and limiting, the quantity of values.  The .append
method removes the restriction on repeats for a single Argument.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-04-07 11:47:09 -07:00
Mike Zozu
4de9f89b59 add tests for repr() and extend tests for help msg 2020-12-15 19:05:18 +03:00
Pranav Srinivas Kumar
f3e65f69a9 Added --version as a special flag similar to --help
* ArgumentParser takes a second argument - the program version (std::string)
* Using --version or -v will print the program version and exit
2020-05-15 19:06:00 -05:00
Zhihao Yuan
15e4d44f11
Avoid use of cmd.exe in Travis 2020-05-12 22:42:57 -05:00
Pranav Srinivas Kumar
33101e7972 Closes #73 2020-05-08 14:35:05 -05:00
Zhihao Yuan
e6c6c9b31c
Get arguments in optional<T> with .present<T>()
fixes: p-ranav/argparse#66
2019-12-01 02:14:13 -06:00
Zhihao Yuan
e8a44d289d
Parse floating-point numbers in .scan
fixes: p-ranav/argparse#63
2019-11-26 00:11:21 -06:00
Zhihao Yuan
426a5dbb76
Parse integers in the .scan fluent interface 2019-11-25 23:45:55 -06:00
Zhihao Yuan
964790cf3c
Determine negative numeric values with a grammar
Two differences that diverge from the existing behavior:

  1. Leading zeros are not allowed for integers.  Negative
     octal numbers such as `-066` are not meant to be treated
     as positional arguments, but existing code recognize them
     as decimal numbers.  Note that negative floating-point
     numbers with leading zeros (`-003.`) are unambiguous and
     are recognized.
  2. Inf and NaN are not recognized.  This is because options
     like `-inf` is indistinguishable from a compound argument
     that meant to be a shorthand for `-i -n -f`.

fixes: p-ranav/argparse#55
2019-11-24 00:31:05 -06:00
Zhihao Yuan
77f3bd9b8d
Default --help to print help and exit
The change also fixes a rare bug introduced in 9007958:
when there are duplicated keys, insert_or_update overrides
previously inserted ones, emplace doesn't.

fixes: p-ranav/argparse#59
2019-11-22 16:54:58 -06:00
Zhihao Yuan
1af8b826c8
Annotate test cases with doctest::test_suite
This change also explicitly lists the source files
for CMake.  This is because `GLOB` does not remove
old files when switching branches in Git, and
`CONFIGURE_DEPENDS` will add unstaged files after
stashing.

See also: https://cmake.org/cmake/help/latest/command/file.html#glob
2019-11-21 14:24:50 -06:00
Pranav Srinivas Kumar
d5f48fa204 Minor test cases 2019-11-21 10:28:34 -06:00
Pranav Srinivas Kumar
c707bcc1c4 Closes #56 2019-11-21 10:25:41 -06:00
Pranav Srinivas Kumar
f9ab33d1a6 Closes #53 2019-11-21 09:08:49 -06:00
Zhihao Yuan
991df83d97
Support capturing remaining() arguments
This kind of argument works as if having the "remaining" nargs,
inspired by Python's `argparse.REMAINDER`.

This change also reduces the size of `Argument` by 8 bytes.

See also: https://docs.python.org/2/library/argparse.html#nargs

fixes: p-ranav/argparse#17
2019-11-20 17:47:20 -06:00
Zhihao Yuan
8201a18568
Fix various issues in Argument constructor
Before this change:

1. When the input is built-in string literal or cv-`char*`,
   `is_optional` constructs temporary `std::string` while
   `mNames` initializer is also constructing `std::string`
   due to the use of `std::initializer_list`.
2. When the input is `std::string_view`, doesn't compile.
3. When the input is `std::string`, `mNames` initializer
   moves `args`.  If argument name is longer than
   `std::string`'s SSO buffer, bad thing will happen because
   `is_optional` will be accessing `args` in moved-from
   states.

Because of the use of `strtol` which expects nul-terminated
input, `is_*` series functions must take `std::string`.  This
restriction may be removed after AppleClang adds `<charconv>`.
But for now, it complicates the patch.  My solution is to
create an array prvalue still, but use a array reference
rather than `std::initializer_list` to refer to it, so that
the code in delegated constructor can keep using fold
expressions after the necessary `std::string` objects being
created.
2019-11-17 01:51:36 -06:00
Zhihao Yuan
f84f17d719
Give ArgumentParser value semantics
fixes: p-ranav/argparse#50
2019-11-16 15:15:54 -06:00
Zhihao Yuan
3f949fc3f1
Stop sharing argument values with parent parsers
See also: p-ranav/argparse#50
2019-11-16 14:33:00 -06:00
Zhihao Yuan
f6e686b69c
Bind extra arguments to actions
closes: p-ranav/argparse#38
2019-11-14 01:30:04 -06:00
Zhihao Yuan
dc227448f6
Allow actions that return void
closes: p-ranav/argparse#39
2019-11-13 12:54:07 -06:00
Pranav Srinivas Kumar
883d6c75d5
Merge branch 'master' into help-stream 2019-11-13 08:32:19 -06:00
Pranav Srinivas Kumar
c4256afd4b Added test case for issue #37 2019-11-13 08:25:07 -06:00
Pranav Srinivas Kumar
39618d25a1 Added .clang-format. Closes #43 2019-11-13 08:24:51 -06:00
Zhihao Yuan
9d66976421
Print ArgumentParser help with stream insertion 2019-11-13 02:17:28 -06:00
Mio
3c7fcad146 Fix typo 2019-08-07 19:58:32 +08:00
Mio
466e3aa37c Include new tests 2019-08-07 19:58:25 +08:00
Mio
34165a8ed7 Add corresponding tests for required arguments 2019-08-07 19:53:35 +08:00
Pranav Srinivas Kumar
af1af7d5b7 Unit tests to parse floating point numbers in E-notation. Issue #24 2019-06-06 21:37:36 -04:00
Pranav Srinivas Kumar
73f6aa7538 Fixes Issue #24 2019-06-06 21:24:32 -04:00
Stephan van Veen
9e7b80034e Throw exception in case of unknown argument 2019-05-20 22:21:57 +02:00
Stephan van Veen
36bdfe4a55 Change test cases 2019-05-20 22:21:57 +02:00
Stephan van Veen
94ca8e2552 Unify container operations 2019-05-12 17:17:21 +02:00
Pranav Srinivas Kumar
3ea4c79137 Fixes core dump on optional arg (issue #10) 2019-05-04 11:49:30 -04:00
Pranav Srinivas Kumar
4b59010bd3
Update README.md 2019-04-01 22:03:59 -04:00
Pranav Srinivas Kumar
bb4a2dbba7 Renamed directories 2019-04-01 22:01:40 -04:00