Also converts most C-style arrays to a std::array onjects. The check is
disabled for ArgumentParser::parse_args(int, const char *const[]) as this
is a helper function to convert away from a common input format.
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
A common pattern in the previous code was goto/return/throw if a condition
is true, else goto/return/throw something different. The new pattern
uses the fact that the second goto/return/throw is only reachable when the
first goto/return/throw is not called.
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
Following the clang-tidy suggested fix in consume_digits causes compile
failures with MSVC 19.29 in our CI.
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
Adds names recommended by clang-tidy (e.g. "unused").
Note that clang-tidy v12 appears to detect unnamed parameters in lambdas,
while clang-tidy v13 does not.
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
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>
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>
These were respectively reported as constParameter and functionConst
style issues by cppcheck.
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
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.
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#119Closes#138Closes#139
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
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>
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>
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>
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>
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>
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>
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>
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>
.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>
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>
While the implicit conversions from `1` to `true` work correctly, this
avoids the conversions.
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
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."