@@ -318,6 +318,7 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
318318 } catch (SystemError &) {
319319 }
320320 }
321+
321322 for (auto pos = cmdline.begin (); pos != cmdline.end ();) {
322323
323324 auto arg = *pos;
@@ -354,6 +355,9 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
354355
355356 processArgs (pendingArgs, true );
356357
358+ if (!completions)
359+ checkArgs ();
360+
357361 initialFlagsProcessed ();
358362
359363 /* Now that we are done parsing, make sure that any experimental
@@ -384,7 +388,7 @@ bool Args::processFlag(Strings::iterator & pos, Strings::iterator end)
384388
385389 auto & rootArgs = getRoot ();
386390
387- auto process = [&](const std::string & name, const Flag & flag) -> bool {
391+ auto process = [&](const std::string & name, Flag & flag) -> bool {
388392 ++pos;
389393
390394 if (auto & f = flag.experimentalFeature )
@@ -413,6 +417,7 @@ bool Args::processFlag(Strings::iterator & pos, Strings::iterator end)
413417 }
414418 if (!anyCompleted)
415419 flag.handler .fun (std::move (args));
420+ flag.timesUsed ++;
416421 return true ;
417422 };
418423
@@ -504,6 +509,14 @@ bool Args::processArgs(const Strings & args, bool finish)
504509 return res;
505510}
506511
512+ void Args::checkArgs ()
513+ {
514+ for (auto & [name, flag] : longFlags) {
515+ if (flag->required && flag->timesUsed == 0 )
516+ throw UsageError (" required argument '--%s' is missing" , name);
517+ }
518+ }
519+
507520nlohmann::json Args::toJSON ()
508521{
509522 auto flags = nlohmann::json::object ();
@@ -643,6 +656,13 @@ bool MultiCommand::processArgs(const Strings & args, bool finish)
643656 return Args::processArgs (args, finish);
644657}
645658
659+ void MultiCommand::checkArgs ()
660+ {
661+ Args::checkArgs ();
662+ if (command)
663+ command->second ->checkArgs ();
664+ }
665+
646666nlohmann::json MultiCommand::toJSON ()
647667{
648668 auto cmds = nlohmann::json::object ();
0 commit comments