commandlineparser / commandlineparser/commandline

Issues with sub options

未关闭
#48 6 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
postponed-after-issue-250-close_temp-label
主要语言
C#
星标
4.8k
派生
478
PR 合并指标
30 天内没有已合并 PR

描述

**Issue by [wadewegner](https://github.com/wadewegner)**
_Thursday Oct 01, 2015 at 18:07 GMT_
_Originally opened as https://github.com/gsscoder/commandline/issues/249_

----

I'm having an issue with the parser not figuring out my sub-options.

Here's the code in `Main`:

```
var invokedVerb = "";
var invokedVerbInstance = new object();

var options = new Options();
if (!CommandLine.Parser.Default.ParseArguments(args, options,
(verb, subOptions) =>
{
// if parsing succeeds the verb name and correct instance
// will be passed to onVerbCommand delegate (string,object)
invokedVerb = verb;
if (!string.IsNullOrEmpty(invokedVerb))
invokedVerbInstance = subOptions;
}))
{
Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
}
```

This is pretty much right from the wiki.

My options are defined as this:

```
class Options
{
public Options()
{
CpVerb = new CPSubOptions { };
}

[VerbOption("cp", HelpText = "Interact with cp.")]
public CPSubOptions CpVerb { get; set; }

[HelpVerbOption]
public string GetUsage(string verb)
{
return HelpText.AutoBuild(this, verb);
}
}

class CPSubOptions
{
[Option('n', "name", HelpText = "Get details name.")]
public string Name { get; set; }
}
```

When I run with `cp` as the args, it works and the parser determines that `CPSubOptions` is the `subOptions` and `invokedVerbInstance` is set appropriately.

However, when I run with `cp --name blah` or `cp -n blah`, the `subOptions` is null, so it's not parsed properly.

Any help?

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。