@@ -1028,15 +1028,94 @@ bool GDALAlgorithmArg::RunValidationActions()
10281028 ret = ValidateRealRange (v) && ret;
10291029 }
10301030
1031- for ( const auto &f : m_validationActions )
1031+ if ( GDALAlgorithmArgTypeIsList ( GetType ()) )
10321032 {
1033- if (!f ())
1033+ int valueCount = 0 ;
1034+ if (GetType () == GAAT_STRING_LIST)
1035+ {
1036+ valueCount =
1037+ static_cast <int >(Get<std::vector<std::string>>().size ());
1038+ }
1039+ else if (GetType () == GAAT_INTEGER_LIST)
1040+ {
1041+ valueCount = static_cast <int >(Get<std::vector<int >>().size ());
1042+ }
1043+ else if (GetType () == GAAT_REAL_LIST)
1044+ {
1045+ valueCount = static_cast <int >(Get<std::vector<double >>().size ());
1046+ }
1047+ else if (GetType () == GAAT_DATASET_LIST)
1048+ {
1049+ valueCount = static_cast <int >(
1050+ Get<std::vector<GDALArgDatasetValue>>().size ());
1051+ }
1052+
1053+ if (valueCount != GetMinCount () && GetMinCount () == GetMaxCount ())
1054+ {
1055+ ReportError (CE_Failure, CPLE_AppDefined,
1056+ " %d value%s been specified for argument '%s', "
1057+ " whereas exactly %d %s expected." ,
1058+ valueCount, valueCount > 1 ? " s have" : " has" ,
1059+ GetName ().c_str (), GetMinCount (),
1060+ GetMinCount () > 1 ? " were" : " was" );
10341061 ret = false ;
1062+ }
1063+ else if (valueCount < GetMinCount ())
1064+ {
1065+ ReportError (CE_Failure, CPLE_AppDefined,
1066+ " Only %d value%s been specified for argument '%s', "
1067+ " whereas at least %d %s expected." ,
1068+ valueCount, valueCount > 1 ? " s have" : " has" ,
1069+ GetName ().c_str (), GetMinCount (),
1070+ GetMinCount () > 1 ? " were" : " was" );
1071+ ret = false ;
1072+ }
1073+ else if (valueCount > GetMaxCount ())
1074+ {
1075+ ReportError (CE_Failure, CPLE_AppDefined,
1076+ " %d value%s been specified for argument '%s', "
1077+ " whereas at most %d %s expected." ,
1078+ valueCount, valueCount > 1 ? " s have" : " has" ,
1079+ GetName ().c_str (), GetMaxCount (),
1080+ GetMaxCount () > 1 ? " were" : " was" );
1081+ ret = false ;
1082+ }
1083+ }
1084+
1085+ if (ret)
1086+ {
1087+ for (const auto &f : m_validationActions)
1088+ {
1089+ if (!f ())
1090+ ret = false ;
1091+ }
10351092 }
10361093
10371094 return ret;
10381095}
10391096
1097+ /* ***********************************************************************/
1098+ /* GDALAlgorithmArg::ReportError() */
1099+ /* ***********************************************************************/
1100+
1101+ void GDALAlgorithmArg::ReportError (CPLErr eErrClass, CPLErrorNum err_no,
1102+ const char *fmt, ...) const
1103+ {
1104+ va_list args;
1105+ va_start (args, fmt);
1106+ if (m_owner)
1107+ {
1108+ m_owner->ReportError (eErrClass, err_no, " %s" ,
1109+ CPLString ().vPrintf (fmt, args).c_str ());
1110+ }
1111+ else
1112+ {
1113+ CPLError (eErrClass, err_no, " %s" ,
1114+ CPLString ().vPrintf (fmt, args).c_str ());
1115+ }
1116+ va_end (args);
1117+ }
1118+
10401119/* ***********************************************************************/
10411120/* GDALAlgorithmArg::GetEscapedString() */
10421121/* ***********************************************************************/
@@ -2849,63 +2928,6 @@ bool GDALAlgorithm::ValidateArguments()
28492928 if (!ProcessDatasetArg (arg.get (), this ))
28502929 ret = false ;
28512930 }
2852- else if (arg->IsExplicitlySet () &&
2853- GDALAlgorithmArgTypeIsList (arg->GetType ()))
2854- {
2855- int valueCount = 0 ;
2856- if (arg->GetType () == GAAT_STRING_LIST)
2857- {
2858- valueCount = static_cast <int >(
2859- arg->Get <std::vector<std::string>>().size ());
2860- }
2861- else if (arg->GetType () == GAAT_INTEGER_LIST)
2862- {
2863- valueCount =
2864- static_cast <int >(arg->Get <std::vector<int >>().size ());
2865- }
2866- else if (arg->GetType () == GAAT_REAL_LIST)
2867- {
2868- valueCount =
2869- static_cast <int >(arg->Get <std::vector<double >>().size ());
2870- }
2871- else if (arg->GetType () == GAAT_DATASET_LIST)
2872- {
2873- valueCount = static_cast <int >(
2874- arg->Get <std::vector<GDALArgDatasetValue>>().size ());
2875- }
2876-
2877- if (valueCount != arg->GetMinCount () &&
2878- arg->GetMinCount () == arg->GetMaxCount ())
2879- {
2880- ReportError (CE_Failure, CPLE_AppDefined,
2881- " %d value%s been specified for argument '%s', "
2882- " whereas exactly %d %s expected." ,
2883- valueCount, valueCount > 1 ? " s have" : " has" ,
2884- arg->GetName ().c_str (), arg->GetMinCount (),
2885- arg->GetMinCount () > 1 ? " were" : " was" );
2886- ret = false ;
2887- }
2888- else if (valueCount < arg->GetMinCount ())
2889- {
2890- ReportError (CE_Failure, CPLE_AppDefined,
2891- " Only %d value%s been specified for argument '%s', "
2892- " whereas at least %d %s expected." ,
2893- valueCount, valueCount > 1 ? " s have" : " has" ,
2894- arg->GetName ().c_str (), arg->GetMinCount (),
2895- arg->GetMinCount () > 1 ? " were" : " was" );
2896- ret = false ;
2897- }
2898- else if (valueCount > arg->GetMaxCount ())
2899- {
2900- ReportError (CE_Failure, CPLE_AppDefined,
2901- " %d value%s been specified for argument '%s', "
2902- " whereas at most %d %s expected." ,
2903- valueCount, valueCount > 1 ? " s have" : " has" ,
2904- arg->GetName ().c_str (), arg->GetMaxCount (),
2905- arg->GetMaxCount () > 1 ? " were" : " was" );
2906- ret = false ;
2907- }
2908- }
29092931
29102932 if (arg->IsExplicitlySet () && arg->GetType () == GAAT_DATASET_LIST &&
29112933 arg->AutoOpenDataset ())
@@ -2978,6 +3000,9 @@ bool GDALAlgorithm::ValidateArguments()
29783000 }
29793001 }
29803002 }
3003+
3004+ if (arg->IsExplicitlySet () && !arg->RunValidationActions ())
3005+ ret = false ;
29813006 }
29823007
29833008 for (const auto &f : m_validationActions)
0 commit comments