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;
|
||||
}
|
||||
|
||||
private int? HandlePreviewDirective(CommandLineInput commandLineInput)
|
||||
private int? HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||
{
|
||||
var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified;
|
||||
if (!isPreviewMode)
|
||||
return null;
|
||||
|
||||
// Render command name
|
||||
_console.Output.WriteLine($"Arguments: {string.Join(" ", commandLineInput.Arguments)}");
|
||||
_console.Output.WriteLine();
|
||||
var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset);
|
||||
|
||||
// Render directives
|
||||
_console.Output.WriteLine("Directives:");
|
||||
foreach (var directive in commandLineInput.Directives)
|
||||
_console.Output.WriteLine("Parser preview:");
|
||||
|
||||
// Command name
|
||||
if (commandSchema != null && argumentOffset > 0)
|
||||
{
|
||||
_console.Output.Write(" ");
|
||||
_console.Output.WriteLine(directive);
|
||||
_console.WithForegroundColor(ConsoleColor.Cyan, () =>
|
||||
_console.Output.Write(commandSchema.Name));
|
||||
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
// Margin
|
||||
_console.Output.WriteLine();
|
||||
// Parameters
|
||||
foreach (var parameter in commandLineInput.Arguments.Skip(argumentOffset))
|
||||
{
|
||||
_console.Output.Write('<');
|
||||
|
||||
// Render options
|
||||
_console.Output.WriteLine("Options:");
|
||||
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||
_console.Output.Write(parameter));
|
||||
|
||||
_console.Output.Write('>');
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
// Options
|
||||
foreach (var option in commandLineInput.Options)
|
||||
{
|
||||
_console.Output.Write(" ");
|
||||
_console.Output.WriteLine(option);
|
||||
_console.Output.Write('[');
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||
_console.Output.Write(option));
|
||||
|
||||
_console.Output.Write(']');
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
_console.Output.WriteLine();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -91,9 +107,7 @@ namespace CliFx
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int? HandleHelpOption(
|
||||
ApplicationSchema applicationSchema,
|
||||
CommandLineInput commandLineInput)
|
||||
private int? HandleHelpOption(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
|
||||
var shouldRenderHelp =
|
||||
@@ -139,7 +153,7 @@ namespace CliFx
|
||||
|
||||
return
|
||||
await HandleDebugDirectiveAsync(commandLineInput) ??
|
||||
HandlePreviewDirective(commandLineInput) ??
|
||||
HandlePreviewDirective(applicationSchema, commandLineInput) ??
|
||||
HandleVersionOption(commandLineInput) ??
|
||||
HandleHelpOption(applicationSchema, commandLineInput) ??
|
||||
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 => !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
|
||||
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 IReadOnlyDictionary<Type, Func<string, object>> PrimitiveConverters =
|
||||
new Dictionary<Type, Func<string, object>>
|
||||
new Dictionary<Type, Func<string?, object>>
|
||||
{
|
||||
[typeof(object)] = v => v,
|
||||
[typeof(string)] = v => v,
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace CliFx.Domain
|
||||
IsRequired = isRequired;
|
||||
}
|
||||
|
||||
public bool MatchesName(string name) =>
|
||||
public bool MatchesName(string? name) =>
|
||||
!string.IsNullOrWhiteSpace(Name) &&
|
||||
string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace CliFx.Domain
|
||||
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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user