171 Commits

Author SHA1 Message Date
root 3eda91b2e1 Bumped version to 3.2 2025-01-25 15:27:24 +00:00
Artyom Fartygin e927d08698 Add missed ToC link to README.md
Add missed link to 'Getting Argument and Subparser Instances' paragraph.
2024-07-31 00:40:55 +03:00
Pranav Srinivas Kumar 68fd0277ee Bumped to v3.1 2024-07-17 19:53:38 -04:00
RichardBrown384 0c44bc349f Correct Parent Parser errors in README.md
* Rename parent_parser -> surface_parser
* The second constructor argument is a string.
2024-05-29 11:05:15 +02:00
Letu Ren cc43cb9a63 add Bazel support 2024-05-05 15:37:45 +08: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 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