Skip to content

Commit d48b4df

Browse files
committed
fix: reset state of commandline app, args, and options on each call of .Parse
Fixes #253
1 parent 6992e9b commit d48b4df

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/CommandLineUtils/CommandArgument.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ public CommandArgument()
5959
/// When validation fails, <see cref="CommandLineApplication.ValidationErrorHandler"/> is invoked.
6060
/// </summary>
6161
public ICollection<IArgumentValidator> Validators { get; } = new List<IArgumentValidator>();
62+
63+
internal void Reset()
64+
{
65+
Values.Clear();
66+
}
6267
}
6368
}

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,27 @@ public void OnExecuteAsync(Func<CancellationToken, Task<int>> invoke)
707707
_handler = invoke;
708708
}
709709

710+
private void Reset()
711+
{
712+
foreach (var arg in Arguments)
713+
{
714+
arg.Reset();
715+
}
716+
717+
foreach (var option in Options)
718+
{
719+
option.Reset();
720+
}
721+
722+
foreach (var cmd in Commands)
723+
{
724+
cmd.Reset();
725+
}
726+
727+
IsShowingInformation = default;
728+
RemainingArguments.Clear();
729+
}
730+
710731
/// <summary>
711732
/// Adds an action to be invoked when all command line arguments have been parsed and validated.
712733
/// </summary>
@@ -729,6 +750,8 @@ public void OnParsingComplete(Action<ParseResult> action)
729750
/// <returns>The result of parsing.</returns>
730751
public ParseResult Parse(params string[] args)
731752
{
753+
Reset();
754+
732755
args ??= Util.EmptyArray<string>();
733756

734757
var processor = new CommandLineProcessor(this, args);

src/CommandLineUtils/CommandOption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,10 @@ private bool IsEnglishLetter(char c)
244244
{
245245
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
246246
}
247+
248+
internal void Reset()
249+
{
250+
Values.Clear();
251+
}
247252
}
248253
}

test/CommandLineUtils.Tests/CommandLineApplicationTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ public CommandLineApplicationTests(ITestOutputHelper output)
2121
_output = output;
2222
}
2323

24+
[Fact]
25+
public void CommandLineAppCanBeCalledTwice()
26+
{
27+
var app = new CommandLineApplication(new TestConsole(_output));
28+
var helpOption = app.HelpOption(inherited: true);
29+
var verboseOption = app.VerboseOption();
30+
var subcmd = app.Command("test", _ => { });
31+
32+
app.Execute("test", "--help");
33+
Assert.True(app.IsShowingInformation);
34+
Assert.True(subcmd.IsShowingInformation);
35+
Assert.True(helpOption.HasValue());
36+
37+
app.Execute("-vvv");
38+
Assert.False(app.IsShowingInformation);
39+
Assert.False(subcmd.IsShowingInformation);
40+
Assert.False(helpOption.HasValue());
41+
Assert.Equal(3, verboseOption.Values.Count);
42+
43+
app.Execute("test");
44+
Assert.Empty(verboseOption.Values);
45+
}
46+
2447
[Fact]
2548
public void CommandNameCanBeMatched()
2649
{

0 commit comments

Comments
 (0)