Skip to content

Commit 42b74b1

Browse files
Copilotarturcic
andcommitted
Enhance SpectreArgumentParser with comprehensive CLI argument support based on documentation
Co-authored-by: arturcic <[email protected]>
1 parent 242709c commit 42b74b1

File tree

1 file changed

+161
-5
lines changed

1 file changed

+161
-5
lines changed

src/GitVersion.App/SpectreArgumentParser.cs

Lines changed: 161 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,157 @@ public Arguments ParseArguments(string[] commandLineArguments)
9090
}
9191
catch (Exception)
9292
{
93-
// If parsing fails, try to handle as legacy format or single target path
93+
// If parsing fails, try to handle legacy forward slash syntax
94+
return HandleLegacySyntax(commandLineArguments);
95+
}
96+
97+
return CreateDefaultArguments();
98+
}
99+
100+
private Arguments HandleLegacySyntax(string[] commandLineArguments)
101+
{
102+
// Convert legacy forward slash syntax to hyphen syntax for Spectre.Console.Cli
103+
var convertedArgs = new List<string>();
104+
105+
for (int i = 0; i < commandLineArguments.Length; i++)
106+
{
107+
var arg = commandLineArguments[i];
108+
109+
// Handle legacy forward slash options
110+
if (arg.StartsWith('/'))
111+
{
112+
var option = arg.Substring(1).ToLowerInvariant();
113+
switch (option)
114+
{
115+
case "output":
116+
convertedArgs.Add("--output");
117+
break;
118+
case "outputfile":
119+
convertedArgs.Add("--output-file");
120+
break;
121+
case "showvariable":
122+
convertedArgs.Add("--show-variable");
123+
break;
124+
case "format":
125+
convertedArgs.Add("--format");
126+
break;
127+
case "l":
128+
convertedArgs.Add("--log-file");
129+
break;
130+
case "config":
131+
convertedArgs.Add("--config");
132+
break;
133+
case "showconfig":
134+
convertedArgs.Add("--show-config");
135+
break;
136+
case "overrideconfig":
137+
convertedArgs.Add("--override-config");
138+
// Handle the key=value format for override config
139+
if (i + 1 < commandLineArguments.Length)
140+
{
141+
var nextArg = commandLineArguments[i + 1];
142+
if (nextArg.Contains('=') && !nextArg.StartsWith('/') && !nextArg.StartsWith('-'))
143+
{
144+
// This is the key=value pair for override config
145+
convertedArgs.Add(nextArg);
146+
i++; // Skip the next argument since we consumed it
147+
}
148+
}
149+
break;
150+
case "nocache":
151+
convertedArgs.Add("--no-cache");
152+
break;
153+
case "nonormalize":
154+
convertedArgs.Add("--no-normalize");
155+
break;
156+
case "allowshallow":
157+
convertedArgs.Add("--allow-shallow");
158+
break;
159+
case "verbosity":
160+
convertedArgs.Add("--verbosity");
161+
break;
162+
case "updateassemblyinfo":
163+
convertedArgs.Add("--update-assembly-info");
164+
break;
165+
case "updateprojectfiles":
166+
convertedArgs.Add("--update-project-files");
167+
break;
168+
case "ensureassemblyinfo":
169+
convertedArgs.Add("--ensure-assembly-info");
170+
break;
171+
case "updatewixversionfile":
172+
convertedArgs.Add("--update-wix-version-file");
173+
break;
174+
case "url":
175+
convertedArgs.Add("--url");
176+
break;
177+
case "b":
178+
convertedArgs.Add("--branch");
179+
break;
180+
case "u":
181+
convertedArgs.Add("--username");
182+
break;
183+
case "p":
184+
convertedArgs.Add("--password");
185+
break;
186+
case "c":
187+
convertedArgs.Add("--commit");
188+
break;
189+
case "dynamicrepolocation":
190+
convertedArgs.Add("--dynamic-repo-location");
191+
break;
192+
case "nofetch":
193+
convertedArgs.Add("--no-fetch");
194+
break;
195+
case "targetpath":
196+
convertedArgs.Add("--target-path");
197+
break;
198+
case "diag":
199+
convertedArgs.Add("--diag");
200+
break;
201+
default:
202+
// Unknown option, keep as is
203+
convertedArgs.Add(arg);
204+
break;
205+
}
206+
}
207+
else if (!arg.StartsWith('-') && i == 0)
208+
{
209+
// First non-option argument is likely the target path
210+
convertedArgs.Add(arg);
211+
}
212+
else
213+
{
214+
// Regular argument or already in correct format
215+
convertedArgs.Add(arg);
216+
}
217+
}
218+
219+
// Try parsing again with converted arguments
220+
try
221+
{
222+
var app = new CommandApp<GitVersionCommand>();
223+
app.Configure(config =>
224+
{
225+
config.SetApplicationName("gitversion");
226+
config.PropagateExceptions();
227+
});
228+
229+
var resultStorage = new ParseResultStorage();
230+
var interceptor = new ArgumentInterceptor(resultStorage, this.environment, this.fileSystem, this.buildAgent, this.console, this.globbingResolver);
231+
app.Configure(config => config.Settings.Interceptor = interceptor);
232+
233+
var parseResult = app.Run(convertedArgs.ToArray());
234+
235+
var result = resultStorage.GetResult();
236+
if (result != null)
237+
{
238+
return result;
239+
}
240+
}
241+
catch (Exception)
242+
{
243+
// Final fallback - if it's a single argument and not an option, treat as target path
94244
if (commandLineArguments.Length == 1 && !commandLineArguments[0].StartsWith('-') && !commandLineArguments[0].StartsWith('/'))
95245
{
96246
return CreateArgumentsWithTargetPath(commandLineArguments[0]);
@@ -275,8 +425,10 @@ private static Arguments ConvertToArguments(GitVersionSettings settings)
275425
{
276426
var arguments = new Arguments();
277427

278-
// Set target path
279-
arguments.TargetPath = settings.TargetPath?.TrimEnd('/', '\\') ?? SysEnv.CurrentDirectory;
428+
// Set target path - prioritize explicit targetpath option over positional argument
429+
arguments.TargetPath = settings.TargetPathOption?.TrimEnd('/', '\\')
430+
?? settings.TargetPath?.TrimEnd('/', '\\')
431+
?? SysEnv.CurrentDirectory;
280432

281433
// Configuration options
282434
arguments.ConfigurationFile = settings.ConfigurationFile;
@@ -382,7 +534,7 @@ internal class GitVersionSettings : CommandSettings
382534
[Description("Path to the Git repository (defaults to current directory)")]
383535
public string? TargetPath { get; set; }
384536

385-
[CommandOption("-c|--config")]
537+
[CommandOption("--config")]
386538
[Description("Path to GitVersion configuration file")]
387539
public string? ConfigurationFile { get; set; }
388540

@@ -418,10 +570,14 @@ internal class GitVersionSettings : CommandSettings
418570
[Description("Target branch name")]
419571
public string? Branch { get; set; }
420572

421-
[CommandOption("--commit")]
573+
[CommandOption("-c|--commit")]
422574
[Description("Target commit SHA")]
423575
public string? Commit { get; set; }
424576

577+
[CommandOption("--target-path")]
578+
[Description("Same as positional path argument")]
579+
public string? TargetPathOption { get; set; }
580+
425581
[CommandOption("--dynamic-repo-location")]
426582
[Description("Path to clone remote repository")]
427583
public string? DynamicRepoLocation { get; set; }

0 commit comments

Comments
 (0)