Commit Graph

97 Commits

Author SHA1 Message Date
Sean Robinson
9eb1fe5cef Enable clang-tidy readability-magic-numbers check
Adds names for integer bases used with details::parse_number.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-02-07 14:29:25 -07:00
Sean Robinson
7cbc66f65b Return default_arguments from operator& of two default_arguments
operator& should return combined values of the same type, not a new type
(i.e. bool).  This is much more verbose, but easier to reason about
without implied conversion.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-02-07 14:27:06 -07:00
Sean Robinson
6a8b1318ec Enable clang-tidy readability-container-size-empty check
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-02-07 14:27:06 -07:00
Sean Robinson
158b8a0d2f Enable clang-tidy readability-braces-around-statements check
All tests still pass.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-02-07 13:10:03 -07:00
Sean Robinson
c0bbcf613c Remove sentry check in ArgumentParser::operator<<
cppcheck reports "Variable 'sen' is assigned a value that is never used.
[unreadVariable]" for this line.

As far as I understand, std::ostream::sentry is used to prepare access to
the stream buffer.  But, we are never directly accessing the stream
buffer.  Stream access in this function uses other operator<< functions.

Most noise in this patch is about unindenting after if() removal.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-11-10 13:54:35 -07:00
Sean Robinson
abb2206141 Declare lambda parameter and ArgumentParser::print_help as const
These were respectively reported as constParameter and functionConst
style issues by cppcheck.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-11-10 13:54:33 -07:00
Sean Robinson
6530a06747 Copy more members in ArgumentParser copy constructor
This showed as a cppcheck warning: uninitMemberVar.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-11-10 13:54:31 -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
748bc95cf5 Rename inner scope variables to differ from outer scope
These variables with the same name are not the same variables because of
scope rules.  While the compiler is not confused by this naming, it may
be less readable by someone attempting to edit this code.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-10-27 09:02:33 -07:00
Sean Robinson
500bc9277e Add ArgumentParser::mIsParsed to copied members
MSVC 19.16 appears to be doing a copy rather than a move in
test_const_correct.  The copy ctor does not handle mIsParsed, so the
initial false value is kept.  This commit adds copying mIsParsed during
copy construction.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-10-27 07:35:52 -07:00
Yoshihiro Hokazono
14abaa47d9 Use member initializer list to SizeRange ctor 2021-09-16 08:01:27 +09:00
Yoshihiro Hokazono
bec93acaa7 Avoid use ALL_CAPS for enumerators 2021-09-16 08:01:26 +09:00
Yoshihiro Hokazono
3d559d3a23 Revive remaining method
I reimplemented remaining() method for backward compatibility.

It consumes "all" remaining args.

So, I added mAcceptsOptionalLikeValue flag and handle it by using this
flag.

Currently, remaining() behavior is slightly different from the original when no
args are provided and get<Container<T>>() method is called.
Originally it raises an exception. But current implementation returns
an empty container instead of exception.

It is possible to implement complete backward compatibility by
referencing mAcceptsOptionalLikeValue flag and raises an exception in get() method,
but I did not do this.
I think that is too much.
2021-09-16 07:54:00 +09:00
Yoshihiro Hokazono
c6c3be04d3 1st version of handling variable length nargs
To handle variable length nargs, I replaced mNumArgs with mNumArgsRange.
I defined SizeRange class for mNumArgsRange, which has simply min and
max std::size_t member.

To concentrate on this big change, I tentatively deleted remaining
feature, which was originally implemented in the way that mNumArgs = -1
internally and maybe_args() -> Optional wrap method.

Library users may make use of 4 types of interface to set
mNumArgsRange.

1. nargs(std::size_t)
2. nargs(std::size_t, std::size_t)
3. nargs(SizeRange)
4. nargs(NArgsPattern)

1. is expected to behave same as original. This mthod sets min=max
SizeRange to mNumArgsRange, which is actually, not a range, but an
"exact" number.

2. sets min and max.

3. uses SizeRange class. This interface may be unnecessary. It is also
an option to delete this method and make SizeRange class internal.

4. is provided to set common patterns. In Python, they are "?", "*" and
"+". NArgsPattern is an enum class for type safety. std::string
interface is also an option to mimic Python argparse. char interface
would be ambiguous with 1.

