diff --git a/src/CommandLineUtils/CommandArgument.cs b/src/CommandLineUtils/CommandArgument.cs index 31e1e763..88c6768a 100644 --- a/src/CommandLineUtils/CommandArgument.cs +++ b/src/CommandLineUtils/CommandArgument.cs @@ -59,5 +59,10 @@ public CommandArgument() /// When validation fails, is invoked. /// public ICollection Validators { get; } = new List(); + + internal void Reset() + { + Values.Clear(); + } } } diff --git a/src/CommandLineUtils/CommandLineApplication.cs b/src/CommandLineUtils/CommandLineApplication.cs index da4ecf2a..1e1210fd 100644 --- a/src/CommandLineUtils/CommandLineApplication.cs +++ b/src/CommandLineUtils/CommandLineApplication.cs @@ -707,6 +707,27 @@ public void OnExecuteAsync(Func> invoke) _handler = invoke; } + private void Reset() + { + foreach (var arg in Arguments) + { + arg.Reset(); + } + + foreach (var option in Options) + { + option.Reset(); + } + + foreach (var cmd in Commands) + { + cmd.Reset(); + } + + IsShowingInformation = default; + RemainingArguments.Clear(); + } + /// /// Adds an action to be invoked when all command line arguments have been parsed and validated. /// @@ -729,6 +750,8 @@ public void OnParsingComplete(Action action) /// The result of parsing. public ParseResult Parse(params string[] args) { + Reset(); + args ??= Util.EmptyArray(); var processor = new CommandLineProcessor(this, args); diff --git a/src/CommandLineUtils/CommandOption.cs b/src/CommandLineUtils/CommandOption.cs index f30371e5..8e524cf3 100644 --- a/src/CommandLineUtils/CommandOption.cs +++ b/src/CommandLineUtils/CommandOption.cs @@ -244,5 +244,10 @@ private bool IsEnglishLetter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } + + internal void Reset() + { + Values.Clear(); + } } } diff --git a/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs b/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs index ca4b369d..85cd96f3 100644 --- a/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs +++ b/test/CommandLineUtils.Tests/CommandLineApplicationTests.cs @@ -21,6 +21,29 @@ public CommandLineApplicationTests(ITestOutputHelper output) _output = output; } + [Fact] + public void CommandLineAppCanBeCalledTwice() + { + var app = new CommandLineApplication(new TestConsole(_output)); + var helpOption = app.HelpOption(inherited: true); + var verboseOption = app.VerboseOption(); + var subcmd = app.Command("test", _ => { }); + + app.Execute("test", "--help"); + Assert.True(app.IsShowingInformation); + Assert.True(subcmd.IsShowingInformation); + Assert.True(helpOption.HasValue()); + + app.Execute("-vvv"); + Assert.False(app.IsShowingInformation); + Assert.False(subcmd.IsShowingInformation); + Assert.False(helpOption.HasValue()); + Assert.Equal(3, verboseOption.Values.Count); + + app.Execute("test"); + Assert.Empty(verboseOption.Values); + } + [Fact] public void CommandNameCanBeMatched() {