Files
spectre.console/src/Spectre.Console.Cli/Internal/Modelling/CommandModel.cs
Frank Ray 714cf179cb Command line improvements (#1103)
Closes #187
Closes #203
Closes #1059
2023-04-02 22:43:21 +02:00

45 lines
1.5 KiB
C#

namespace Spectre.Console.Cli;
internal sealed class CommandModel : ICommandContainer
{
public string? ApplicationName { get; }
public ParsingMode ParsingMode { get; }
public IList<CommandInfo> Commands { get; }
public IList<string[]> Examples { get; }
public bool TrimTrailingPeriod { get; }
public CommandInfo? DefaultCommand => Commands.FirstOrDefault(c => c.IsDefaultCommand);
public CommandModel(
CommandAppSettings settings,
IEnumerable<CommandInfo> commands,
IEnumerable<string[]> examples)
{
ApplicationName = settings.ApplicationName;
ParsingMode = settings.ParsingMode;
TrimTrailingPeriod = settings.TrimTrailingPeriod;
Commands = new List<CommandInfo>(commands ?? Array.Empty<CommandInfo>());
Examples = new List<string[]>(examples ?? Array.Empty<string[]>());
}
public string GetApplicationName()
{
return
ApplicationName ??
Path.GetFileName(GetApplicationFile()) ?? // null is propagated by GetFileName
"?";
}
private static string? GetApplicationFile()
{
var location = Assembly.GetEntryAssembly()?.Location;
if (string.IsNullOrWhiteSpace(location))
{
// this is special case for single file executable
// (Assembly.Location returns empty string)
return Process.GetCurrentProcess().MainModule?.FileName;
}
return location;
}
}