Skip to content

Commit 76d27c0

Browse files
committed
refactor(cli): Extract argument validation and reorganize parsing logic
1 parent 106839c commit 76d27c0

1 file changed

Lines changed: 43 additions & 36 deletions

File tree

src/GitVersion.App/ArgumentParser.cs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,7 @@ public Arguments ParseArguments(string[] commandLineArguments)
4949
var (rootCommand, options) = commandFactory.Value;
5050
var parseResult = rootCommand.Parse(commandLineArguments);
5151

52-
if (parseResult.Errors.Count > 0)
53-
{
54-
var message = parseResult.Errors[0].Message;
55-
var token = message.Contains("Unrecognized command or argument")
56-
? ExtractUnrecognizedToken(message)
57-
: message;
58-
throw new WarningException($"Could not parse command line parameter '{token}'.");
59-
}
60-
61-
if (parseResult.UnmatchedTokens.Count > 0)
62-
{
63-
throw new WarningException($"Could not parse command line parameter '{parseResult.UnmatchedTokens[0]}'.");
64-
}
65-
66-
var positionalCheck = parseResult.GetValue(options.Path);
67-
if (positionalCheck?.StartsWith('-') == true)
68-
{
69-
throw new WarningException($"Could not parse command line parameter '{positionalCheck}'.");
70-
}
52+
ValidateParsedResult(parseResult, options);
7153

7254
if (IsOptionExplicitlySet<HelpOption>())
7355
{
@@ -83,33 +65,37 @@ public Arguments ParseArguments(string[] commandLineArguments)
8365
var arguments = new Arguments();
8466
AddAuthentication(arguments);
8567
MapParsedValues(arguments, parseResult, options);
68+
ValidateConfigurationFile(arguments);
8669

87-
if (arguments.Output.Count == 0)
70+
return arguments;
71+
72+
bool IsOptionExplicitlySet<T>() where T : Option
8873
{
89-
arguments.Output.Add(OutputType.Json);
74+
var option = rootCommand.Options.SingleOfType<T>();
75+
return parseResult.GetResult(option) is { Implicit: false };
9076
}
77+
}
9178

92-
if (arguments.Output.Contains(OutputType.File) && arguments.OutputFile == null)
79+
private static void ValidateParsedResult(ParseResult parseResult, CommandOptions options)
80+
{
81+
if (parseResult.Errors.Count > 0)
9382
{
94-
arguments.OutputFile = DefaultOutputFileName;
83+
var message = parseResult.Errors[0].Message;
84+
var token = message.Contains("Unrecognized command or argument")
85+
? ExtractUnrecognizedToken(message)
86+
: message;
87+
throw new WarningException($"Could not parse command line parameter '{token}'.");
9588
}
9689

97-
arguments.TargetPath ??= parseResult.GetValue(options.Path) ?? SysEnv.CurrentDirectory;
98-
arguments.TargetPath = arguments.TargetPath.TrimEnd('/', '\\');
99-
100-
if (!arguments.EnsureAssemblyInfo)
90+
if (parseResult.UnmatchedTokens.Count > 0)
10191
{
102-
arguments.UpdateAssemblyInfoFileName = ResolveFiles(arguments.TargetPath, arguments.UpdateAssemblyInfoFileName).ToHashSet();
92+
throw new WarningException($"Could not parse command line parameter '{parseResult.UnmatchedTokens[0]}'.");
10393
}
10494

105-
ValidateConfigurationFile(arguments);
106-
107-
return arguments;
108-
109-
bool IsOptionExplicitlySet<T>() where T : Option
95+
var positionalCheck = parseResult.GetValue(options.Path);
96+
if (positionalCheck?.StartsWith('-') == true)
11097
{
111-
var option = rootCommand.Options.SingleOfType<T>();
112-
return parseResult.GetResult(option) is { Implicit: false };
98+
throw new WarningException($"Could not parse command line parameter '{positionalCheck}'.");
11399
}
114100
}
115101

@@ -270,6 +256,24 @@ private void MapParsedValues(Arguments arguments, ParseResult parseResult, Comma
270256
{
271257
arguments.ClonePath = dynRepo;
272258
}
259+
260+
if (arguments.Output.Count == 0)
261+
{
262+
arguments.Output.Add(OutputType.Json);
263+
}
264+
265+
if (arguments.Output.Contains(OutputType.File) && arguments.OutputFile == null)
266+
{
267+
arguments.OutputFile = DefaultOutputFileName;
268+
}
269+
270+
arguments.TargetPath ??= parseResult.GetValue(options.Path) ?? SysEnv.CurrentDirectory;
271+
arguments.TargetPath = arguments.TargetPath.TrimEnd('/', '\\');
272+
273+
if (!arguments.EnsureAssemblyInfo)
274+
{
275+
arguments.UpdateAssemblyInfoFileName = ResolveFiles(arguments.TargetPath, arguments.UpdateAssemblyInfoFileName).ToHashSet();
276+
}
273277
}
274278

275279
private static bool IsBooleanTrue(string value) =>
@@ -442,7 +446,10 @@ Allows GitVersion to run on a shallow clone.
442446

443447
// Configure the built-in help system to wrap at 260 characters to avoid too small help messages
444448
var helpOption = rootCommand.Options.SingleOfType<HelpOption>();
445-
helpOption.Action = new HelpAction { MaxWidth = 260 };
449+
if (helpOption.Action is HelpAction helpAction)
450+
{
451+
helpAction.MaxWidth = 260;
452+
}
446453

447454
return (rootCommand, new CommandOptions(
448455
Path: path, Diagnose: diagnose, LogFile: logFile,

0 commit comments

Comments
 (0)