96 lines
3.0 KiB
C#
96 lines
3.0 KiB
C#
using ArgumentsToolkit;
|
||
using ArgumentsToolkit.Help;
|
||
|
||
namespace Demo;
|
||
|
||
internal class Program
|
||
{
|
||
static int Main(string[] args)
|
||
{ // Пример запуска:
|
||
// dotnet run --server myhost --port 8080 --env staging --mode Incremental --config "{\"Author\":\"FrigaT\",\"Timeout\":60}"
|
||
|
||
|
||
var result = ArgumentsParser.Parse<DeployOptions>(args);
|
||
|
||
if (!result.Success)
|
||
{
|
||
Console.ForegroundColor = ConsoleColor.Red;
|
||
foreach (var e in result.Errors)
|
||
Console.WriteLine($"{e.Code}: {e.Message}");
|
||
Console.ResetColor();
|
||
|
||
var help = HelpCollector.Collect<DeployOptions>();
|
||
Console.WriteLine(help.AsMarkdown());
|
||
return 1;
|
||
}
|
||
|
||
if (!Validator.Validate(result.Value!, out var vErrors))
|
||
{
|
||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||
foreach (var e in vErrors)
|
||
Console.WriteLine($"Validation: {e}");
|
||
Console.ResetColor();
|
||
return 2;
|
||
}
|
||
|
||
Console.ForegroundColor = ConsoleColor.Green;
|
||
Console.WriteLine("Аргументы успешно разобраны и прошли валидацию:");
|
||
Console.WriteLine($"Server: {result.Value!.Server}");
|
||
Console.WriteLine($"Port: {result.Value.Port}");
|
||
Console.WriteLine($"Environment: {result.Value.Environment}");
|
||
Console.WriteLine($"DryRun: {result.Value.DryRun}");
|
||
Console.WriteLine($"Mode: {result.Value.Mode}");
|
||
Console.WriteLine($"Path: {result.Value.Path}");
|
||
Console.WriteLine($"Config.Author: {result.Value.Config.Author}");
|
||
Console.WriteLine($"Config.Timeout: {result.Value.Config.Timeout}");
|
||
Console.ResetColor();
|
||
|
||
Console.WriteLine("Строка аргументов:");
|
||
Console.WriteLine(ArgumentsParser.ToArguments(result.Value!, true));
|
||
|
||
return 0;
|
||
|
||
}
|
||
}
|
||
|
||
|
||
public class DeployOptions
|
||
{
|
||
[Option("server", "s", "Адрес сервера", required: true)]
|
||
public string Server { get; set; } = default!;
|
||
|
||
[Option("port", "p", "Порт подключения", defaultValue: 22)]
|
||
[Range(1, 65535)]
|
||
public int Port { get; set; }
|
||
|
||
[Option("env", "e", "Среда деплоя")]
|
||
[AllowedValues("dev", "staging", "prod")]
|
||
public string Environment { get; set; } = "dev";
|
||
|
||
[Option("path", null, "Среда деплоя")]
|
||
public string? Path { get; set; }
|
||
|
||
[Option("dry-run", "d", "Пробный запуск без изменений")]
|
||
public bool DryRun { get; set; }
|
||
|
||
[Option("config", "c", "JSON‑конфиг для деплоя")]
|
||
[OptionConverter(typeof(JsonOptionConverter))]
|
||
public MyConfig Config { get; set; } = new();
|
||
|
||
[Option("mode", "m", "Режим деплоя")]
|
||
public DeployMode Mode { get; set; } = DeployMode.Full;
|
||
}
|
||
|
||
public class MyConfig
|
||
{
|
||
public string Author { get; set; } = "unknown";
|
||
public int Timeout { get; set; } = 30;
|
||
}
|
||
|
||
public enum DeployMode
|
||
{
|
||
Full,
|
||
Incremental,
|
||
DryRun
|
||
}
|