Commit Graph

129 Commits

Author SHA1 Message Date
Even Rouault
9b4c63a2d8
usage(): do not emit blank line when a single argument exceeds set_usage_max_line_width() 2025-01-20 15:55:01 +01:00
Even Rouault
097bac1854
subparser: use full parser path instead of just parser name in usage() message 2024-11-08 20:49:15 +01:00
Muhammad Hussein
43072b8e8c Fix range of choices bug
- Support range of choices.
- Add testcases for range of choices scenario.

fix #370
2024-09-03 18:08:50 +03:00
Even Rouault
84b7b46ce5
Add argument name after 'Too few arguments' error
If a non-positional argument doesn't get the number of required values,
a generic "Too few arguments" error is generated, without its name, when
it is not the last argument (but if it is the last argument, we get its
name)
2024-05-27 19:21:29 +02:00
Letu Ren
cc43cb9a63 add Bazel support 2024-05-05 15:37:45 +08:00
Alessandro Pasotti
b85a0a414d Add Argument::store_into(std::set<int||string> &var) method 2024-04-30 17:30:48 +02:00
Andrew Bell
7cb70ed6f3 Don't store -1 into unsigned value. 2024-04-18 18:11:07 -04:00
Andrew Bell
29f1d12333 Add cstdint header. 2024-04-18 08:24:57 -04:00
Andrew Bell
d141b8d2a1 Add support for general integer types in store_into. 2024-04-17 13:35:51 -04:00
Pranav
9550b0a88c
Merge pull request #344 from rouault/At_least_one_followed_by_exactly_one
Fix parsing of a program that accepts a positional argument with 1:* cardinality followed by another positional argument with 1:1
2024-04-02 19:46:38 -04:00
Even Rouault
2c245a2c3b
Fix parsing of a program that accepts a positional argument with 1:* cardinality followed by another positional argument with 1:1 2024-04-02 23:35:43 +02:00
Even Rouault
29367256d3
Add Argument::store_into(std::vector<int> &var) method 2024-04-02 23:22:53 +02:00
Even Rouault
d7d2326f42
Add a Argument::hidden() method to prevent an argument from appearing in usage or help 2024-03-16 14:23:51 +01:00
Even Rouault
8f5b3e0722
Several bug fixes in usage, and improvement in usage and help
- Display mutually exclusive arguments as ``[[-a]|[-b]]`` in usage
- Add ... trailer to repeatable arguments in usage: ``[-x]...``

- Implement the following enhancements:

By default usage is reported on a single line.

The ``ArgumentParser::set_usage_max_line_width(width)`` method can be used
to display the usage() on multiple lines, by defining the maximum line width.

It can be combined with a call to ``ArgumentParser::set_usage_break_on_mutex()``
to ask grouped mutually exclusive arguments to be displayed on a separate line.

``ArgumentParser::add_usage_newline()`` can also be used to force the next
argument to be displayed on a new line in the usage output.

The following snippet

```cpp
    argparse::ArgumentParser program("program");
    program.set_usage_max_line_width(80);
    program.set_usage_break_on_mutex();
    program.add_argument("--quite-long-option-name").flag();
    auto &group = program.add_mutually_exclusive_group();
    group.add_argument("-a").flag();
    group.add_argument("-b").flag();
    program.add_argument("-c").flag();
    program.add_argument("--another-one").flag();
    program.add_argument("-d").flag();
    program.add_argument("--yet-another-long-one").flag();
    program.add_argument("--will-go-on-new-line").flag();
    program.add_usage_newline();
    program.add_argument("--new-line").flag();
    std::cout << program.usage() << std::endl;
```

will display:
```console
Usage: program [--help] [--version] [--quite-long-option-name]
               [[-a]|[-b]]
               [-c] [--another-one] [-d] [--yet-another-long-one]
               [--will-go-on-new-line]
               [--new-line]
```

Furthermore arguments can be separated into several groups by calling
``ArgumentParser::add_group(group_name)``. Only optional arguments should
be specified after the first call to add_group().

```cpp
    argparse::ArgumentParser program("program");
    program.set_usage_max_line_width(80);
    program.add_argument("-a").flag().help("help_a");
    program.add_group("Advanced options");
    program.add_argument("-b").flag().help("help_b");
```

