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>
These examples give a false impression that a space in the middle of the
application name is well handled. While a space might be possible, it
must be escaped in shells, i.e. a common environment for a CLI argument
parser.
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
argparse seems to use the "std::" qualifier for std namespace members,
continue that in the documentation.
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>
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>
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.
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.
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>
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>
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>
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>
Run tests on Apple macOS, MS Windows, and Ubuntu Linux hosts.
macOS-hosted tests are only run on latest, which is currently macOS 10.15.
GH will eventually update latest to macOS 11 and we can look at expanding
to two versions of macOS.
Windows-hosted tests run on Windows Server 2016 and latest.
Ubuntu-hosted tests only run on latest because g++ fails to build argparse
on ubuntu-18.04 (GCC 8) since charconv was added in commit ea2f16d2. But,
Ubuntu-hosted tests do run with g++ and clang++.
Closes#128.
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
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>
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>
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>
IMHO, the .scan function and its supporting functions are beautiful
metaprogramming that needs to be more visible to argparse users. As a
start, document the function, with simple examples.
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>