Skip to content

Commit 5940dc1

Browse files
committed
Add CommandLineApplication.ThrowOnUnexpectedArgument and make public fields into properties
1 parent 52c370e commit 5940dc1

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,13 @@ namespace McMaster.Extensions.CommandLineUtils
1717
/// </summary>
1818
public class CommandLineApplication
1919
{
20-
// Indicates whether the parser should throw an exception when it runs into an unexpected argument.
21-
// If this field is set to false, the parser will stop parsing when it sees an unexpected argument, and all
22-
// remaining arguments, including the first unexpected argument, will be stored in RemainingArguments property.
23-
private readonly bool _throwOnUnexpectedArg;
24-
2520
/// <summary>
2621
/// Initializes a new instance of <see cref="CommandLineApplication"/>.
2722
/// </summary>
28-
/// <param name="throwOnUnexpectedArg">
29-
/// Defaults to true.
30-
/// Determines if <see cref="CommandParsingException"/> should be thrown when <see cref="Execute(string[])"/> receives an unrecognzed argument.
31-
/// </param>
23+
/// <param name="throwOnUnexpectedArg">Initial value for <see cref="ThrowOnUnexpectedArgument"/>.</param>
3224
public CommandLineApplication(bool throwOnUnexpectedArg = true)
3325
{
34-
_throwOnUnexpectedArg = throwOnUnexpectedArg;
26+
ThrowOnUnexpectedArgument = throwOnUnexpectedArg;
3527
Options = new List<CommandOption>();
3628
Arguments = new List<CommandArgument>();
3729
Commands = new List<CommandLineApplication>();
@@ -78,7 +70,7 @@ public CommandLineApplication(bool throwOnUnexpectedArg = true)
7870
/// <summary>
7971
/// Available command-line options on this command. Use <see cref="GetOptions"/> to get all available options, which may include inherited options.
8072
/// </summary>
81-
public readonly List<CommandOption> Options;
73+
public List<CommandOption> Options { get; private set; }
8274

8375
/// <summary>
8476
/// The option used to determine if help text should be displayed. This is set by calling <see cref="HelpOption(string)"/>.
@@ -93,12 +85,19 @@ public CommandLineApplication(bool throwOnUnexpectedArg = true)
9385
/// <summary>
9486
/// Required command-line arguments.
9587
/// </summary>
96-
public readonly List<CommandArgument> Arguments;
88+
public List<CommandArgument> Arguments { get; private set; }
89+
90+
/// <summary>
91+
/// When initialized with <see cref="ThrowOnUnexpectedArgument"/> to <c>false</c>, this will contain any unrecognized arguments.
92+
/// </summary>
93+
public List<string> RemainingArguments { get; private set; }
9794

9895
/// <summary>
99-
/// When initialized with throwOnUnexpectedArg to <c>false</c>, this will contain any unrecognized arguments.
96+
/// Indicates whether the parser should throw an exception when it runs into an unexpected argument.
97+
/// If this field is set to false, the parser will stop parsing when it sees an unexpected argument, and all
98+
/// remaining arguments, including the first unexpected argument, will be stored in RemainingArguments property.
10099
/// </summary>
101-
public readonly List<string> RemainingArguments;
100+
public bool ThrowOnUnexpectedArgument { get; set; }
102101

103102
/// <summary>
104103
/// True when <see cref="OptionHelp"/> or <see cref="OptionVersion"/> was matched.
@@ -122,7 +121,7 @@ public CommandLineApplication(bool throwOnUnexpectedArg = true)
122121
/// <summary>
123122
/// Subcommands.
124123
/// </summary>
125-
public readonly List<CommandLineApplication> Commands;
124+
public List<CommandLineApplication> Commands { get; private set; }
126125

127126
/// <summary>
128127
/// Determines if '--' can be used to separate known arguments and options from additional content passed to <see cref="RemainingArguments"/>.
@@ -316,7 +315,7 @@ public int Execute(params string[] args)
316315

317316
if (option == null)
318317
{
319-
if (string.IsNullOrEmpty(longOptionName) && !command._throwOnUnexpectedArg && AllowArgumentSeparator)
318+
if (string.IsNullOrEmpty(longOptionName) && !command.ThrowOnUnexpectedArgument && AllowArgumentSeparator)
320319
{
321320
// skip over the '--' argument separator
322321
index++;
@@ -692,7 +691,7 @@ public void ShowRootCommandFullNameAndVersion()
692691

693692
private void HandleUnexpectedArg(CommandLineApplication command, string[] args, int index, string argTypeName)
694693
{
695-
if (command._throwOnUnexpectedArg)
694+
if (command.ThrowOnUnexpectedArgument)
696695
{
697696
command.ShowHint();
698697
throw new CommandParsingException(command, $"Unrecognized {argTypeName} '{args[index]}'");

0 commit comments

Comments
 (0)