will display:
```console
Usage: program [--help] [--version] [-a]

Advanced options:
               [-b]
```
2024-03-13 02:09:44 +01:00
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
d8aa2ba1db Added nargs test for multiple values to a choices() argument 2023-11-13 14:19:55 -08:00
Pranav Srinivas Kumar
f5287e2f20 Closes #307 2023-11-13 14:16:26 -08:00
Pranav Srinivas Kumar
16e2a1da72 Added support for binary notation, e.g., 0b101 2023-11-05 19:06:50 -06:00
Pranav Srinivas Kumar
f84fa8484a Marked copy and move constructors as deleted 2023-11-05 18:13:17 -06:00
Alexey Ismagilov
33146122a2 NEW: suppress flag for subcommand
resolve #258
2023-11-05 15:23:15 +03: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
Pranav Srinivas Kumar
7bbde0defb Added unit test for 2 mutex_groups 2023-11-04 15:24:07 -05:00
Pranav Srinivas Kumar
8f70dde82e Added unit test for mutex_args with three arguments 2023-11-04 15:20:47 -05:00
Pranav Srinivas Kumar
eea95c0e3a Added mutex args to copy constructor, changed to ordered set for data structure 2023-11-04 15:19:10 -05:00
Pranav Srinivas Kumar
39988ec62d Initial commit for implementing MutuallyExclusiveGroup 2023-11-04 14:57:01 -05:00
Pranav Srinivas Kumar
281f1ab017 Closes #113, add custom std::ostream& argument to ArgumentParser constructor 2023-11-04 09:31:13 -05:00
Pranav Srinivas Kumar
7a3e0f1cb3 Added another unit test for optional argument error reporting 2023-11-04 09:17:59 -05:00
Pranav Srinivas Kumar
62052fefcb Closes #285 2023-11-04 09:01:31 -05:00
Pranav Srinivas Kumar
7657a22001 Updated formatting of error message, showing all variations of argument name 2023-11-03 22:18:08 -05:00
Pranav Srinivas Kumar
78ba5e9828 Removed brackets and quotes in error message 2023-11-03 22:12:00 -05:00
Pranav Srinivas Kumar
4111905a74 Improved error reporting for #299 2023-11-03 22:05:46 -05:00
Pranav Srinivas Kumar
716ec60291 Added flag() shorthand function 2023-10-27 16:37:20 -05:00
Pranav Srinivas Kumar
5e7ce61ca7 Closes #278 2023-10-27 12:59:04 -05:00
Pranav Srinivas Kumar
d28188f4d5 Closes #247 2023-10-27 12:34:57 -05:00
Pranav Srinivas Kumar
bf9642c51d Upgraded doctest to v2.4.11 2023-10-27 11:41:35 -05:00
Pranav Srinivas Kumar
0b8d0e2426 Added support for integer type in choices 2023-10-27 10:28:54 -05:00
Pranav Srinivas Kumar
9bb553b882 #277 Added in-built support for string_type choices 2023-10-27 09:16:25 -05:00
Christoph Hindermann
6c4bddb990 CMakefile: Use -Wpedantic, -Werror and -Wextra for compilation in gcc. Fixed warnings 2023-10-20 19:57:12 +02:00
Pranav
57b63b09fa
Merge pull request #290 from Arthapz/master
Add C++20 module
2023-10-20 08:06:14 -05:00
Kian-Meng Ang
aa25a2f3ac Fix typos
Found via `codespell -L seh`
2023-10-19 13:09:11 +08:00
Arthur LAURENT
6723c81877
update test to use module when WITH_MODULE macro is set 2023-10-15 18:51:56 +02:00
Pranav
b0930ab028
Merge pull request #268 from fanurs/align_multiline_message
Fix issue #248: Align multiline help messages
2023-07-11 08:15:12 -05:00
Fanurs
e82653c2d9 Fixed test for multiline help message alignment 2023-04-22 19:23:17 -04:00
Fanurs
19d85eadb0 Passed test for multiline help message alignment 2023-04-22 15:16:47 -04: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
Pranav
e516556733
Merge pull request #254 from skrobinson/fix-maintenance
Various maintenance tasks
2023-02-19 10:29:59 -06:00
Sean Robinson
7ed952f4fe Add test for ArgumentParser::get() with inappropriate type
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2023-01-17 13:57:59 -07:00
Sean Robinson
cb3da173f6 Fix crash with char[] default values
Closes #249

Reported-by: @pfeatherstone
Signed-off-by: Sean Robinson <sean.robinson@scottsdalecc.edu>
2023-01-05 13:02:40 -07:00