我正在重新设计一个命令行应用程序,希望找到一种使其使用更加直观的方法。有没有关于命令行应用程序参数格式的惯例?或者其他人发现有用的任何方法?
我正在重新设计一个命令行应用程序,希望找到一种使其使用更加直观的方法。有没有关于命令行应用程序参数格式的惯例?或者其他人发现有用的任何方法?
myCli.exe describe someThing
myCli.exe descr someThing
myCli.exe desc someThing
一个好的和有帮助的参考资料:
https://commandline.codeplex.com/
通过NuGet可用的库:
Install-Package CommandLineParser
。 Install-Package CommandLineParser -pre
。使用默认单例进行一行解析:CommandLine.Parser.Default.ParseArguments(...)
。
一行帮助屏幕生成器:HelpText.AutoBuild(...)
。
将命令行参数映射到IList<string>
、数组、枚举或标准标量类型。
插件友好的体系结构,如此处所述。
定义动词命令:git commit -a
。
使用Lambda表达式创建解析器实例。
快速入门:https://commandline.codeplex.com/wikipage?title=Quickstart&referringTitle=Documentation
// Define a class to receive parsed values
class Options {
[Option('r', "read", Required = true,
HelpText = "Input file to be processed.")]
public string InputFile { get; set; }
[Option('v', "verbose", DefaultValue = true,
HelpText = "Prints all messages to standard output.")]
public bool Verbose { get; set; }
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption]
public string GetUsage() {
return HelpText.AutoBuild(this,
(HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
// Consume them
static void Main(string[] args) {
var options = new Options();
if (CommandLine.Parser.Default.ParseArguments(args, options)) {
// Values are available here
if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
}
}
最好的做法是,如果可以的话,不要假定任何东西。当操作员键入您的应用程序名称以进行执行并且没有任何参数时,请向他们显示使用说明块或者在另一种情况下打开一个Windows窗体并允许他们输入您所需的所有内容。
c:\>FOO
FOO
USAGE FOO -{Option}{Value}
-A Do A stuff
-B Do B stuff
c:\>
我将把参数分隔放在宗教话题的标题下:连字符(破折号),双破折号,斜杠,空白,位置等。
您没有说明您的平台,但是对于下一个评论,我将假设Windows和.net
您可以在.net中创建基于控制台的应用程序,并通过选择控制台项目然后添加Windows.Forms、System.Drawing等DLL来允许其与桌面交互。
我们经常这样做。这确保了没有人走进黑暗的小巷。
这里有一篇 CodeProject 的文章可能会对你有所帮助...
如果你喜欢 VB,这里有一篇单独的文章(包含更多相关内容的指导)可以查看...
补充 @vonc 的回答,不要接受含糊不清的缩写。例如:
myCli.exe describe someThing
myCli.exe destroy someThing
myCli.exe des someThing ???
事实上,在那种情况下,我可能不会接受“destroy”的缩写...
我开发了这个框架,也许可以帮到你:
SysCommand是一个强大的跨平台框架,用于在.NET中开发控制台应用程序。它很简单,类型安全,并且受MVC模式的很大影响。
https://github.com/juniorgasparotto/SysCommand
namespace Example.Initialization.Simple
{
using SysCommand.ConsoleApp;
public class Program
{
public static int Main(string[] args)
{
return App.RunApplication();
}
}
// Classes inheriting from `Command` will be automatically found by the system
// and its public properties and methods will be available for use.
public class MyCommand : Command
{
public void Main(string arg1, int? arg2 = null)
{
if (arg1 != null)
this.App.Console.Write(string.Format("Main arg1='{0}'", arg1));
if (arg2 != null)
this.App.Console.Write(string.Format("Main arg2='{0}'", arg2));
}
public void MyAction(bool a)
{
this.App.Console.Write(string.Format("MyAction a='{0}'", a));
}
}
}
// auto-generate help
$ my-app.exe help
// method "Main" typed
$ my-app.exe --arg1 value --arg2 1000
// or without "--arg2"
$ my-app.exe --arg1 value
// actions support
$ my-app.exe my-action -a