You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Added an `argument_group` class with the following attribute setters:
- `required` - at least one argument from the group must be used
- `mutually_exclusive` - at most one argument from the group can be used
- Added an `add_group` method to `argument_parser`
- Added new overloads for the `add_optional_argument` and `add_flag` functions with an additional `group` parameter
- Aligned the final parsing validation logic to properly verify the requirements of defined argument groups
- Removed the `program_name` attribute setter from `argument_parser` and added the `name` parameter to the class constructor to enforce the program name's presence in a parser instance
@@ -118,21 +123,26 @@ If you do not use CMake you can dowload the desired [library release](https://gi
118
123
119
124
To use the argument parser in your code you need to use the `ap::argument_parser` class.
120
125
121
-
The parameters you can specify for a parser's instance are:
122
-
123
-
- The program's name, version and description - used in the parser's configuration output (`std::cout << parser`).
124
-
- Verbosity mode - `false` by default; if set to `true` the parser's configuration output will include more detailed info about arguments' parameters in addition to their names and help messages.
125
-
-[Arguments](#adding-arguments) - specify the values/options accepted by the program.
.program_description("Description of the program")
133
130
.verbose();
134
131
```
135
132
133
+
> [!IMPORTANT]
134
+
>
135
+
> - When creating an argument parser instance, you must provide a program name to the constructor.
136
+
>
137
+
> The program name given to the parser cannot contain whitespace characters.
138
+
>
139
+
> - Additional parameters you can specify for a parser's instance incldue:
140
+
> - The program's version and description - used in the parser's configuration output (`std::cout << parser`).
141
+
> - Verbosity mode - `false` by default; if set to `true` the parser's configuration output will include more detailed info about arguments' parameters in addition to their names and help messages.
142
+
> - [Arguments](#adding-arguments) - specify the values/options accepted by the program.
143
+
> - [Argument Groups](#argument-groups) - organize related optional arguments into sections and optionally enforce usage rules.
Argument groups provide a way to organize related optional arguments into logical sections. They make the command-line interface easier to read in help messages, and can enforce rules such as **mutual exclusivity** or **required usage**.
822
+
823
+
By default, every parser comes with two predefined groups:
824
+
825
+
- **Positional Arguments** – contains all arguments added via `add_positional_argument`.
826
+
- **Optional Arguments** – contains all arguments added via `add_optional_argument` or `add_flag` without explicitly specifying an argument group.
827
+
828
+
User-defined groups can only contain optional arguments (including flags). This allows you to structure your command-line interface into meaningful sections such as "Input Options", "Output Options", or "Debug Settings".
829
+
830
+
831
+
### Creating New Groups
832
+
833
+
A new group can be created by calling the `add_group` method of an argument parser:
The group’s name will appear as a dedicated section in the help message and arguments added to this group will be listed under `Output Options` instead of the default `Optional Arguments` section.
841
+
842
+
> [!NOTE]
843
+
>
844
+
> If a group has no visible arguments, it will not be included in the parser's help message output at all.
845
+
846
+
### Adding Arguments to Groups
847
+
848
+
Arguments are added to a group by passing the group reference as the first parameter to the `add_optional_argument` and `add_flag` functions:
When invoked with the `--help` flag, the above program produces a help message that clearly shows the group and its rules:
905
+
906
+
```
907
+
Program: myprog
908
+
909
+
Output Options: (required, mutually exclusive)
910
+
911
+
--output, -o : Print output to a given file
912
+
--print, -p : Print output to the console
913
+
```
914
+
915
+
<br/>
916
+
<br/>
917
+
<br/>
918
+
810
919
## Parsing Arguments
811
920
812
921
To parse the command-line arguments use the `void argument_parser::parse_args(const AR& argv)` method, where `AR` must be a type that satisfies `std::ranges::range` and its value type is convertible to `std::string`.
@@ -845,19 +954,18 @@ The simple example below demonstrates how (in terms of the program's structure)
845
954
846
955
int main(int argc, char* argv[]) {
847
956
// create the parser class instance
848
-
ap::argument_parser parser;
957
+
ap::argument_parser parser("some-program");
849
958
850
-
// define the parser's attributes
851
-
parser.program_name("some-program")
852
-
.program_description("The program does something with command-line arguments");
959
+
// define the parser's attributes and default arguments
960
+
parser.program_version({0u, 0u, 0u})
961
+
.program_description("The program does something with command-line arguments")
0 commit comments