зеркало из https://github.com/dotnet/tye.git
basic cli skeleton
This commit is contained in:
Родитель
3c26b6febb
Коммит
1aeeca1ca9
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
|
||||
namespace Tye
|
||||
{
|
||||
internal class CommandException : Exception
|
||||
{
|
||||
public CommandException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public CommandException(string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
using System.Linq;
|
||||
|
||||
namespace System.CommandLine
|
||||
{
|
||||
internal static class ConsoleExtensions
|
||||
{
|
||||
private static bool? isConsoleRedirectionCheckSupported;
|
||||
|
||||
private static bool IsConsoleRedirectionCheckSupported
|
||||
{
|
||||
get
|
||||
{
|
||||
if (isConsoleRedirectionCheckSupported == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var check = Console.IsOutputRedirected;
|
||||
isConsoleRedirectionCheckSupported = true;
|
||||
}
|
||||
|
||||
catch (PlatformNotSupportedException)
|
||||
{
|
||||
isConsoleRedirectionCheckSupported = false;
|
||||
}
|
||||
}
|
||||
|
||||
return isConsoleRedirectionCheckSupported.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetTerminalForegroundColor(this IConsole console, ConsoleColor color)
|
||||
{
|
||||
if (console.GetType().GetInterfaces().Any(i => i.Name == "ITerminal"))
|
||||
{
|
||||
((dynamic)console).ForegroundColor = color;
|
||||
}
|
||||
|
||||
if (IsConsoleRedirectionCheckSupported &&
|
||||
!Console.IsOutputRedirected)
|
||||
{
|
||||
Console.ForegroundColor = color;
|
||||
}
|
||||
else if (IsConsoleRedirectionCheckSupported)
|
||||
{
|
||||
Console.ForegroundColor = color;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ResetTerminalForegroundColor(this IConsole console)
|
||||
{
|
||||
if (console.GetType().GetInterfaces().Any(i => i.Name == "ITerminal"))
|
||||
{
|
||||
((dynamic)console).ForegroundColor = ConsoleColor.Red;
|
||||
}
|
||||
|
||||
if (IsConsoleRedirectionCheckSupported &&
|
||||
!Console.IsOutputRedirected)
|
||||
{
|
||||
Console.ResetColor();
|
||||
}
|
||||
else if (IsConsoleRedirectionCheckSupported)
|
||||
{
|
||||
Console.ResetColor();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,73 @@
|
|||
using System;
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.Builder;
|
||||
using System.CommandLine.Help;
|
||||
using System.CommandLine.Invocation;
|
||||
using System.CommandLine.IO;
|
||||
using System.CommandLine.Parsing;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace tye
|
||||
namespace Tye
|
||||
{
|
||||
class Program
|
||||
static partial class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
public static async Task<int> Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello World!");
|
||||
var command = new RootCommand()
|
||||
{
|
||||
Description = "Developer tools and publishing for microservices.",
|
||||
};
|
||||
|
||||
// Show commandline help unless a subcommand was used.
|
||||
command.Handler = CommandHandler.Create<IHelpBuilder>(help =>
|
||||
{
|
||||
help.Write(command);
|
||||
return 1;
|
||||
});
|
||||
|
||||
var builder = new CommandLineBuilder(command);
|
||||
builder.UseHelp();
|
||||
builder.UseVersionOption();
|
||||
builder.UseDebugDirective();
|
||||
builder.UseParseErrorReporting();
|
||||
builder.ParseResponseFileAs(ResponseFileHandling.ParseArgsAsSpaceSeparated);
|
||||
|
||||
builder.CancelOnProcessTermination();
|
||||
builder.UseExceptionHandler(HandleException);
|
||||
|
||||
var parser = builder.Build();
|
||||
return await parser.InvokeAsync(args);
|
||||
}
|
||||
|
||||
private static void HandleException(Exception exception, InvocationContext context)
|
||||
{
|
||||
context.Console.ResetTerminalForegroundColor();
|
||||
context.Console.SetTerminalForegroundColor(ConsoleColor.Red);
|
||||
|
||||
if (exception is OperationCanceledException)
|
||||
{
|
||||
context.Console.Error.WriteLine("Oh dear! Operation canceled.");
|
||||
}
|
||||
else if (exception is CommandException command)
|
||||
{
|
||||
context.Console.Error.WriteLine($"Drats! '{context.ParseResult.CommandResult.Command.Name}' failed:");
|
||||
context.Console.Error.WriteLine($"\t{command.Message}");
|
||||
|
||||
if (command.InnerException != null)
|
||||
{
|
||||
context.Console.Error.WriteLine();
|
||||
context.Console.Error.WriteLine(command.InnerException.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Console.Error.WriteLine("An unhandled exception has occurred, how unseemly: ");
|
||||
context.Console.Error.WriteLine(exception.ToString());
|
||||
}
|
||||
|
||||
context.Console.ResetTerminalForegroundColor();
|
||||
|
||||
context.ResultCode = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,14 @@
|
|||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<RootNamespace>Tye</RootNamespace>
|
||||
<AssemblyName>tye</AssemblyName>
|
||||
<ToolCommandName>tye</ToolCommandName>
|
||||
<PackAsTool>true</PackAsTool>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20104.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Загрузка…
Ссылка в новой задаче