Commit Graph

700 Commits

Author SHA1 Message Date
Alessandro Pasotti
8dead89026 FEATURE: multiple actions
Also fixes the incompatibility between store_into and scan and action:
when the three methods above were called, being all based on the
(unique) action, the last one would overwrite the previous ones.

This issue was making the parser strictly dependant on the order
of the scan/store_into/action calls making them mutually exclusive.
2024-05-09 09:53:34 +02:00
Pranav
eba16b3773
Merge pull request #353 from rouault/do_from_chars_coverity_warning
do_from_chars(): initialize variable to fix Coverity Scan warning
2024-05-05 19:12:04 -04:00
Even Rouault
5228c57938
do_from_chars(): initialize variable to fix Coverity Scan warning
The argparse.hpp copy inside GDAL has caused Coverity Scan to emit a
(false-positive) warning about x not being initialized.

```
________________________________________________________________________________________________________
*** CID 1544814:  Uninitialized variables  (UNINIT)
/gdal/apps/argparse/argparse.hpp: 257 in gdal_argparse::details::do_from_chars<unsigned char, (int)10>(std::basic_string_view<char, std::char_traits<char>>)()
251       if (ec == std::errc::invalid_argument) {
252         throw std::invalid_argument{"pattern '" + std::string(s) + "' not found"};
253       }
254       if (ec == std::errc::result_out_of_range) {
255         throw std::range_error{"'" + std::string(s) + "' not representable"};
256       }
>>>     CID 1544814:  Uninitialized variables  (UNINIT)
>>>     Using uninitialized value "x".
```

Let's initialize it to 0 to make the analyzer happy
2024-05-05 21:35:58 +02:00
Pranav
ce7db9962a
Merge pull request #352 from FantasqueX/bazel-1
add Bazel support
2024-05-05 10:18:58 -04:00
Letu Ren
cc43cb9a63 add Bazel support 2024-05-05 15:37:45 +08:00
Pranav
805e2356a9
Merge pull request #348 from elpaso/store_into-set-of-string-int
Add Argument::store_into(std::set<int||string> &var) method
2024-04-30 20:38:21 -04:00
Alessandro Pasotti
b85a0a414d Add Argument::store_into(std::set<int||string> &var) method 2024-04-30 17:30:48 +02:00
Pranav
ac4c2c2d96
Merge pull request #346 from abellgithub/store_ints
Store ints
2024-04-18 20:45:29 -04: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
e54e459286 Pass type along to parse_number(). 2024-04-17 13:42:37 -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
Pranav
1c4820579c
Merge pull request #343 from rouault/store_into_vector_int
Add Argument::store_into(std::vector<int> &var) method
2024-04-02 19:45:31 -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
Pranav
a1c41c5537
Merge pull request #336 from rouault/hidden_argument
Add a Argument::hidden() method to prevent an argument from appearing in usage or help
2024-03-16 10:51:35 -04: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
Pranav
cebee4bb4b
Merge pull request #334 from rouault/enhance_usage_and_help
Several bug fixes in usage, and improvement in usage and help
2024-03-13 20:43:43 -04: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
0fa7d59964
Merge pull request #332 from rouault/consume_dry_run
Add a dry_run argument to Argument::consume(), and change ArgumentParser private section to protected
2024-03-12 18:51:01 -04:00
Even Rouault
3a6118e4de
Fix clang-tidy warning 2024-03-12 15:36:54 +01:00
Even Rouault
f2e7db6090
Add a dry_run argument to Argument::consume(), and change ArgumentParser private section to protected
Both changes are needed in the special case of GDAL where our existing
hand-made argument parser accepts positional arguments placed anywhere
in the arguments, not just after optional arguments.

