mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Improve preview directive
This commit is contained in:
@@ -47,35 +47,51 @@ namespace CliFx
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int? HandlePreviewDirective(CommandLineInput commandLineInput)
|
private int? HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||||
{
|
{
|
||||||
var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified;
|
var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified;
|
||||||
if (!isPreviewMode)
|
if (!isPreviewMode)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
// Render command name
|
var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset);
|
||||||
_console.Output.WriteLine($"Arguments: {string.Join(" ", commandLineInput.Arguments)}");
|
|
||||||
_console.Output.WriteLine();
|
|
||||||
|
|
||||||
// Render directives
|
_console.Output.WriteLine("Parser preview:");
|
||||||
_console.Output.WriteLine("Directives:");
|
|
||||||
foreach (var directive in commandLineInput.Directives)
|
// Command name
|
||||||
|
if (commandSchema != null && argumentOffset > 0)
|
||||||
{
|
{
|
||||||
_console.Output.Write(" ");
|
_console.WithForegroundColor(ConsoleColor.Cyan, () =>
|
||||||
_console.Output.WriteLine(directive);
|
_console.Output.Write(commandSchema.Name));
|
||||||
|
|
||||||
|
_console.Output.Write(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Margin
|
// Parameters
|
||||||
_console.Output.WriteLine();
|
foreach (var parameter in commandLineInput.Arguments.Skip(argumentOffset))
|
||||||
|
{
|
||||||
|
_console.Output.Write('<');
|
||||||
|
|
||||||
// Render options
|
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||||
_console.Output.WriteLine("Options:");
|
_console.Output.Write(parameter));
|
||||||
|
|
||||||
|
_console.Output.Write('>');
|
||||||
|
_console.Output.Write(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options
|
||||||
foreach (var option in commandLineInput.Options)
|
foreach (var option in commandLineInput.Options)
|
||||||
{
|
{
|
||||||
_console.Output.Write(" ");
|
_console.Output.Write('[');
|
||||||
_console.Output.WriteLine(option);
|
|
||||||
|
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||||
|
_console.Output.Write(option));
|
||||||
|
|
||||||
|
_console.Output.Write(']');
|
||||||
|
_console.Output.Write(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_console.Output.WriteLine();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,9 +107,7 @@ namespace CliFx
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int? HandleHelpOption(
|
private int? HandleHelpOption(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||||
ApplicationSchema applicationSchema,
|
|
||||||
CommandLineInput commandLineInput)
|
|
||||||
{
|
{
|
||||||
// Help is rendered either when it's requested or when the user provides no arguments and there is no default command
|
// Help is rendered either when it's requested or when the user provides no arguments and there is no default command
|
||||||
var shouldRenderHelp =
|
var shouldRenderHelp =
|
||||||
@@ -139,7 +153,7 @@ namespace CliFx
|
|||||||
|
|
||||||
return
|
return
|
||||||
await HandleDebugDirectiveAsync(commandLineInput) ??
|
await HandleDebugDirectiveAsync(commandLineInput) ??
|
||||||
HandlePreviewDirective(commandLineInput) ??
|
HandlePreviewDirective(applicationSchema, commandLineInput) ??
|
||||||
HandleVersionOption(commandLineInput) ??
|
HandleVersionOption(commandLineInput) ??
|
||||||
HandleHelpOption(applicationSchema, commandLineInput) ??
|
HandleHelpOption(applicationSchema, commandLineInput) ??
|
||||||
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
|
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
|
||||||
|
|||||||
@@ -43,7 +43,8 @@ namespace CliFx.Domain
|
|||||||
? Commands.Where(c => TryFindParentCommand(c.Name)?.MatchesName(parentCommandName) == true).ToArray()
|
? Commands.Where(c => TryFindParentCommand(c.Name)?.MatchesName(parentCommandName) == true).ToArray()
|
||||||
: Commands.Where(c => !string.IsNullOrWhiteSpace(c.Name) && TryFindParentCommand(c.Name) == null).ToArray();
|
: Commands.Where(c => !string.IsNullOrWhiteSpace(c.Name) && TryFindParentCommand(c.Name) == null).ToArray();
|
||||||
|
|
||||||
private CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset)
|
// TODO: this out parameter is not a really nice design
|
||||||
|
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset)
|
||||||
{
|
{
|
||||||
// Try to find the command that contains the most of the input arguments in its name
|
// Try to find the command that contains the most of the input arguments in its name
|
||||||
for (var i = commandLineInput.Arguments.Count; i >= 0; i--)
|
for (var i = commandLineInput.Arguments.Count; i >= 0; i--)
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ namespace CliFx.Domain
|
|||||||
private static readonly IFormatProvider ConversionFormatProvider = CultureInfo.InvariantCulture;
|
private static readonly IFormatProvider ConversionFormatProvider = CultureInfo.InvariantCulture;
|
||||||
|
|
||||||
private static readonly IReadOnlyDictionary<Type, Func<string, object>> PrimitiveConverters =
|
private static readonly IReadOnlyDictionary<Type, Func<string, object>> PrimitiveConverters =
|
||||||
new Dictionary<Type, Func<string, object>>
|
new Dictionary<Type, Func<string?, object>>
|
||||||
{
|
{
|
||||||
[typeof(object)] = v => v,
|
[typeof(object)] = v => v,
|
||||||
[typeof(string)] = v => v,
|
[typeof(string)] = v => v,
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ namespace CliFx.Domain
|
|||||||
IsRequired = isRequired;
|
IsRequired = isRequired;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool MatchesName(string name) =>
|
public bool MatchesName(string? name) =>
|
||||||
!string.IsNullOrWhiteSpace(Name) &&
|
!string.IsNullOrWhiteSpace(Name) &&
|
||||||
string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
|
string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ namespace CliFx.Domain
|
|||||||
Parameters = parameters;
|
Parameters = parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool MatchesName(string name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase);
|
public bool MatchesName(string? name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
private void InjectParameters(ICommand command, IReadOnlyList<string> parameterInputs)
|
private void InjectParameters(ICommand command, IReadOnlyList<string> parameterInputs)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user