Skip to content

Commit ed9f37a

Browse files
committed
adds support for command options
Extends the command-line interface generator to support additional options for commands. This enhancement allows specifying options with their descriptions, aliases, and required status, improving flexibility.
1 parent 263ef09 commit ed9f37a

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

new-cli/.run/TestCommand.run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<component name="ProjectRunConfigurationManager">
22
<configuration default="false" name="TestCommand" type="DotNetProject" factoryName=".NET Project">
33
<option name="EXE_PATH" value="$PROJECT_DIR$/GitVersion.Cli/bin/Debug/net9.0/gitversion" />
4-
<option name="PROGRAM_PARAMETERS" value="test --input-file test.txt" />
4+
<option name="PROGRAM_PARAMETERS" value="test -i test.txt" />
55
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
66
<option name="PASS_PARENT_ENVS" value="1" />
77
<option name="USE_EXTERNAL_CONSOLE" value="0" />

new-cli/GitVersion.Cli.Generator.Tests/SystemCommandlineGeneratorTests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,38 @@ public class TestCommandImpl : Command, ICommandImpl
4040
public string CommandImplName => nameof(TestCommandImpl);
4141
public string ParentCommandImplName => string.Empty;
4242
// Options list
43+
protected readonly Option<System.IO.FileInfo?> LogFileOption;
4344
protected readonly Option<string> OutputFileOption;
45+
protected readonly Option<GitVersion.Infrastructure.Verbosity?> VerbosityOption;
46+
protected readonly Option<System.IO.DirectoryInfo?> WorkDirOption;
4447
4548
public TestCommandImpl(TestCommand command)
4649
: base("test", "Test description.")
4750
{
48-
OutputFileOption = new Option<string>("--output-file", [])
51+
LogFileOption = new Option<System.IO.FileInfo?>("--log-file", "-l")
52+
{
53+
Required = false,
54+
Description = "The log file",
55+
};
56+
OutputFileOption = new Option<string>("--output-file")
4957
{
5058
Required = true,
5159
Description = "The output file",
5260
};
61+
VerbosityOption = new Option<GitVersion.Infrastructure.Verbosity?>("--verbosity")
62+
{
63+
Required = false,
64+
Description = "The verbosity of the logging information",
65+
};
66+
WorkDirOption = new Option<System.IO.DirectoryInfo?>("--work-dir")
67+
{
68+
Required = false,
69+
Description = "The working directory with the git repository",
70+
};
71+
Add(LogFileOption);
5372
Add(OutputFileOption);
73+
Add(VerbosityOption);
74+
Add(WorkDirOption);
5475
5576
this.SetAction(Run);
5677
return;
@@ -59,7 +80,10 @@ Task<int> Run(ParseResult parseResult, CancellationToken cancellationToken)
5980
{
6081
var settings = new TestCommandSettings
6182
{
83+
LogFile = parseResult.GetValue(LogFileOption),
6284
OutputFile = parseResult.GetValue(OutputFileOption)!,
85+
Verbosity = parseResult.GetValue(VerbosityOption),
86+
WorkDir = parseResult.GetValue(WorkDirOption),
6387
};
6488
return command.InvokeAsync(settings, cancellationToken);
6589
}
@@ -198,7 +222,7 @@ public Task<int> InvokeAsync(TestCommandSettings settings, CancellationToken can
198222
199223
namespace {{Constants.CommandNamespaceName}};
200224
201-
public record TestCommandSettings
225+
public record TestCommandSettings : GitVersionSettings
202226
{
203227
[Option("--output-file", "The output file")]
204228
public required string OutputFile { get; init; }

new-cli/GitVersion.Cli.Generator/CommandBaseGenerator.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,13 @@ private static SettingsPropertyInfo MapToPropertyInfo(IPropertySymbol propertySy
9292
name.NotNull();
9393
description.NotNull();
9494

95-
var alias = string.Empty;
95+
string[] aliases = [];
9696
if (ctorArguments.Length == 3)
9797
{
9898
var aliasesArgs = ctorArguments[2];
99-
var aliases = (aliasesArgs.Kind == TypedConstantKind.Array
99+
aliases = (aliasesArgs.Kind == TypedConstantKind.Array
100100
? aliasesArgs.Values.Select(x => Convert.ToString(x.Value)).ToArray()
101-
: [Convert.ToString(aliasesArgs.Value)]).Select(x => $@"""{x?.Trim()}""");
102-
alias = string.Join(", ", aliases);
101+
: [Convert.ToString(aliasesArgs.Value)]);
103102
}
104103

105104
var isRequired = propertySymbol.IsRequired;
@@ -108,7 +107,7 @@ private static SettingsPropertyInfo MapToPropertyInfo(IPropertySymbol propertySy
108107
Name = propertySymbol.Name,
109108
TypeName = propertySymbol.Type.ToDisplayString(),
110109
OptionName = name,
111-
Aliases = alias,
110+
Aliases = aliases,
112111
Description = description,
113112
Required = isRequired
114113
};

new-cli/GitVersion.Cli.Generator/Models.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal record SettingsPropertyInfo
1717
public required string Name { get; init; }
1818
public required string TypeName { get; init; }
1919
public required string OptionName { get; init; }
20-
public required string Aliases { get; init; }
20+
public required string[] Aliases { get; init; }
2121
public required string Description { get; init; }
2222
public required bool Required { get; init; }
2323
}

new-cli/GitVersion.Cli.Generator/SystemCommandlineContent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class {{Model.CommandTypeName}}Impl : Command, ICommandImpl
3030
: base("{{Model.CommandName}}", "{{Model.CommandDescription}}")
3131
{
3232
{{~ for $prop in $settingsProperties ~}}
33-
{{$prop.Name}}Option = new Option<{{$prop.TypeName}}>("{{$prop.OptionName}}", [{{$prop.Aliases}}])
33+
{{$prop.Name}}Option = new Option<{{$prop.TypeName}}>("{{$prop.OptionName}}"{{if $prop.Aliases.size == 0}}{{else}}, {{for $alias in $prop.Aliases}}"{{$alias}}"{{if !for.last}}, {{end}}{{end}}{{end}})
3434
{
3535
Required = {{$prop.Required}},
3636
Description = "{{$prop.Description}}",

0 commit comments

Comments
 (0)