In the GDAL code, in 8bb3455eed (diff-4592e7daae8543536dbeadf05a3c110282ea0fc089216186138c923a0d4d8000R170) ,
I've implemented a preliminary pass before calling
ArgumentParser::process_args() that re-order arguments. For that, I need
to be able to call consume() but without side-effects (dry_run=False)
and accept members that are private
2024-03-12 15:30:05 +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
Pranav
ce2d4316a4
Merge pull request #330 from rouault/hidden_alias
Add a ArgumentParser::add_hidden_alias_for() method
2024-03-12 10:10:03 -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
c69d8e1960
Merge pull request #329 from rouault/custom_strtoX
Add ways to substitute strtof/strtod/strtold with custom functions
2024-03-12 06:59:52 -04:00
Even Rouault
c1fb3c0005
Add ways to substitute strtof/strtod/strtold with custom functions
The standard strtof/strtod/strtold are by default locale-aware. There
are a number of situations where we'd rather want to be able to use
locale-independent parsing functions. This can be done by setting the
ARGPARSE_CUSTOM_STRTOF/ARGPARSE_CUSTOM_STRTOD/ARGPARSE_CUSTOM_STRTOLD
macros to the appropriate value.
2024-03-12 10:23:02 +01:00
Pranav
20f0196321
Merge pull request #328 from rouault/make_unsigned-integer-overflow_happy
Make clang -fsanitize=unsigned-integer-overflow happy
2024-03-11 20:55:27 -04:00
Even Rouault
907b942db2
Change key of m_subparser_map and m_subparser_used to be a std::string to make clang -fsanitize=unsigned-integer-overflow happy 2024-03-11 22:37:14 +01:00
Even Rouault
c4406e2479
Change key of m_argument_map to be a std::string to make clang -fsanitize=unsigned-integer-overflow happy 2024-03-11 22:09:39 +01:00
Pranav
1b3abd9b92
Merge pull request #326 from stripe2933/master
Fix for C++23 standard library module usage.
2024-02-06 21:23:27 -05:00
LEE KYOUNGHEON
f9057b85a0
Update argparse.cppm
Fix typo
2024-02-06 17:10:14 +09:00
LEE KYOUNGHEON
6bdc003e15
Import std.compat module.
To use <cstdlib> feature, std.compat module should be imported.
2024-02-06 17:05:54 +09:00
LEE KYOUNGHEON
873596b41d
Move <errno> include outside the ARGPARSE_MODULE_USE_STD_MODULE
<errno> header exposes errno macro, which cannot be exported in C++ module. It must always included regardless of module usage.
2024-02-06 17:05:07 +09:00
Pranav
69dabd88a8
Merge pull request #310 from p-ranav/bugfix/307_choices
Bugfix/307 choices
2023-11-13 16:23:15 -06: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
af442b4da0
Merge pull request #306 from p-ranav/feature/binary_number_parsing
Added support for binary notation, e.g., 0b101
2023-11-05 19:15:14 -06:00
Pranav Srinivas Kumar
16e2a1da72 Added support for binary notation, e.g., 0b101 2023-11-05 19:06:50 -06:00
Pranav
5effc4f206
Merge pull request #305 from p-ranav/bugfix/260_segfault_copy
Bumped version to v3.0 ahead of release
2023-11-05 18:32:32 -06:00
Pranav Srinivas Kumar
379ebb6b16 Bumped version to v3.0 ahead of release 2023-11-05 18:20:10 -06:00
Pranav
6a5fbf778c
Merge pull request #304 from p-ranav/bugfix/260_segfault_copy
Marked copy and move constructors as deleted
2023-11-05 18:18:20 -06:00
Pranav Srinivas Kumar
f84fa8484a Marked copy and move constructors as deleted 2023-11-05 18:13:17 -06:00
Pranav Srinivas Kumar
3596748798 Removed test code 2023-11-05 17:10:27 -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
Pranav
3cc913d5ff
Merge pull request #273 from ismagilli/issue_258
NEW: suppress flag for subcommand
2023-11-05 07:58:02 -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