@@ -643,9 +643,7 @@ algorithm
643643 // Stop parsing arguments if -- is encountered.
644644 break ;
645645 else
646- if not readArg(arg, flags) then
647- outArgs := arg :: outArgs;
648- end if ;
646+ (rest_args, outArgs) := readArg(arg, flags, rest_args, outArgs);
649647 end if ;
650648 end while ;
651649
@@ -663,7 +661,8 @@ protected function readArg
663661 consumed, otherwise false."
664662 input String inArg;
665663 input Flags . Flag inFlags;
666- output Boolean outConsumed;
664+ input output list< String > restArgs;
665+ input output list< String > nonFlags;
667666protected
668667 String flagtype;
669668 Integer len;
@@ -675,68 +674,82 @@ algorithm
675674 if flagtype == "+" then
676675 if len == 1 then
677676 // + alone is not a valid flag.
678- parseFlag(inArg, Flags . NO_FLAGS ());
677+ parseFlag(inArg, Flags . NO_FLAGS (), restArgs );
679678 else
680- parseFlag(System . substring(inArg, 2 , len), inFlags, flagtype);
679+ restArgs : = parseFlag(System . substring(inArg, 2 , len), inFlags, restArgs , flagtype);
681680 end if ;
682- outConsumed := true ;
683681 // Flags beginning with - must have another - for long flags, i.e. -h or --help.
684682 elseif flagtype == "-" then
685683 if len == 1 then
686684 // - alone is not a valid flag.
687- parseFlag(inArg, Flags . NO_FLAGS ());
685+ parseFlag(inArg, Flags . NO_FLAGS (), restArgs );
688686 elseif len == 2 then
689687 // Short flag without argument, i.e. -h.
690- parseFlag(System . substring(inArg, 2 , 2 ), inFlags, flagtype);
688+ restArgs : = parseFlag(System . substring(inArg, 2 , 2 ), inFlags, restArgs , flagtype);
691689 elseif stringGetStringChar(inArg, 2 ) == "-" then
692690 if len < 4 or stringGetStringChar(inArg, 4 ) == "=" then
693691 // Short flags may not be used with --, i.e. --h or --h=debug.
694- parseFlag(inArg, Flags . NO_FLAGS ());
692+ parseFlag(inArg, Flags . NO_FLAGS (), restArgs );
695693 else
696694 // Long flag, i.e. --help or --help=debug.
697- parseFlag(System . substring(inArg, 3 , len), inFlags, "--" );
695+ restArgs : = parseFlag(System . substring(inArg, 3 , len), inFlags, restArgs , "--" );
698696 end if ;
699697 else
700698 if stringGetStringChar(inArg, 3 ) == "=" then
701699 // Short flag with argument, i.e. -h=debug.
702- parseFlag(System . substring(inArg, 2 , len), inFlags, flagtype);
700+ restArgs : = parseFlag(System . substring(inArg, 2 , len), inFlags, restArgs , flagtype);
703701 else
704702 // Long flag used with -, i.e. -help, which is not allowed.
705- parseFlag(inArg, Flags . NO_FLAGS ());
703+ parseFlag(inArg, Flags . NO_FLAGS (), restArgs );
706704 end if ;
707705 end if ;
708- outConsumed := true ;
709706 else
710707 // Arguments that don't begin with + or - are not flags, ignore them.
711- outConsumed := false ;
708+ nonFlags := inArg :: nonFlags ;
712709 end if ;
713710end readArg;
714711
715712protected function parseFlag
716713 "Parses a single flag."
717714 input String inFlag;
718715 input Flags . Flag inFlags;
716+ input output list< String > restArgs;
719717 input String inFlagPrefix = "" ;
720718protected
721719 String flag;
722720 list< String > values;
721+ Boolean missing_value;
723722algorithm
724723 flag :: values := System . strtok(inFlag, "=" );
725- values := List . flatten( List . map1( values, System . strtok , "," ) );
726- parseConfigFlag(flag, values, inFlags, inFlagPrefix);
724+ missing_value := listEmpty( values) and not StringUtil . endsWith(inFlag , "=" );
725+ restArgs : = parseConfigFlag(flag, values, inFlags, restArgs, inFlagPrefix, missing_value );
727726end parseFlag;
728727
729728protected function parseConfigFlag
730729 "Tries to look up the flag with the given name, and set it to the given value."
731730 input String inFlag;
732731 input list< String > inValues;
733732 input Flags . Flag inFlags;
733+ input output list< String > restArgs;
734734 input String inFlagPrefix;
735+ input Boolean missingValue;
735736protected
736737 Flags . ConfigFlag config_flag;
738+ list< String > values;
737739algorithm
738740 config_flag := lookupConfigFlag(inFlag, inFlagPrefix);
739- evaluateConfigFlag(config_flag, inValues, inFlags);
741+
742+ if missingValue and flagRequiresValue(config_flag) and not listEmpty(restArgs) then
743+ // If no value was given using = and the flag requires a value,
744+ // use the next argument as the value.
745+ values := {listHead(restArgs)};
746+ restArgs := listRest(restArgs);
747+ else
748+ values := inValues;
749+ end if ;
750+
751+ values := List . flatten(List . map1(values, System . strtok, "," ));
752+ evaluateConfigFlag(config_flag, values, inFlags);
740753end parseConfigFlag;
741754
742755protected function lookupConfigFlag
@@ -766,6 +779,16 @@ algorithm
766779 end match;
767780end configFlagEq;
768781
782+ protected function flagRequiresValue
783+ input Flags . ConfigFlag flag;
784+ output Boolean requiresValue;
785+ algorithm
786+ requiresValue := match flag
787+ case Flags . CONFIG_FLAG (defaultValue = Flags . BOOL_FLAG ()) then false ;
788+ else true ;
789+ end match;
790+ end flagRequiresValue;
791+
769792protected function setAdditionalOptModules
770793 input Flags . ConfigFlag inFlag;
771794 input Flags . ConfigFlag inOppositeFlag;
0 commit comments