Changes on consume method is important.
The parser tries to consume args until the count reaches mNumArgsRanges::max or
it meets another optional like string.
If consumed args count is under mNumArgsRanges::min, the parser fails.

Now, when the required number of arguments are not provided, the parser
will fail.
So, we have to take care of get() method as well.
get() failed when argument count is 0 and default value not provided.
But now there are 0..1 or 0..* nargs are OK.
So this behaviour has to be fixed.
When T is container_v, it returns empty container.

I implemented validate method so that it shows kind message.
2021-09-16 07:22:46 +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
Yoshihiro Hokazono
971f687a17 Kind error message 2021-09-13 07:08:37 +09:00
Yoshihiro Hokazono
8727c132fc get("out") and get("--out") are both avaibale 2021-09-13 07:07:20 +09:00
Sean Robinson
37dc4f20ec Revert "Use std::chars_format rather than local copy"
It's too early to use std::chars_format as there is not wide enough
support in stdlib implementations.  After the following stdlib become our
supported versions, this can be revisited.

GCC >= 10.1.0
Clang >= 7.0.0 (already our minimum)
MSVC >= 19.4

Reverts commit 1c61082a4c.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-08-26 06:55:36 -07:00
Sean Robinson
1c61082a4c Use std::chars_format rather than local copy
I believe the Supported Toolchains all now include <charconv> (and
std::chars_format) and we can use the stdlib-defined values.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-08-24 09:20:50 -07:00
Pranav
ccf3920ce2
Merge pull request #108 from Bedzior/const-correct-argument-parser
Const-correct ArgumentParser
2021-08-07 07:22:12 -05:00
Chuvi
a3f164bcc7 Replace size_t to std::size_t.
https://github.com/p-ranav/argparse/pull/109#issuecomment-892943775
2021-08-05 16:06:06 +03:00
Chuvi
0652496435
Use std::min type specialisation 2021-08-04 19:43:20 +03:00
Chuvi
14fd4c6d5b Fix std::min conflict with min/max definitions from windows.h 2021-08-04 13:54:46 +03:00
Rafal Bedzkowski
4ede429264 Const-correct ArgumentParser 2021-08-02 17:21:46 +02:00
Oliver 'kfsone' Smith
38b1b8114e Allow user to limit version argument to --version 2021-06-04 16:56:22 -07: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
Sean Robinson
0fe17e22f6 Replace integers with bool value keywords
While the implicit conversions from `1` to `true` work correctly, this
avoids the conversions.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-04-06 12:07:53 -07:00
Sean Robinson
e371313b87 Update copyright dates and authors
The full list of contributors is available in the git commit log.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-04-06 11:53:19 -07:00
Kenny Shen
c869f20e2b Qualify iterator functions 2021-03-25 12:55:26 +08:00
Mike Zozu
ed06438faf nicer usage text for required arg 2020-12-15 23:01:07 +03:00
Mike Zozu
51dd98f49d fix is_streamable trait 2020-12-15 21:59:52 +03:00
Mike Zozu
69c2cada35 simplified traits using std::void_t 2020-12-15 20:01:26 +03:00
Mike Zozu
b025166e4c prettier output in case of absence of arg help msg 2020-12-15 19:09:08 +03:00
Mike Zozu
6b30a65ffd special case for repr: bool value 2020-12-15 16:59:45 +03:00
Mike Zozu
5572cb0862 trying to fix strange AppleClang compiler error 2020-12-15 16:58:38 +03:00
Mike Zozu
6964cccd2f clang-format 2020-12-15 16:20:41 +03:00
Mike Zozu
282f9ebf91 show default values in help 2020-12-15 16:10:56 +03:00
Robert Kalinowski
bf12edd9a7 Fix help if required and def-value. Fixes #89.
Propose, skip text "[Required]" if argument has default value,
because the argument can be omitted in commend-line.
2020-11-30 19:53:06 +01:00
Joseph Durel
aef670bd43 Make ArgumentParser::add_*() functions working on the parser itself chainable 2020-07-11 10:12:57 -04:00
bufferbase
43f4629be5
Fix incorrect message when mUsedName is empty
Previously, it printed ": expected 1 argument(s). 0 provided." when one positional argument is defined but nothing is provided. Now it prints "1 argument(s) expected. 0 provided."
2020-06-04 17:06:20 -07: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
Pranav Srinivas Kumar
33101e7972 Closes #73 2020-05-08 14:35:05 -05:00