mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 21:38:16 +08:00
Make -v|--version
opt-in
We added an automatic version option in 0.49. We did this with good intentions, but forgot that people might already use --version as an option for a root command. This commit makes -v|--version completely opt-in.
This commit is contained in:

committed by
Patrik Svensson

parent
88515b7d7f
commit
3acc90e47c
@ -41,7 +41,7 @@ public class HelpProvider : IHelpProvider
|
||||
public bool Required { get; }
|
||||
public string? Description { get; }
|
||||
|
||||
public HelpArgument(string name, int position, bool required, string? description)
|
||||
private HelpArgument(string name, int position, bool required, string? description)
|
||||
{
|
||||
Name = name;
|
||||
Position = position;
|
||||
@ -68,7 +68,7 @@ public class HelpProvider : IHelpProvider
|
||||
public string? Description { get; }
|
||||
public object? DefaultValue { get; }
|
||||
|
||||
public HelpOption(string? @short, string? @long, string? @value, bool? valueIsOptional, string? description, object? defaultValue)
|
||||
private HelpOption(string? @short, string? @long, string? @value, bool? valueIsOptional, string? description, object? defaultValue)
|
||||
{
|
||||
Short = @short;
|
||||
Long = @long;
|
||||
@ -78,17 +78,27 @@ public class HelpProvider : IHelpProvider
|
||||
DefaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public static IReadOnlyList<HelpOption> Get(ICommandInfo? command, HelpProviderResources resources)
|
||||
public static IReadOnlyList<HelpOption> Get(
|
||||
ICommandModel model,
|
||||
ICommandInfo? command,
|
||||
HelpProviderResources resources)
|
||||
{
|
||||
var parameters = new List<HelpOption>();
|
||||
parameters.Add(new HelpOption("h", "help", null, null, resources.PrintHelpDescription, null));
|
||||
var parameters = new List<HelpOption>
|
||||
{
|
||||
new HelpOption("h", "help", null, null, resources.PrintHelpDescription, null),
|
||||
};
|
||||
|
||||
// Version information applies to the entire application
|
||||
// Include the "-v" option in the help when at the root of the command line application
|
||||
// Don't allow the "-v" option if users have specified one or more sub-commands
|
||||
if ((command == null || command?.Parent == null) && !(command?.IsBranch ?? false))
|
||||
if ((command?.Parent == null) && !(command?.IsBranch ?? false))
|
||||
{
|
||||
parameters.Add(new HelpOption("v", "version", null, null, resources.PrintVersionDescription, null));
|
||||
// Only show the version command if there is an
|
||||
// application version set.
|
||||
if (model.ApplicationVersion != null)
|
||||
{
|
||||
parameters.Add(new HelpOption("v", "version", null, null, resources.PrintVersionDescription, null));
|
||||
}
|
||||
}
|
||||
|
||||
parameters.AddRange(command?.Parameters.OfType<ICommandOption>().Where(o => !o.IsHidden).Select(o =>
|
||||
@ -101,11 +111,6 @@ public class HelpProvider : IHelpProvider
|
||||
}
|
||||
}
|
||||
|
||||
internal Composer NewComposer()
|
||||
{
|
||||
return new Composer(RenderMarkupInline);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HelpProvider"/> class.
|
||||
/// </summary>
|
||||
@ -383,7 +388,7 @@ public class HelpProvider : IHelpProvider
|
||||
public virtual IEnumerable<IRenderable> GetOptions(ICommandModel model, ICommandInfo? command)
|
||||
{
|
||||
// Collect all options into a single structure.
|
||||
var parameters = HelpOption.Get(command, resources);
|
||||
var parameters = HelpOption.Get(model, command, resources);
|
||||
if (parameters.Count == 0)
|
||||
{
|
||||
return Array.Empty<IRenderable>();
|
||||
@ -420,7 +425,7 @@ public class HelpProvider : IHelpProvider
|
||||
|
||||
if (defaultValueColumn)
|
||||
{
|
||||
columns.Add(GetOptionDefaultValue(option.DefaultValue));
|
||||
columns.Add(GetDefaultValueForOption(option.DefaultValue));
|
||||
}
|
||||
|
||||
columns.Add(NewComposer().Text(option.Description?.TrimEnd('.') ?? " "));
|
||||
@ -433,60 +438,6 @@ public class HelpProvider : IHelpProvider
|
||||
return result;
|
||||
}
|
||||
|
||||
private IRenderable GetOptionParts(HelpOption option)
|
||||
{
|
||||
var composer = NewComposer();
|
||||
|
||||
if (option.Short != null)
|
||||
{
|
||||
composer.Text("-").Text(option.Short);
|
||||
if (option.Long != null)
|
||||
{
|
||||
composer.Text(", ");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
composer.Text(" ");
|
||||
if (option.Long != null)
|
||||
{
|
||||
composer.Text(" ");
|
||||
}
|
||||
}
|
||||
|
||||
if (option.Long != null)
|
||||
{
|
||||
composer.Text("--").Text(option.Long);
|
||||
}
|
||||
|
||||
if (option.Value != null)
|
||||
{
|
||||
composer.Text(" ");
|
||||
if (option.ValueIsOptional ?? false)
|
||||
{
|
||||
composer.Style(helpStyles?.Options?.OptionalOption ?? Style.Plain, $"[{option.Value}]");
|
||||
}
|
||||
else
|
||||
{
|
||||
composer.Style(helpStyles?.Options?.RequiredOption ?? Style.Plain, $"<{option.Value}>");
|
||||
}
|
||||
}
|
||||
|
||||
return composer;
|
||||
}
|
||||
|
||||
private IRenderable GetOptionDefaultValue(object? defaultValue)
|
||||
{
|
||||
return defaultValue switch
|
||||
{
|
||||
null => NewComposer().Text(" "),
|
||||
"" => NewComposer().Text(" "),
|
||||
Array { Length: 0 } => NewComposer().Text(" "),
|
||||
Array array => NewComposer().Join(", ", array.Cast<object>().Select(o => NewComposer().Style(helpStyles?.Options?.DefaultValue ?? Style.Plain, o.ToString() ?? string.Empty))),
|
||||
_ => NewComposer().Style(helpStyles?.Options?.DefaultValue ?? Style.Plain, defaultValue?.ToString() ?? string.Empty),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the commands section of the help information.
|
||||
/// </summary>
|
||||
@ -556,4 +507,63 @@ public class HelpProvider : IHelpProvider
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
private Composer NewComposer()
|
||||
{
|
||||
return new Composer(RenderMarkupInline);
|
||||
}
|
||||
|
||||
private IRenderable GetOptionParts(HelpOption option)
|
||||
{
|
||||
var composer = NewComposer();
|
||||
|
||||
if (option.Short != null)
|
||||
{
|
||||
composer.Text("-").Text(option.Short);
|
||||
if (option.Long != null)
|
||||
{
|
||||
composer.Text(", ");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
composer.Text(" ");
|
||||
if (option.Long != null)
|
||||
{
|
||||
composer.Text(" ");
|
||||
}
|
||||
}
|
||||
|
||||
if (option.Long != null)
|
||||
{
|
||||
composer.Text("--").Text(option.Long);
|
||||
}
|
||||
|
||||
if (option.Value != null)
|
||||
{
|
||||
composer.Text(" ");
|
||||
if (option.ValueIsOptional ?? false)
|
||||
{
|
||||
composer.Style(helpStyles?.Options?.OptionalOption ?? Style.Plain, $"[{option.Value}]");
|
||||
}
|
||||
else
|
||||
{
|
||||
composer.Style(helpStyles?.Options?.RequiredOption ?? Style.Plain, $"<{option.Value}>");
|
||||
}
|
||||
}
|
||||
|
||||
return composer;
|
||||
}
|
||||
|
||||
private Composer GetDefaultValueForOption(object? defaultValue)
|
||||
{
|
||||
return defaultValue switch
|
||||
{
|
||||
null => NewComposer().Text(" "),
|
||||
"" => NewComposer().Text(" "),
|
||||
Array { Length: 0 } => NewComposer().Text(" "),
|
||||
Array array => NewComposer().Join(", ", array.Cast<object>().Select(o => NewComposer().Style(helpStyles?.Options?.DefaultValue ?? Style.Plain, o.ToString() ?? string.Empty))),
|
||||
_ => NewComposer().Style(helpStyles?.Options?.DefaultValue ?? Style.Plain, defaultValue?.ToString() ?? string.Empty),
|
||||
};
|
||||
}
|
||||
}
|
@ -9,4 +9,9 @@ public interface ICommandModel : ICommandContainer
|
||||
/// Gets the name of the application.
|
||||
/// </summary>
|
||||
string ApplicationName { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the version of the application.
|
||||
/// </summary>
|
||||
string? ApplicationVersion { get; }
|
||||
}
|
||||
|
@ -39,9 +39,12 @@ internal sealed class CommandExecutor
|
||||
if (firstArgument.Equals("--version", StringComparison.OrdinalIgnoreCase) ||
|
||||
firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var console = configuration.Settings.Console.GetConsole();
|
||||
console.WriteLine(ResolveApplicationVersion(configuration));
|
||||
return 0;
|
||||
if (configuration.Settings.ApplicationVersion != null)
|
||||
{
|
||||
var console = configuration.Settings.Console.GetConsole();
|
||||
console.MarkupLine(configuration.Settings.ApplicationVersion);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -126,13 +129,6 @@ internal sealed class CommandExecutor
|
||||
return parsedResult;
|
||||
}
|
||||
|
||||
private static string ResolveApplicationVersion(IConfiguration configuration)
|
||||
{
|
||||
return
|
||||
configuration.Settings.ApplicationVersion ?? // potential override
|
||||
VersionHelper.GetVersion(Assembly.GetEntryAssembly());
|
||||
}
|
||||
|
||||
private static async Task<int> Execute(
|
||||
CommandTree leaf,
|
||||
CommandTree tree,
|
||||
|
@ -3,6 +3,7 @@ namespace Spectre.Console.Cli;
|
||||
internal sealed class CommandModel : ICommandContainer, ICommandModel
|
||||
{
|
||||
public string? ApplicationName { get; }
|
||||
public string? ApplicationVersion { get; }
|
||||
public ParsingMode ParsingMode { get; }
|
||||
public IList<CommandInfo> Commands { get; }
|
||||
public IList<string[]> Examples { get; }
|
||||
@ -20,9 +21,10 @@ internal sealed class CommandModel : ICommandContainer, ICommandModel
|
||||
IEnumerable<string[]> examples)
|
||||
{
|
||||
ApplicationName = settings.ApplicationName;
|
||||
ApplicationVersion = settings.ApplicationVersion;
|
||||
ParsingMode = settings.ParsingMode;
|
||||
Commands = new List<CommandInfo>(commands ?? Array.Empty<CommandInfo>());
|
||||
Examples = new List<string[]>(examples ?? Array.Empty<string[]>());
|
||||
Commands = new List<CommandInfo>(commands);
|
||||
Examples = new List<string[]>(examples);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user