如何加强与NDesk.Options所需的命令行选项? [英] How to enforce required command-line options with NDesk.Options?
问题描述
我只是写一个控制台程序,并决定使用命令行解析NDesk.Options。我的问题是,我该如何执行所需的命令行选项?
我在文档是:
用所需的值选项(追加'='的选项名称)或可选的值(追加':'给选项名称)。
块引用>然而,当我把
=
在选项名称的末尾有行为没有区别。理想情况下,Parse方法会抛出异常。有没有别的东西,我需要做什么?
下面是我的测试code:
类节目
{
静态无效的主要(字串[] args)
{
布尔show_help = FALSE;
字符串someoption = NULL; VAR P =新OptionSet(){
{someoption =,一些字符串选项,V = GT; someoption = V},
{H |帮助,显示此消息并退出,V = GT; show_help = V!= NULL}
}; 清单<串GT;额外;
尝试
{
额外= p.Parse(参数);
}
赶上(OptionException E)
{
System.Console.Write(myconsole:);
的System.Console.WriteLine(e.Message);
的System.Console.WriteLine(尝试'myconsole --help以获取更多信息。);
返回;
} 如果(show_help)
{
ShowHelp(P);
返回;
} 的System.Console.WriteLine(==================);
的System.Console.WriteLine(someoption);
} 静态无效ShowHelp(OptionSet P)
{
的System.Console.WriteLine(用法:myconsole [OPTIONS]);
的System.Console.WriteLine();
的System.Console.WriteLine(选项:);
p.WriteOptionDescriptions(System.Console.Out);
}
}
解决方案的问题是,文件不明确的,因为它显然需要是。 : - (
具体地讲,每
<一个href=\"http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionValueType.html#F:NDesk.Options.OptionValueType.Required\">http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionValueType.html#F:NDesk.Options.OptionValueType.Required
的
=
中的一个选项规范并不适用于OptionSet作为一个整体,只是给的值的该特定选项。这一点的重要性实际上只有在两种情况下相关的,所以首先让我们考虑OptionSet解析器:
字符串= NULL;
字符串B = NULL;
VAR的选择=新OptionSet {
{一个=,V =&GT; A = V},
{B =,V =&GT; B = V},
};方案1在那里重要的是,OptionSet.Parse()工作在单通,只进的方式,并执行的不的看选项值,以确定他们是否应该是值。因此,考虑:
options.Parse(新[] { - 一个,-b});
这样做的结果将是
A
值为- B
和b
是空
。由于处理程序-a
的需要的值,它的总是的获取以下值(除非该值是EN codeD到原来的选项,如:-a =值
)。第二个地方,这是很重要的是当值需要选择是最后的选择,并没有为它的价值present:
options.Parse(新[] { - 一个});
这将引发OptionException,因为处理程序
-a
的需要的一个值,没有值是present。因此,如果你有自己需要(而不是需要值的选项)选项,则需要手动检查这一点:
字符串DIR = NULL;
新OptionSet {
{O =,V = GT; DIR = V},
} .Parse(参数);如果(DIR == NULL)
抛出新的InvalidOperationException异常(缺少必需的选项-o = DIR);I was just writing a console utility and decided to use NDesk.Options for command-line parsing. My question is, How do I enforce required command-line options?
I see in the docs that:
options with a required value (append '=' to the option name) or an optional value (append ':' to the option name).
However, when I put a
=
at the end of the option name there is no difference in behavior. Ideally the Parse method would throw an exception.Is there something else I need to do?
Here is my test code:
class Program { static void Main(string[] args) { bool show_help = false; string someoption = null; var p = new OptionSet() { { "someoption=", "Some String Option", v => someoption = v}, { "h|help", "show this message and exit", v => show_help = v != null } }; List<string> extra; try { extra = p.Parse(args); } catch (OptionException e) { System.Console.Write("myconsole: "); System.Console.WriteLine(e.Message); System.Console.WriteLine("Try `myconsole --help' for more information."); return; } if (show_help) { ShowHelp(p); return; } System.Console.WriteLine("=================="); System.Console.WriteLine(someoption); } static void ShowHelp(OptionSet p) { System.Console.WriteLine("Usage: myconsole [OPTIONS]"); System.Console.WriteLine(); System.Console.WriteLine("Options:"); p.WriteOptionDescriptions(System.Console.Out); } }
解决方案The problem is that documentation isn't as clear as it apparently needs to be. :-(
Specifically, as per:
The
=
within an option specification doesn't apply to the OptionSet as a whole, but just to the value for that specific option.The importance of this is really only relevant in two scenarios, so first let's consider the OptionSet parser:
string a = null; string b = null; var options = new OptionSet { { "a=", v => a = v }, { "b=", v => b = v }, };
Scenario 1 where it's important is that OptionSet.Parse() works in a single-pass, forward-only manner, and does not look at option values to determine if they "should be" values. Thus, consider:
options.Parse(new[]{"-a", "-b"});
The result of this will be that
a
has the value"-b"
, andb
isnull
. Since the handler for-a
requires a value, it always gets the following value (unless the value is "encoded" into the original option, e.g.-a=value
).The second place where this is important is when a value-requiring option is the last option, and there isn't a value present for it:
options.Parse(new[]{"-a"});
This will throw an OptionException, as the handler for
-a
requires a value, and no value is present.Consequently, if you have an option that itself is required (as opposed to an option that requires a value), you need to manually check for this:
string dir = null; new OptionSet { { "o=", v => dir = v }, }.Parse (args); if (dir == null) throw new InvalidOperationException ("Missing required option -o=DIR");
这篇关于如何加强与NDesk.Options所需的命令行选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!