Commit Graph

163 Commits

Author SHA1 Message Date
Pranav
874b939f13
Merge pull request #331 from rouault/store_into
Add Argument::store_into() functions
2024-03-12 10:13:31 -04:00
Even Rouault
8784cc8ddf
Add Argument::store_into() functions
It is possible to bind arguments to a variable storing their value, as an
alternative to explicitly calling ``program.get<T>(arg_name)`` or ``program[arg_name]``

This is currently implementeted for variables of type ``bool`` (this also
implicitly calls ``flag()``), ``int``, ``double``, ``std::string`` and
``std::vector<std::string>``. If the argument is not specified in the command
line, the default value (if set) is set into the variable.

```cpp
bool flagvar = false;
program.add_argument("--flagvar").store_into(flagvar);

int intvar = 0;
program.add_argument("--intvar").store_into(intvar);

double doublevar = 0;
program.add_argument("--doublevar").store_into(doublevar);

std::string strvar;
program.add_argument("--strvar").store_into(strvar);

std::vector<std::string> strvar_repeated;
program.add_argument("--strvar-repeated").append().store_into(strvar_repeated);

std::vector<std::string> strvar_multi_valued;
program.add_argument("--strvar-multi-valued").nargs(2).store_into(strvar_multi_valued);
```
2024-03-12 12:50:22 +01:00
Even Rouault
e6e41d43c6
Add a ArgumentParser::add_hidden_alias_for() method
It is sometimes desirable to offer an alias for an argument, but without it
appearing it in the usage. For example, to phase out a deprecated wording of
an argument while not breaking backwards compatible. This can be done with
the ``ArgumentParser::add_hidden_alias_for()` method.

```cpp
argparse::ArgumentParser program("test");

