mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-07-07 13:08:14 +08:00
* Support command description localization
eg. [CommandOption("-a|--args")] [Description(nameof(Str.GitArgs))] [Localization(typeof(Str))] public string Args { get; set; }
This commit is contained in:
@ -12,7 +12,7 @@ internal sealed class CommandExecutor
|
||||
_registrar.Register(typeof(DefaultPairDeconstructor), typeof(DefaultPairDeconstructor));
|
||||
}
|
||||
|
||||
public async Task<int> Execute(IConfiguration configuration, IEnumerable<string> args)
|
||||
public async Task<int> ExecuteAsync(IConfiguration configuration, IEnumerable<string> args)
|
||||
{
|
||||
CommandTreeParserResult parsedResult;
|
||||
|
||||
@ -118,7 +118,7 @@ internal sealed class CommandExecutor
|
||||
leaf.Command.Data);
|
||||
|
||||
// Execute the command tree.
|
||||
return await Execute(leaf, parsedResult.Tree, context, resolver, configuration).ConfigureAwait(false);
|
||||
return await ExecuteAsync(leaf, parsedResult.Tree, context, resolver, configuration).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -215,7 +215,7 @@ internal sealed class CommandExecutor
|
||||
return (parsedResult, tokenizerResult);
|
||||
}
|
||||
|
||||
private static async Task<int> Execute(
|
||||
private static async Task<int> ExecuteAsync(
|
||||
CommandTree leaf,
|
||||
CommandTree tree,
|
||||
CommandContext context,
|
||||
@ -249,7 +249,7 @@ internal sealed class CommandExecutor
|
||||
}
|
||||
|
||||
// Execute the command.
|
||||
var result = await command.Execute(context, settings);
|
||||
var result = await command.ExecuteAsync(context, settings);
|
||||
foreach (var interceptor in interceptors)
|
||||
{
|
||||
interceptor.InterceptResult(context, settings, ref result);
|
||||
|
@ -9,7 +9,7 @@ internal sealed class DelegateCommand : ICommand
|
||||
_func = func;
|
||||
}
|
||||
|
||||
public Task<int> Execute(CommandContext context, CommandSettings settings)
|
||||
public Task<int> ExecuteAsync(CommandContext context, CommandSettings settings)
|
||||
{
|
||||
return _func(context, settings);
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
internal static class LocalizationExtensions
|
||||
{
|
||||
public static string? LocalizedDescription(this MemberInfo me)
|
||||
{
|
||||
var description = me.GetCustomAttribute<DescriptionAttribute>();
|
||||
if (description is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var localization = me.GetCustomAttribute<LocalizationAttribute>();
|
||||
string? localizedText;
|
||||
return (localizedText = localization?.ResourceClass
|
||||
.GetProperty(description.Description)?
|
||||
.GetValue(default) as string) != null
|
||||
? localizedText
|
||||
: description.Description;
|
||||
}
|
||||
}
|
@ -48,10 +48,10 @@ internal sealed class CommandInfo : ICommandContainer, ICommandInfo
|
||||
|
||||
if (CommandType != null && string.IsNullOrWhiteSpace(Description))
|
||||
{
|
||||
var description = CommandType.GetCustomAttribute<DescriptionAttribute>();
|
||||
var description = CommandType.LocalizedDescription();
|
||||
if (description != null)
|
||||
{
|
||||
Description = description.Description;
|
||||
Description = description;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -174,7 +174,7 @@ internal static class CommandModelBuilder
|
||||
|
||||
private static CommandOption BuildOptionParameter(PropertyInfo property, CommandOptionAttribute attribute)
|
||||
{
|
||||
var description = property.GetCustomAttribute<DescriptionAttribute>();
|
||||
var description = property.LocalizedDescription();
|
||||
var converter = property.GetCustomAttribute<TypeConverterAttribute>();
|
||||
var deconstructor = property.GetCustomAttribute<PairDeconstructorAttribute>();
|
||||
var valueProvider = property.GetCustomAttribute<ParameterValueProviderAttribute>();
|
||||
@ -189,14 +189,14 @@ internal static class CommandModelBuilder
|
||||
}
|
||||
|
||||
return new CommandOption(property.PropertyType, kind,
|
||||
property, description?.Description, converter, deconstructor,
|
||||
property, description, converter, deconstructor,
|
||||
attribute, valueProvider, validators, defaultValue,
|
||||
attribute.ValueIsOptional);
|
||||
}
|
||||
|
||||
private static CommandArgument BuildArgumentParameter(PropertyInfo property, CommandArgumentAttribute attribute)
|
||||
{
|
||||
var description = property.GetCustomAttribute<DescriptionAttribute>();
|
||||
var description = property.LocalizedDescription();
|
||||
var converter = property.GetCustomAttribute<TypeConverterAttribute>();
|
||||
var defaultValue = property.GetCustomAttribute<DefaultValueAttribute>();
|
||||
var valueProvider = property.GetCustomAttribute<ParameterValueProviderAttribute>();
|
||||
@ -206,7 +206,7 @@ internal static class CommandModelBuilder
|
||||
|
||||
return new CommandArgument(
|
||||
property.PropertyType, kind, property,
|
||||
description?.Description, converter,
|
||||
description, converter,
|
||||
defaultValue, attribute, valueProvider,
|
||||
validators);
|
||||
}
|
||||
|
Reference in New Issue
Block a user