Skip to content

Commit af578ca

Browse files
committed
Don't require = to set a flag value
- If a command line flag doesn't specify a value with = and requires a value, use the next argument as the value (to allow e.g. `-i test`).
1 parent eab61fd commit af578ca

2 files changed

Lines changed: 55 additions & 18 deletions

File tree

OMCompiler/Compiler/Util/FlagsUtil.mo

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
667666
protected
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;
713710
end readArg;
714711

715712
protected 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 = "";
720718
protected
721719
String flag;
722720
list<String> values;
721+
Boolean missing_value;
723722
algorithm
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);
727726
end parseFlag;
728727

729728
protected 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;
735736
protected
736737
Flags.ConfigFlag config_flag;
738+
list<String> values;
737739
algorithm
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);
740753
end parseConfigFlag;
741754

742755
protected function lookupConfigFlag
@@ -766,6 +779,16 @@ algorithm
766779
end match;
767780
end 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+
769792
protected function setAdditionalOptModules
770793
input Flags.ConfigFlag inFlag;
771794
input Flags.ConfigFlag inOppositeFlag;

testsuite/openmodelica/interactive-API/FlagParsing.mos

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ setCommandLineOptions("fish"); getErrorString();
6464
// only take flags.
6565
setCommandLineOptions("-- -d=failtrace"); getErrorString();
6666

67+
// Should work, flags don't require =.
68+
setCommandLineOptions("-d failtrace"); getErrorString();
69+
setCommandLineOptions("-d failtrace --debug=ceval"); getErrorString();
70+
71+
// Shouldn't work, some flags require a value.
72+
setCommandLineOptions("-i="); getErrorString();
73+
6774
// Result:
6875
// true
6976
// ""
@@ -143,4 +150,11 @@ setCommandLineOptions("-- -d=failtrace"); getErrorString();
143150
// ""
144151
// false
145152
// ""
153+
// true
154+
// ""
155+
// true
156+
// ""
157+
// false
158+
// "Error: Invalid type of flag instClass, expected a string but got nothing.
159+
// "
146160
// endResult

0 commit comments

Comments
 (0)