Skip to content

Commit

Permalink
Make -v|--version opt-in
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
patriksvensson committed Apr 25, 2024
1 parent 88515b7 commit 3acc90e
Show file tree
Hide file tree
Showing 36 changed files with 276 additions and 211 deletions.
146 changes: 78 additions & 68 deletions src/Spectre.Console.Cli/Help/HelpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private sealed class HelpArgument
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;
Expand All @@ -68,7 +68,7 @@ private sealed class HelpOption
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;
Expand All @@ -78,17 +78,27 @@ public HelpOption(string? @short, string? @long, string? @value, bool? valueIsOp
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 =>
Expand All @@ -101,11 +111,6 @@ public static IReadOnlyList<HelpOption> Get(ICommandInfo? command, HelpProviderR
}
}

internal Composer NewComposer()
{
return new Composer(RenderMarkupInline);
}

/// <summary>
/// Initializes a new instance of the <see cref="HelpProvider"/> class.
/// </summary>
Expand Down Expand Up @@ -383,7 +388,7 @@ public virtual IEnumerable<IRenderable> GetArguments(ICommandModel model, IComma
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>();
Expand Down Expand Up @@ -420,7 +425,7 @@ public virtual IEnumerable<IRenderable> GetOptions(ICommandModel model, ICommand

if (defaultValueColumn)
{
columns.Add(GetOptionDefaultValue(option.DefaultValue));
columns.Add(GetDefaultValueForOption(option.DefaultValue));
}

columns.Add(NewComposer().Text(option.Description?.TrimEnd('.') ?? " "));
Expand All @@ -433,60 +438,6 @@ public virtual IEnumerable<IRenderable> GetOptions(ICommandModel model, ICommand
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>
Expand Down Expand Up @@ -556,4 +507,63 @@ public virtual IEnumerable<IRenderable> GetFooter(ICommandModel model, ICommandI
{
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),
};
}
}
5 changes: 5 additions & 0 deletions src/Spectre.Console.Cli/Help/ICommandModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
16 changes: 6 additions & 10 deletions src/Spectre.Console.Cli/Internal/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ public async Task<int> Execute(IConfiguration configuration, IEnumerable<string>
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;
}
}
}
}
Expand Down Expand Up @@ -126,13 +129,6 @@ private CommandTreeParserResult ParseCommandLineArguments(CommandModel model, Co
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,
Expand Down
6 changes: 4 additions & 2 deletions src/Spectre.Console.Cli/Internal/Modelling/CommandModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -20,9 +21,10 @@ public CommandModel(
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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
USAGE:
USAGE:
myapp <FOO> <BAR> <BAZ> <CORGI> [QUX] [OPTIONS]

ARGUMENTS:
Expand All @@ -9,5 +9,4 @@ ARGUMENTS:
[QUX]

OPTIONS:
-h, --help Prints help information
-v, --version Prints version information
-h, --help Prints help information
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
--------------------------------------
--- CUSTOM HELP PROVIDER ---
--------------------------------------

USAGE:
myapp [OPTIONS] <COMMAND>

OPTIONS:
-h, --help Prints help information
-v, --version Prints version information

COMMANDS:
dog <AGE> The dog command

--------------------------------------
--- CUSTOM HELP PROVIDER ---
--------------------------------------

USAGE:
myapp [OPTIONS] <COMMAND>

OPTIONS:
-h, --help Prints help information

COMMANDS:
dog <AGE> The dog command

Version 1.0
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
--------------------------------------
--------------------------------------
--- CUSTOM HELP PROVIDER ---
--------------------------------------

USAGE:
myapp [OPTIONS] <COMMAND>

OPTIONS:
-h, --help Prints help information
-v, --version Prints version information
-h, --help Prints help information

COMMANDS:
dog <AGE> The dog command
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DESCRIPTION:
DESCRIPTION:
The lion command.

USAGE:
Expand All @@ -10,8 +10,7 @@ ARGUMENTS:

OPTIONS:
DEFAULT
-h, --help Prints help information
-v, --version Prints version information
-h, --help Prints help information
-a, --alive Indicates whether or not the animal is alive
-n, --name <VALUE>
--agility <VALUE> 10 The agility between 0 and 100
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DESCRIPTION:
DESCRIPTION:
The dog command.

USAGE:
Expand All @@ -18,7 +18,6 @@ ARGUMENTS:

OPTIONS:
-h, --help Prints help information
-v, --version Prints version information
-a, --alive Indicates whether or not the animal is alive
-n, --name <VALUE>
-g, --good-boy
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DESCRIPTION:
DESCRIPTION:
The lion command.

USAGE:
Expand All @@ -10,8 +10,7 @@ ARGUMENTS:

OPTIONS:
DEFAULT
-h, --help Prints help information
-v, --version Prints version information
-h, --help Prints help information
-a, --alive Indicates whether or not the animal is alive
-n, --name <VALUE>
--agility <VALUE> 10 The agility between 0 and 100
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BESCHREIBUNG:
BESCHREIBUNG:
The lion command.

VERWENDUNG:
Expand All @@ -14,7 +14,6 @@ ARGUMENTE:
OPTIONEN:
STANDARDWERT
-h, --help Zeigt Hilfe an
-v, --version Zeigt Versionsinformationen an
-a, --alive Indicates whether or not the animal is alive
-n, --name <VALUE>
--agility <VALUE> 10 The agility between 0 and 100
Expand Down
Loading

0 comments on commit 3acc90e

Please sign in to comment.