Commit Graph

73 Commits

Author SHA1 Message Date
Pranav Srinivas Kumar
616062e2bf Added -Wshadow and -Wconversion to CXX_FLAGS and fixed warnings 2022-09-21 07:43:46 -07:00
Pranav Srinivas Kumar
6dd2a3cf4b Added example to README for option=value support 2022-09-21 07:23:02 -07:00
Pranav Srinivas Kumar
20095a697a Added option=value support using #185 2022-09-21 06:54:34 -07:00
Pranav Srinivas Kumar
14287af007 Closes #94 2022-09-21 06:23:42 -07:00
Pranav Srinivas Kumar
6e23d5b22e Closes #181 2022-09-21 05:47:47 -07:00
Pranav Srinivas Kumar
793fbcde16 Added is_subcommand_used helper function 2022-09-20 21:15:58 -07:00
Pranav Srinivas Kumar
6c7da857b6 Added table of contents to README and clang formatted code 2022-09-20 20:27:24 -07:00
Pranav Srinivas Kumar
2f2858a8a7 Draft implementation of subparsers #42 2022-09-20 19:54:29 -07:00
Yoshihiro Hokazono
7b5084c454 Fix a typo 2022-06-22 07:16:41 +09:00
Yoshihiro Hokazono
b869b5a209 NArgsPattern -> nargs_pattern (to snake case) 2022-06-22 07:07:59 +09:00
Yoshihiro Hokazono
08943f47ab Merge branch 'master' into feature/variable-length-nargs 2022-06-21 08:36:33 +09:00
Yoshihiro Hokazono
12fcae66a7 Prefer pre-const to post-const 2022-06-21 06:48:13 +09:00
Yoshihiro Hokazono
6dfaa1c20c Restore a "remaining" test case for compat 2022-06-21 06:44:29 +09:00
Ameya Vikram Singh
f56aec307f Updates doctest from v2.3.5 to v2.4.8
Fixes the build error on latest GNU/Linux Systems: (https://github.com/doctest/doctest/issues/473)

Signed-off-by: Ameya Vikram Singh <ameya.v.singh@gmail.com>
2022-04-16 20:35:37 +05:30
Sean Robinson
97204363b5 Refactor configuration for StaticAnalysis
Upstream StaticAnalysis looks to be changing for more flexibility with
source files to process.  While these changes may eventually benefit
argparse, the public interfaces are in flux and we need a stable tool.
argparse also needs a SA change which is not yet upstream.

Trying to run clang-tidy via StaticAnalysis on a single file in a
directory with many source file is not easy, so move the analysis kernel
to a location (i.e. tools) where it is the only C++ source file.

Another benefit is cppcheck no longer needs to be told to ignore the test
sources.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-02-07 11:51:22 -07:00
Sean Robinson
18a229e849 Add a minimal binary to generate compile_commands.json file
clang-tidy needs compile_commands.json to generate a set of source files
to parse.  tidy-base is not built but is used as a source file in which
included headers can be parsed.  Without this process, clang-tidy will
process many other source files (e.g. doctest.hpp) that we do not want
to worry about.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-11-10 13:54:25 -07:00
Maciej Patro
87afaba6ba Improve thrown message in case of invalid argument.
Now message contains information which argument is the source of error.
It's easier to spot typo/understand which part of more complex command
is the source of problem.
2021-11-05 09:56:17 +01:00
Sean Robinson
ea1f7ef663 Allow removal of default arguments (i.e. --help and --version)
The help and version arguments are still included by default, but which
default arguments to include can be overridden at ArgumentParser creation.

argparse generally copies Python argparse behavior.  This includes a
default `--help`/`-h` argument to print a help message and exit.  Some
developers using argparse find the automatic exit to be undesirable.

The Python argparse has an opt-out parameter when constructing an
ArgumentParser.  Using `add_help=False` avoids adding a default `--help`
argument and allows the developer to implement a custom help.

This commit adds a similar opt-out to our C++ argparse, but keeps the
current behavior as the default.  The `--help`/`-h` and `--version`/`-v`
Arguments handle their own output and exit rather than specially treating
them in ArgumentParser::parse_args_internal.

Closes #119
Closes #138
Closes #139

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-10-27 09:02:33 -07:00
Sean Robinson
2b05334a3c Run Argumnet::action functor for zero-parameter arguments
Previously, only arguments with one or more parameters would run actions.
But, at times it can be useful to run an action when an argument does not
expect any parameters.

Closes #104

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-10-27 09:02:33 -07:00
Sean Robinson
af6bbc1d47 Skip --version test until it can be made to work
Because program.parse_args( { "test", "--version" }) calls std::exit(0),
the REQUIRE line never runs and this test is less useful.  Because tests
execution stops here, the doctest status report is not output.  If
--version can be made to not exit during this test, then the test could
be restored.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-10-27 07:33:22 -07:00
Yoshihiro Hokazono
bec93acaa7 Avoid use ALL_CAPS for enumerators 2021-09-16 08:01:26 +09:00
Yoshihiro Hokazono
8845260885 Add test for reversed order nargs 2021-09-16 08:01:25 +09:00
Yoshihiro Hokazono
bb115c0419 Add test for variable nargs 2021-09-16 08:01:24 +09:00
Yoshihiro Hokazono
aae2e9347a Add another test for remaining 2021-09-16 08:01:23 +09:00
Yoshihiro Hokazono
2cfe115dfb Change test for remaining to test for * nargs 2021-09-16 08:01:22 +09:00
Yoshihiro Hokazono
10ddd393b6 Fix tests for remaining 2021-09-16 08:01:20 +09:00
Yoshihiro Hokazono
c99272b93d Remove negative parameter test for now unsigned 2021-09-16 08:01:13 +09:00
Sean Robinson
f0d68de134 Confirm arguments are parsed before allowing ArgumentParser::get
If the developer forgot to call ArgumentParser::parse_args<>, attempts to
use ::get, ::present, etc., would raise "No value provided...".  With this
change, the error better describes what went wrong.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-09-14 09:55:03 -07:00
Sean Robinson
6344b5dcc7 Add argument name in exception thrown by Argument::consume
Here, the user gave an argument name but failed to provide the required
parameters to the argument.  Tell the user which argument wants more.

This is an API change that may affect programs trying to match the
specific "Too few arguments" message.  The new error message appends the
user-supplied argument that caused the error.

A solution which works with both versions is to look for "Too few
arguments" at the beginning of the error message.

- if (err.what() == "Too few arguments")
+ if (std:string(err.what()).rfind("Too few arguments", 0) == 0)

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-09-14 09:54:43 -07:00
Sean Robinson
1c2fd8726d Add argument name in exception thrown by Argument::get
As the user did not include the argument, the longest name for the unused
argument is in the last position of mNames.

This is an API change that may affect programs trying to match the
specific "No value provided" message.  The new error message appends the
argument that caused the error.

A solution which works with both versions is to look for "No value
provided" at the beginning of the error message.

- if (err.what() == "No value provided")
+ if (std:string(err.what()).rfind("No value provided", 0) == 0)

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-09-14 09:51:10 -07:00
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