Добавьте файлы проекта.
This commit is contained in:
14
Demo/Demo.csproj
Normal file
14
Demo/Demo.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ArgumentsToolkit\ArgumentsToolkit.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
88
Demo/Program.cs
Normal file
88
Demo/Program.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
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($"Config.Author: {result.Value.Config.Author}");
|
||||
Console.WriteLine($"Config.Timeout: {result.Value.Config.Timeout}");
|
||||
Console.ResetColor();
|
||||
|
||||
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("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
|
||||
}
|
||||
12
Demo/Properties/launchSettings.json
Normal file
12
Demo/Properties/launchSettings.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Demo": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "--server myhost --port 8080 --env staging --mode Incremental --config \"{\\\"Author\\\":\\\"FrigaT\\\",\\\"Timeout\\\":60}"
|
||||
},
|
||||
"Demo (Error)": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "--server myhost --port \"128080\" --env staging --mode Incremental --config \"{\\\"Author\\\":\\\"FrigaT\\\",\\\"Timeout\\\":60}"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user