如何通过 gsscoder 访问 C# CommandLine Parser 中的未绑定参数?
How to access unbound parameters in C# CommandLine Parser by gsscoder?
有一个 CommandLine 由 gsscoder 编写的 C# 解析器库(它有自己的 SO 标记,我正在添加它)。它以 getopt
风格解析命令行选项,即:
myprogram --foo --bar=baz abc def ghi
它也可以有所谓的“未绑定”参数,即不绑定选项的独立位置参数;在上面的示例中,它们是 abc
、def
和 ghi
。不幸的是,the documentation 只提到“解析器有处理这些的方法”,但没有给出示例。而且我的 C# 不是那么敏锐,所以我被要扫描以找出它的源代码量吓坏了。
有人可以举例说明如何在解析后访问这些未绑定的参数吗?
使用 ValueList[Attribute]
(参见 docs on CodePlex):
Each value not captured by an option can be included in a collection of strings derived from System.Collections.Generic.IList.
Obviously, this attribute has no name(s) and is derived directly from System.Attribute.
It's currently the only exception, but it's not excluded that in the future it will have similars.
示例(来自上面链接的页面):
class Options
{
// ...
[ValueList(typeof(List<string>), MaximumElements = 3)]
public IList<string> Items { get; set; };
// ...
}
其中 ValueList
- Must be assigned to a property of type
IList<string>
.
- The constructor must accept a type derived from
IList<string>
as List<string>
.
- If the
MaximumElements
property is set to a number greater than 0, the parser will fail if the limit is exceeded.
- Set
MaximumElements
to 0 means that you do not accept values disassociated from options.
- The default implicit setting of
MaximumElements
(-1) allows an unlimited number of values.
有一个 CommandLine 由 gsscoder 编写的 C# 解析器库(它有自己的 SO 标记,我正在添加它)。它以 getopt
风格解析命令行选项,即:
myprogram --foo --bar=baz abc def ghi
它也可以有所谓的“未绑定”参数,即不绑定选项的独立位置参数;在上面的示例中,它们是 abc
、def
和 ghi
。不幸的是,the documentation 只提到“解析器有处理这些的方法”,但没有给出示例。而且我的 C# 不是那么敏锐,所以我被要扫描以找出它的源代码量吓坏了。
有人可以举例说明如何在解析后访问这些未绑定的参数吗?
使用 ValueList[Attribute]
(参见 docs on CodePlex):
Each value not captured by an option can be included in a collection of strings derived from System.Collections.Generic.IList.
Obviously, this attribute has no name(s) and is derived directly from System.Attribute. It's currently the only exception, but it's not excluded that in the future it will have similars.
示例(来自上面链接的页面):
class Options
{
// ...
[ValueList(typeof(List<string>), MaximumElements = 3)]
public IList<string> Items { get; set; };
// ...
}
其中 ValueList
- Must be assigned to a property of type
IList<string>
.- The constructor must accept a type derived from
IList<string>
asList<string>
.- If the
MaximumElements
property is set to a number greater than 0, the parser will fail if the limit is exceeded.- Set
MaximumElements
to 0 means that you do not accept values disassociated from options.- The default implicit setting of
MaximumElements
(-1) allows an unlimited number of values.