Skip to content

Commit e38a86d

Browse files
committed
Address option parsing review feedback
1 parent 9661de9 commit e38a86d

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/ConsoleAppFramework/Emitter.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,14 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
144144
using (sb.BeginBlock("for (int i = 0; i < commandArgs.Length; i++)"))
145145
{
146146
sb.AppendLine("var name = commandArgs[i];");
147-
sb.AppendLine("var optionMatched = false;");
148-
sb.AppendLine("var optionCandidate = name.Length > 1 && name[0] == '-' && !char.IsDigit(name[1]);");
147+
if (hasArgument)
148+
{
149+
sb.AppendLine("var optionCandidate = name.Length > 1 && name[0] == '-' && !char.IsDigit(name[1]);");
150+
}
151+
else
152+
{
153+
sb.AppendLine("var optionCandidate = name.Length > 1 && name[0] == '-';");
154+
}
149155
sb.AppendLine();
150156

151157
if (!command.Parameters.All(p => !p.IsParsable || p.IsArgument))
@@ -173,8 +179,7 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
173179
{
174180
sb.AppendLine($"arg{i}Parsed = true;");
175181
}
176-
sb.AppendLine("optionMatched = true;");
177-
sb.AppendLine("break;");
182+
sb.AppendLine("continue;");
178183
}
179184
}
180185

@@ -200,21 +205,14 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
200205
{
201206
sb.AppendLine($"arg{i}Parsed = true;");
202207
}
203-
sb.AppendLine("optionMatched = true;");
204-
sb.AppendLine($"break;");
208+
sb.AppendLine("continue;");
205209
}
206210
}
207211

208212
sb.AppendLine("ThrowArgumentNameNotFound(name);");
209213
sb.AppendLine("break;");
210214
}
211215
}
212-
213-
sb.AppendLine("if (optionMatched)");
214-
using (sb.BeginBlock())
215-
{
216-
sb.AppendLine("continue;");
217-
}
218216
}
219217
}
220218

@@ -241,7 +239,10 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
241239
sb.AppendLine();
242240
}
243241

244-
sb.AppendLine("ThrowArgumentNameNotFound(name);");
242+
if (hasArgument)
243+
{
244+
sb.AppendLine("ThrowArgumentNameNotFound(name);");
245+
}
245246
}
246247

247248
// validate parsed

tests/ConsoleAppFramework.GeneratorTests/RunTest.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ public void OptionTokenHandlesParamsArguments()
6666
verifier.Execute(code, "--dry-run path.txt", "True:path.txt:");
6767
}
6868

69+
[Fact]
70+
public void ArgumentAllowsLeadingDashValue()
71+
{
72+
var code = """
73+
ConsoleApp.Run(args, ([Argument] int count, bool dryRun) =>
74+
{
75+
Console.Write((count, dryRun).ToString());
76+
});
77+
""";
78+
79+
verifier.Execute(code, "-5 --dry-run", "(-5, True)");
80+
verifier.Execute(code, "-5", "(-5, False)");
81+
}
82+
6983
[Fact]
7084
public void SyncRunShouldFailed()
7185
{
@@ -75,7 +89,7 @@ public void SyncRunShouldFailed()
7589
[Fact]
7690
public void MissingArgument()
7791
{
78-
verifier.Error("ConsoleApp.Run(args, (int x, int y) => { Console.Write((x + y)); });", "--x 10 y 20").ShouldContain("Argument 'y' is not recognized.");
92+
verifier.Error("ConsoleApp.Run(args, (int x, int y) => { Console.Write((x + y)); });", "--x 10 y 20").ShouldContain("Required argument 'y' was not specified.");
7993

8094
Environment.ExitCode.ShouldBe(1);
8195
Environment.ExitCode = 0;

0 commit comments

Comments
 (0)