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
Modified the command-line tokenization logic to try to split unknown secondary flag arguments into multiple secondary argument flag tokens (one for each character of the unknown flag).
Example: If the program defines an argument with a secondary name `v`, then `-vvv` would be equivalent to `-v -v -v`, unless the program also defines a `-vvv` argument.
Additional:
- Added support for declaring the program version
- Optimized argument lookup during argument tokenization and parsing stages
@@ -103,17 +105,34 @@ To use the argument parser in your code you need to use the `ap::argument_parser
103
105
104
106
The parameters you can specify for a parser's instance are:
105
107
106
-
-Program name and description - used in the parser's configuration output (`std::cout << parser`).
108
+
-The program's name, version and description - used in the parser's configuration output (`std::cout << parser`).
107
109
- 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.
108
110
-[Arguments](#adding-arguments) - specify the values/options accepted by the program.
109
111
110
112
```cpp
111
113
ap::argument_parser parser;
112
114
parser.program_name("Name of the program")
115
+
.program_version("alhpa")
113
116
.program_description("Description of the program")
114
117
.verbose();
115
118
```
116
119
120
+
> [!TIP]
121
+
>
122
+
> You can specify the program version using a string (like in the example above) or using the `ap::version` structure:
> - contains the three members - `major`, `minor`, `patch` - all of which are of type `std::uint32_t`,
133
+
> - defines a `std::string str() const` method which returns a `v{major}.{minor}.{path}` version string,
134
+
> - defines the `std::ostream& operator<<` for stream insertion.
135
+
117
136
<br/>
118
137
<br/>
119
138
<br/>
@@ -803,41 +822,37 @@ int main(int argc, char* argv[]) {
803
822
804
823
```shell
805
824
./power 2
806
-
# out:
807
-
# no exponent values given
825
+
no exponent values given
808
826
```
809
827
810
828
```shell
811
829
./power
812
-
# out:
813
-
# [ERROR] : No values parsed for a required argument [base]
814
-
# Program: power calculator
815
-
#
816
-
# Calculates the value of an expression: base ^ exponent
817
-
#
818
-
# Positional arguments:
819
-
#
820
-
# base : the exponentation base value
821
-
#
822
-
# Optional arguments:
823
-
#
824
-
# --exponent, -e : the exponent value
825
-
# --help, -h : Display the help message
830
+
[ERROR] : No values parsed for a required argument [base]
831
+
Program: power calculator
832
+
833
+
Calculates the value of an expression: base ^ exponent
834
+
835
+
Positional arguments:
836
+
837
+
base : the exponentation base value
838
+
839
+
Optional arguments:
840
+
841
+
--exponent, -e : the exponent value
842
+
--help, -h : Display the help message
826
843
```
827
844
828
845
> [!IMPORTANT]
829
846
>
830
847
> For each positional argument there must be **exactly one value**.
831
848
832
-
- Optional arguments are parsed only with a flag:
849
+
- Optional arguments are parsed only with a flag. The values passed after an argument flag will be treated as the values of the last optional argument that preceeds them. If no argument flag preceeds a value argument, then it will be treated as an **unknown** value.
@@ -850,25 +865,85 @@ int main(int argc, char* argv[]) {
850
865
851
866
```shell
852
867
./power 2 1 2 3
853
-
# out:
854
-
# [ERROR] : Failed to deduce the argument for values [1, 2, 3]
855
-
# Program: power calculator
856
-
#
857
-
# Calculates the value of an expression: base ^ exponent
858
-
#
859
-
# Positional arguments:
860
-
#
861
-
# base : the exponentation base value
862
-
#
863
-
# Optional arguments:
864
-
#
865
-
# --exponent, -e : the exponent value
866
-
# --help, -h : Display the help message
868
+
[ERROR] : Failed to deduce the argument for values [1, 2, 3]
869
+
Program: power calculator
870
+
871
+
Calculates the value of an expression: base ^ exponent
872
+
873
+
Positional arguments:
874
+
875
+
base : the exponentation base value
876
+
877
+
Optional arguments:
878
+
879
+
--exponent, -e : the exponent value
880
+
--help, -h : Display the help message
867
881
```
868
882
883
+
> [!WARNING]
884
+
>
885
+
> If an optional argument has the `nargs` parameter set with an upper bound, then the values that succeed this argument's flag will be assigned to this argument only until the specified upper bound is reached. Further values will be treated as **unknown** values.
<< "\nNumbers: " << join(parser.values<int>("numbers"), ", ") // join is an imaginary function :)
933
+
<< std::endl;
934
+
935
+
/*
936
+
> ./program -vvon 1 2 3
937
+
Verbosity level: 2
938
+
Option used: true
939
+
Numbers: 1, 2, 3
940
+
```
941
+
869
942
> [!IMPORTANT]
870
943
>
871
-
> The parser's behavior depends on the argument definitions - see [Argument Parameters](#argument-parameters) section.
944
+
> - If there exists an argument whose secondary name matches a possible compound of other arguments, the parser will still treat the flag as a flag of the **single matching argument**, not as multiple flags.
945
+
> - The argument parser will try to assign the values following a compound argument flag to the argument represented by the **last character** of the compound flag.
0 commit comments