@@ -199,7 +199,7 @@ struct ValidationBase {
199
199
template <class T >
200
200
auto operator ()(const T& value, span<string_view> values,
201
201
string_view option_name) -> void {
202
- if (!this ->isValid (value, values)) {
202
+ if (!this ->isValid (value, values)) [[unlikely]] {
203
203
throw ValidationError (
204
204
format (" Option {} has invalid value {}" , option_name, value));
205
205
}
@@ -284,7 +284,7 @@ struct Range final : public ValidationBase {
284
284
template <class U >
285
285
auto operator ()(const U& value, span<string_view> values,
286
286
string_view option_name) -> void {
287
- if (!this ->isValid (value, values)) {
287
+ if (!this ->isValid (value, values)) [[unlikely]] {
288
288
throw ValidationError (
289
289
format (" Option {} has invalid value {}" , option_name, value));
290
290
}
@@ -833,7 +833,7 @@ ARGO_ALWAYS_INLINE constexpr auto GetkeyFromShortKey(char key) {
833
833
}
834
834
return false ;
835
835
}() || ...);
836
- }(make_type_sequence_t <Arguments>())) {
836
+ }(make_type_sequence_t <Arguments>())) [[likely]] {
837
837
return make_tuple (name, is_flag);
838
838
}
839
839
throw ParserInternalError (" Fail to lookup" );
@@ -956,7 +956,7 @@ ARGO_ALWAYS_INLINE constexpr auto ValiadicArgAssign(
956
956
template <class Arg >
957
957
ARGO_ALWAYS_INLINE constexpr auto NLengthArgAssign (span<string_view>& values)
958
958
-> void {
959
- if (Arg::nargs.nargs > values.size ()) {
959
+ if (Arg::nargs.nargs > values.size ()) [[unlikely]] {
960
960
throw Argo::InvalidArgument (
961
961
format (" Argument {}: invalid argument {}" , Arg::name.getKey (), values));
962
962
}
@@ -1004,7 +1004,7 @@ ARGO_ALWAYS_INLINE constexpr auto PArgAssigner(span<string_view> values)
1004
1004
return true ;
1005
1005
}
1006
1006
if constexpr (Arg::nargs.nargs == 1 ) {
1007
- if (values.empty ()) {
1007
+ if (values.empty ()) [[unlikely]] {
1008
1008
throw Argo::InvalidArgument (
1009
1009
format (" Argument {} should take exactly one value but zero" ,
1010
1010
Arg::name.getKey ()));
@@ -1034,10 +1034,12 @@ ARGO_ALWAYS_INLINE constexpr auto AssignOneArg(const string_view& key,
1034
1034
throw Argo::InvalidArgument (format (" Duplicated argument {}" , key));
1035
1035
}
1036
1036
if constexpr (derived_from<Head, FlagArgTag>) {
1037
- if (!values. empty () ) {
1038
- if constexpr (is_same_v<PArgs, tuple<>>) {
1037
+ if constexpr (is_same_v<PArgs, tuple<>> ) {
1038
+ if (!values. empty ()) [[unlikely]] {
1039
1039
throw Argo::InvalidArgument (format (" Flag {} can not take value" , key));
1040
- } else {
1040
+ }
1041
+ } else {
1042
+ if (!values.empty ()) {
1041
1043
PArgAssigner<PArgs>(values);
1042
1044
}
1043
1045
}
@@ -1063,14 +1065,14 @@ ARGO_ALWAYS_INLINE constexpr auto AssignOneArg(const string_view& key,
1063
1065
ValiadicArgAssign<Head>(values);
1064
1066
return true ;
1065
1067
} else if constexpr (Head::nargs.nargs_char == ' +' ) {
1066
- if (values.empty ()) {
1068
+ if (values.empty ()) [[unlikely]] {
1067
1069
throw Argo::InvalidArgument (
1068
1070
format (" Argument {} should take more than one value" , key));
1069
1071
}
1070
1072
ValiadicArgAssign<Head>(values);
1071
1073
return true ;
1072
1074
} else if constexpr (Head::nargs.nargs == 1 ) {
1073
- if (values.empty ()) {
1075
+ if (values.empty ()) [[unlikely]] {
1074
1076
throw Argo::InvalidArgument (
1075
1077
format (" Argument {} should take exactly one value but zero" , key));
1076
1078
}
@@ -1093,27 +1095,29 @@ ARGO_ALWAYS_INLINE constexpr auto AssignOneArg(const string_view& key,
1093
1095
template <class Args , class PArgs >
1094
1096
ARGO_ALWAYS_INLINE constexpr auto assignArg (const string_view& key,
1095
1097
const span<string_view>& values) {
1096
- [&key, &values]< size_t ... Is>(index_sequence<Is...>)
1097
- ARGO_ALWAYS_INLINE -> void {
1098
- if (!(... ||
1099
- ( tuple_element_t <Is, Args>::name. getKey () == key and
1100
- AssignOneArg< tuple_element_t <Is, Args>, PArgs>(key, values)))) {
1101
- throw Argo::InvalidArgument (format (" Invalid argument {}" , key));
1102
- }
1103
- }(make_index_sequence<tuple_size_v<Args>>());
1098
+ [&key,
1099
+ &values]< size_t ... Is>(index_sequence<Is...>) ARGO_ALWAYS_INLINE -> void {
1100
+ if (!(... || ( tuple_element_t <Is, Args>::name. getKey () == key and
1101
+ AssignOneArg< tuple_element_t <Is, Args>, PArgs>( key, values))))
1102
+ [[unlikely]] {
1103
+ throw Argo::InvalidArgument (format (" Invalid argument {}" , key));
1104
+ }
1105
+ }(make_index_sequence<tuple_size_v<Args>>());
1104
1106
}
1105
1107
1106
1108
template <class Arguments , class PArgs >
1107
1109
ARGO_ALWAYS_INLINE constexpr auto Assigner (string_view key,
1108
1110
const span<string_view>& values)
1109
1111
-> void {
1110
- if (key. empty () ) {
1111
- if constexpr (!is_same_v<PArgs, tuple<>> ) {
1112
- if (!PArgAssigner<PArgs>(values)) {
1112
+ if constexpr (!is_same_v<PArgs, tuple<>> ) {
1113
+ if (key. empty () ) {
1114
+ if (!PArgAssigner<PArgs>(values)) [[unlikely]] {
1113
1115
throw InvalidArgument (" Duplicated positional argument" );
1114
1116
}
1115
1117
return ;
1116
- } else {
1118
+ }
1119
+ } else {
1120
+ if (key.empty ()) [[unlikely]] {
1117
1121
throw Argo::InvalidArgument (format (" Invalid argument {}" , key));
1118
1122
}
1119
1123
}
@@ -1135,7 +1139,7 @@ ARGO_ALWAYS_INLINE constexpr auto ShortArgAssigner(
1135
1139
auto value = vector<string_view>{key.substr (i + 1 )};
1136
1140
assignArg<Arguments, PArgs>(found_key, value);
1137
1141
return has_help;
1138
- } else {
1142
+ } else [[unlikely]] {
1139
1143
throw Argo::InvalidArgument (
1140
1144
format (" Invalid Flag argument {} {}" , key[i], key.substr (i + 1 )));
1141
1145
}
@@ -1439,7 +1443,7 @@ class Parser {
1439
1443
1440
1444
template <ArgName Name>
1441
1445
constexpr auto getArg () {
1442
- if (!this ->parsed_ ) {
1446
+ if (!this ->parsed_ ) [[unlikely]] {
1443
1447
throw ParseError (" Parser did not parse argument, call parse first" );
1444
1448
}
1445
1449
if constexpr (!is_same_v<PArgs, tuple<>>) {
@@ -1470,7 +1474,7 @@ class Parser {
1470
1474
1471
1475
template <ArgName Name>
1472
1476
constexpr auto isAssigned () {
1473
- if (!this ->parsed_ ) {
1477
+ if (!this ->parsed_ ) [[unlikely]] {
1474
1478
throw ParseError (" Parser did not parse argument, call parse first" );
1475
1479
}
1476
1480
if constexpr (!is_same_v<PArgs, tuple<>>) {
@@ -1638,17 +1642,20 @@ constexpr auto Parser<ID, Args, PArgs, HArg, SubParsers>::parse(int argc,
1638
1642
} else {
1639
1643
is_flag = true ;
1640
1644
}
1645
+
1641
1646
if (i != 1 and is_flag) {
1642
1647
if (!key.empty ()) {
1643
1648
goto SetArgSection;
1644
1649
}
1645
1650
if (!short_keys.empty ()) {
1646
1651
goto SetShortArgSection;
1647
1652
}
1648
- if (!values. empty () ) {
1649
- if constexpr (!is_same_v<PArgs, tuple<>> ) {
1653
+ if constexpr (!is_same_v<PArgs, tuple<>> ) {
1654
+ if (!values. empty () ) {
1650
1655
goto SetArgSection;
1651
- } else {
1656
+ }
1657
+ } else {
1658
+ if (!values.empty ()) [[unlikely]] {
1652
1659
throw InvalidArgument (
1653
1660
format (" Invalid positional argument: {}" , values));
1654
1661
}
@@ -1664,9 +1671,11 @@ constexpr auto Parser<ID, Args, PArgs, HArg, SubParsers>::parse(int argc,
1664
1671
values.clear ();
1665
1672
End:
1666
1673
}
1667
- if (i == cmd_end_pos) [[unlikely]] {
1674
+
1675
+ if (i == cmd_end_pos) {
1668
1676
break ;
1669
1677
}
1678
+
1670
1679
if (is_flag) {
1671
1680
if (arg.size () > 1 and arg.at (1 ) == ' -' ) {
1672
1681
if (arg.contains (' =' )) [[unlikely]] {
@@ -1695,7 +1704,7 @@ constexpr auto Parser<ID, Args, PArgs, HArg, SubParsers>::parse(int argc,
1695
1704
}
1696
1705
});
1697
1706
1698
- if (!required_keys.empty ()) {
1707
+ if (!required_keys.empty ()) [[unlikely]] {
1699
1708
throw InvalidArgument (format (" Requried {}" , required_keys));
1700
1709
}
1701
1710
if (subcmd_found_idx != -1 ) {
0 commit comments