Commit Graph

617 Commits

Author SHA1 Message Date
Sean Robinson
6246a9df0e Change Static Analysis trigger event to pull_request_target
The GH security model restricts comment posting from PR actions.
StaticAnalysis has added support for pull_request_target to mitigate
risks while still allowing comments by the bot.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-01-11 06:48:00 -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
Sean Robinson
8d8282bac3 Add Static Analysis action to run on Pull Request
Unit test source files are not currently checked.  Hopefully, these can
be added so that all source files in a pull request are verified.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-11-10 13:54:29 -07:00
Sean Robinson
9a9c3042aa Add a clang-tidy configuration file
This file matches the current practices of the argparse project.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-11-10 13:54:27 -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
Pranav
4cacdc4b30
Merge pull request #147 from MaciejPatro/master
Improve thrown message in case of invalid argument.
2021-11-09 12:44:40 -06: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
Pranav
b9583b42ab
Merge pull request #146 from p-ranav/update_readme_stdout_to_stderr
Update readme stdout to stderr
2021-11-03 08:34:40 -05:00
Pranav Srinivas Kumar
6f1a90f101 Removed duplicate entry for Daniel Marshall 2021-11-03 08:31:09 -05:00
Ashwin Rohit
2e25423db8 Modify README.md to show printing errors to stderr instead of stdout 2021-11-03 08:29:56 -05:00
Pranav
9c729f54b1
Updated contributors 2021-10-31 17:34:51 -05:00
Pranav
10190c89dc
Merge pull request #144 from aashwinr/update_err
Modify README.md to show printing errors to stderr instead of stdout
2021-10-31 17:22:32 -05:00
Ashwin Rohit
2c55a2e423 Modify README.md to show printing errors to stderr instead of stdout 2021-10-31 12:29:19 -07:00
Pranav
b98bf25a34
Merge pull request #142 from skrobinson/wip-deactivatable-defaults
Actions with nonary arguments, removable defaults, and some clean ups
2021-10-27 12:52:29 -05:00
Sean Robinson
5cceb98e3c Update "Printing Help" documentation
Help output has changed format over time.  This updates the README
example to reflect current practice by running the example code
and copy-pasting its output.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2021-10-27 09:02:33 -07: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
58777d8c84 Replace spaces with underscores in example program names
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>
2021-10-27 09:02:33 -07:00
Sean Robinson
8772b37aab Update examples from exit() to std::exit()
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>
2021-10-27 09:02:33 -07:00
Pranav
c9e2958a6c
Merge pull request #143 from skrobinson/fix-copy-misparsed
Fix copying mIsParsed
2021-10-27 10:50:47 -05: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
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
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
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
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
Pranav
ab0a28c3bf
Merge pull request #132 from skrobinson/feat-improve-errors
Improve error messages
2021-09-14 15:03:48 -05: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
Pranav
b33ba4fb5d
Merge pull request #127 from hokacci/feature/flexible-get-syntax
Make get syntax flexible
2021-09-14 09:36:07 -05:00
Pranav
0f392ae057
Merge pull request #130 from skrobinson/fix-cmake-warning
Add CMake project name
2021-09-14 09:35:28 -05:00
Pranav
ffe619fe08
Merge pull request #129 from skrobinson/wip-gh-action-pr
Add GitHub Action to run tests on pull request
2021-09-14 09:35:14 -05:00
Sean Robinson
335e6272bd Add GitHub Action to run tests on pull request
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>
2021-09-13 14:17:19 -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
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
Pranav
b0cb28ab17
Merge pull request #115 from HydrArgs/cpack
Some cleanup in CPack packaging
2021-09-01 09:16:48 -05:00