描述
我有一个控制台应用程序,它有很多参数。为了管理它们,我使用了 nuget 包commandlineparser。它工作得非常好,但是一个参数没有像 -r 或 --reload 这样的选项。有没有人知道如何捕捉一个未命名的参数?
示例参数:-p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro --restart always --log-opt max-size=1g nginx
程序.cs
using DockerComposeConverterTool.Commands;
using DustInTheWind.ConsoleTools;
using DustInTheWind.ConsoleTools.Menues;
using DustInTheWind.ConsoleTools.InputControls;
using System;
using System.Collections.Generic;
using CommandLine;
namespace DockerComposeConverterTool
{
class Program
{
public static StringListView Services = new StringListView();
static void Main(string[] args)
{
if (args.Length == 0)
{
PrintMenu();
}
else
{
var parser = new Parser(options =>
{
options.IgnoreUnknownArguments = false;
options.CaseSensitive = false;
});
var result = parser.ParseArguments<ArgumentOptions>(args)
.WithNotParsed<ArgumentOptions>(o =>
{
Console.WriteLine(o);
})
.WithParsed<ArgumentOptions>(o =>
{
if (o.Verbose)
{
Console.WriteLine($"Verbose output enabled. Current Arguments: -v {o.Verbose}");
Console.WriteLine("Quick Start Example! App is in Verbose mode!");
}
else
{
Console.WriteLine($"Current Arguments: -v {o.Verbose}");
Console.WriteLine("Quick Start Example!");
}
});
var parsedValue = ((Parsed<ArgumentOptions>)result).Value;
Console.WriteLine();
//foreach(var arg in args)
//{
// Console.WriteLine(arg);
//}
}
Environment.Exit(0);
//var test = new Converter(new List<DockerComposeServiceModel>(new DockerComposeServiceModel
//{
// Service = "Testvalue",
// Parameters = new DockerComposeConverterCore.Parameters.DockerParameter()
//}));
//Console.WriteLine(test.GetYamlComposeModel());
//CustomConsole.WriteLine("Normal: This is a normal line of text.");
//CustomConsole.WriteLine();
//CustomConsole.WriteLineEmphasies("Emphasies: But I can also write an emphasized text.");
//CustomConsole.WriteLine();
//CustomConsole.WriteLineSuccess("Success: And everything is ok if it finishes well :)");
//CustomConsole.WriteLine();
//CustomConsole.WriteLineWarning("Warning: But I have to warn you about the consequences of something not being done correctly.");
//CustomConsole.WriteLine();
//CustomConsole.WriteLineError("Error: If some error occures and the application will crush with an exception, I will display it on the screen immediately.");
}
static void PrintMenu()
{
Console.Clear();
HorizontalLine.QuickDisplay();
CustomConsole.WriteLineEmphasies(@"[$$$$$$$$\ $$\ $$\ $$$$$$\ $$\]");
CustomConsole.WriteLineEmphasies(@"[$$ _____|\__| $$ | $$ __$$\ $$ |]");
CustomConsole.WriteLineEmphasies(@"[$$ | $$\ $$$$$$\ $$$$$$$\ $$$$$$\ $$ / \__| $$$$$$\ $$$$$$$ | $$$$$$\ $$$$$$\]");
CustomConsole.WriteLineEmphasies(@"[$$$$$\ $$ |$$ __$$\ $$ _____|\_$$ _|$$$$$$\ $$ | $$ __$$\ $$ __$$ |$$ __$$\ $$ __$$\]");
CustomConsole.WriteLineEmphasies(@"[$$ __| $$ |$$ | \__|\$$$$$$\ $$ | \______|$$ | $$ / $$ |$$ / $$ |$$$$$$$$ |$$ | \__|]");
CustomConsole.WriteLineEmphasies(@"[$$ | $$ |$$ | \____$$\ $$ |$$\ $$ | $$\ $$ | $$ |$$ | $$ |$$ ____|$$ |]");
CustomConsole.WriteLineEmphasies(@"[$$ | $$ |$$ | $$$$$$$ | \$$$$ | \$$$$$$ |\$$$$$$ |\$$$$$$$ |\$$$$$$$\ $$ |]");
CustomConsole.WriteLineEmphasies(@"[\__| \__|\__| \_______/ \____/ \______/ \______/ \_______| \_______|\__|]");
HorizontalLine.QuickDisplay();
if (Services.Values != null && !Services.Values.Count.Equals(0))
{
Services.Write();
HorizontalLine.QuickDisplay();
}
TextMenu textMenu = new TextMenu
{
TitleText = "Docker Compose Converter Tool",
TitleForegroundColor = ConsoleColor.Cyan,
EraseAfterClose = false
};
textMenu.AddItems(new List<TextMenuItem>
{
new TextMenuItem
{
Id = "1",
Text = "Add Service"
},
new TextMenuItem
{
Id = "2",
Text = "Remove Service"
},
new TextMenuItem
{
Id = "3",
Text = "Clear Services"
},
new TextMenuItem
{
Id = "4",
Text = "Print Service to console"
},
new TextMenuItem
{
Id = "4",
Text = "Print Service docker-compose.yml"
},
new TextMenuItem
{
Id = "0",
Text = "Exit",
Command = new ExitCommand()
}
});
textMenu.Display();
PrintMenu();
}
}
}