auto &arg = program.add_argument("--suppress").flag();
program.add_hidden_alias_for(arg, "--supress"); // old misspelled alias
```
2024-03-12 12:08:40 +01:00
Pranav Srinivas Kumar
379ebb6b16 Bumped version to v3.0 ahead of release 2023-11-05 18:20:10 -06:00
Pranav Srinivas Kumar
f84fa8484a Marked copy and move constructors as deleted 2023-11-05 18:13:17 -06:00
Pranav
ef92a8523e
Merge pull request #302 from ismagilli/typo_in_readme
FIX: typo in README.txt
2023-11-05 07:59:32 -06:00
Alexey Ismagilov
634b1202b0 FIX: typo in README.txt 2023-11-05 16:05:55 +03:00
Alexey Ismagilov
33146122a2 NEW: suppress flag for subcommand
resolve #258
2023-11-05 15:23:15 +03:00
Pranav Srinivas Kumar
e0a095571f Updated README to include mutex_args feature 2023-11-04 15:41:48 -05:00
Pranav Srinivas Kumar
de4239483d Added logic and unit tests for the required flag in mutex_args 2023-11-04 15:36:01 -05:00
cobyj33
e6d2628723 Set argparse to not run tests or set up install if not top level 2023-10-31 21:09:12 -05:00
Pranav Srinivas Kumar
4d8cb2391b Added README entry for flag() 2023-10-27 16:38:53 -05:00
Pranav Srinivas Kumar
6d49d5ee1b Limiting choices support to string type or integer type 2023-10-27 10:35:04 -05:00
Pranav Srinivas Kumar
0b8d0e2426 Added support for integer type in choices 2023-10-27 10:28:54 -05:00
cobyj33
1a7a1dfd43 Change parse_args examples to catch const std::exception instead of std::runtime_error
parse_args can throw exceptions that are not based on std::runtime_error, and the
error message should show on all errors.
2023-10-25 20:33:24 -05:00
cobyj33
90945c5c95 Add to readme FetchContent section to automatically turn off building tests and samples 2023-10-25 19:12:11 -05:00
Pranav
557948f123
Update README.md 2023-03-29 11:54:38 -05:00
Sean Robinson
9377e0d3b2 Use return in place of exit() in README and samples
Only those places in the README where an error is explicitly found in the
main function have been updated.  Other uses of exit are left untouched as
there is not enough context to know if return will work in that location.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2023-03-29 08:42:12 -07:00
Sean Robinson
0ae3c7d919 Add exit_on_default_arguments parameter to ArgumentParser
Allows users to opt-out of std::exit call in default arguments without
needing to replace with new --help and --version arguments.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2023-03-22 09:07:45 -07:00
Sean Robinson
04ac1fe366 Refactor Parent Parsers documentation
This replaces the verbiage copied from the Python argparse documentation
and makes the code sample more concrete. This illustrates how to avoid the
multiple help output problem reported in #165.

Closes #165

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2023-01-17 13:57:59 -07:00
Sean Robinson
a5ab5b0ce8 Update minimum supported MSVC version
https://learn.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-170

Closes #228

Reported-by: @c0rn1ie
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2023-01-17 13:57:59 -07:00
Sean Robinson
ecba90a4eb Highlight default arguments and their default behavior
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-12-20 09:44:34 -07:00
Sean Robinson
5f22faa973 Add ArgumentParser::at to retrieve arguments and subparsers
This allows updating attached object properties without holding external
references to the various Argument and ArgumentParser objects.

Closes #227

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-11-29 13:43:16 -07:00
Sergey Podobry
8e6a36dd0d Add is_subcommand_used overload
It's useful for removing string literals duplication in a code.
2022-11-08 01:31:45 +02:00
Pranav
ed2953aa3d
Merge pull request #218 from skrobinson/feat-bool-argparser
Allow check if ArgumentParser has parsed values
2022-10-11 17:46:05 -05:00
Sean Robinson
f710bbdacf Add operator bool for ArgumentParser
This allows checking whether user input was processed into the parser
or any attached subparsers.

Closes #212

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-10-10 15:26:42 -07:00
Sean Robinson
0c83c631c5 Update CMake version in README example for FetchContent
FetchContent_MakeAvailable was added in CMake v3.14.  Also includes
end-of-line whitespace clean-ups.

Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2022-10-06 13:57:34 -07:00
Pranav Srinivas Kumar
997da92556 Bumped version to v2.9 2022-09-21 19:24:04 -07:00
Pranav
d6c3f3b704
Updated cmake command for building samples 2022-09-21 21:17:24 -05:00
Pranav Srinivas Kumar
6f1e89885e Added nargs to help output, added test samples 2022-09-21 18:48:11 -07:00
Pranav Srinivas Kumar
3b9df0b1e7 Added support for metavar and improved help/usage based on #187 2022-09-21 18:01:36 -07:00
Pranav
5a83edd3c4
Added build and test instructions 2022-09-21 13:10:14 -05:00
Pranav Srinivas Kumar
632ca2fcf8 Added prefix_chars and assign_chars to README #67 2022-09-21 09:53:30 -07:00
Pranav Srinivas Kumar
66730967aa Added support for custom prefix characters #67 2022-09-21 09:48:48 -07:00
Pranav
8c91f1a290
Added option=value example to table of contents 2022-09-21 09:24:17 -05:00
Pranav Srinivas Kumar
6dd2a3cf4b Added example to README for option=value support 2022-09-21 07:23:02 -07:00
Pranav Srinivas Kumar
6a3c6e06e6 Added 'Parse Known Args' section 2022-09-21 05:54:21 -07:00
Pranav Srinivas Kumar
4f10f378c5 Bumped version to v2.8 2022-09-20 21:21:06 -07:00
Pranav
c91fc8477a
Renamed section Subparsers -> Subcommands 2022-09-20 23:18:14 -05:00
Pranav
c879553fba
Fixed indentation in table of contents 2022-09-20 23:17:41 -05:00
Pranav Srinivas Kumar
793fbcde16 Added is_subcommand_used helper function 2022-09-20 21:15:58 -07:00
Pranav Srinivas Kumar
e35ce54028 Added example for subparser 2022-09-20 20:48:01 -07:00
Pranav Srinivas Kumar
6ed180f89e Updated copy constructor and added parser path to correctly print program name in help for subparsers 2022-09-20 20:45:32 -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
327e16c61b Bumped to version v2.7 2022-09-18 07:14:36 -07:00
Pranav Srinivas Kumar
e9099de7d4 Removed contributors list. See https://github.com/p-ranav/argparse/graphs/contributors for full list 2022-09-18 07:13:49 -07:00
Pranav
af282c0f68
Updated example to use std::string instead of cstring for default value 2022-09-07 08:30:22 -05:00
Adrian Schneider
17d567c283
Extend Readme with CMake FetchContent integration of argparse. 2022-07-09 17:04:02 +02:00
Pranav Srinivas Kumar
40a3888f15 Bumped version to v2.6 2022-06-22 10:59:52 -07:00
Yoshihiro Hokazono
acff046fc5 Use optional instead of zero_or_one 2022-06-22 09:24:51 +09:00