下面是一个典型OptionSet
建设代码:翻译NDesk.Options.OptionSet建设的PowerShell
var p = new OptionSet {
{ "h|help", "Show this help", v => { isHelp = (v != null); } },
};
var extra = p.Parse(args);
相同的代码我Powershell的版本是:
$p = New-Object NDesk.Options.OptionSet
$p.Add("h|help", "Show this help", { param([string]$v) $global:isHelp = $true })
$extra = $p.Parse($args)
不幸的是,它有两个问题。当我执行的第二行,我得到这样的:
Multiple ambiguous overloads found for "Add" and the argument count: "3".
At C:\Work\hg\utils\HgTagPromotedBuild.ps1:62 char:7
+ $p.Add <<<< ("h|help", "Show this help message", { param([string]$v) $global:isHelp =
$true })
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
然后执行下一行的结果:
Cannot convert argument "0", with value: "System.Object[]", for "Parse" to type "System
.Collections.Generic.IEnumerable`1[System.String]": "Cannot convert the "System.Object[
]" value of type "System.Object[]" to type "System.Collections.Generic.IEnumerable`1[Sy
stem.String]"."
At C:\Work\hg\utils\HgTagPromotedBuild.ps1:63 char:18
+ $extra = $p.Parse <<<< ($args)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
OptionSet
申报的有关部分:
public OptionSet Add(Option option);
public OptionSet Add(string prototype, OptionAction<string, string> action);
public OptionSet Add<TKey, TValue>(string prototype, OptionAction<TKey, TValue> action);
public OptionSet Add<T>(string prototype, Action<T> action);
public OptionSet Add(string prototype, Action<string> action);
public OptionSet Add<TKey, TValue>(string prototype, string description, OptionAction<TKey, TValue> action);
public OptionSet Add(string prototype, string description, OptionAction<string, string> action);
public OptionSet Add<T>(string prototype, string description, Action<T> action);
public OptionSet Add(string prototype, string description, Action<string> action);
我不了解第一个错误发生了什么。
二是明确的 - 显然$args
的类型为object[]
,而OptionSet.Parse
预计IEnumerable<string>
,但我怎么也找不到投射到string[]
。
所以,我的问题是如何将原始C#代码翻译成Powershell而没有这些恶意的异常?
谢谢。
编辑
感谢谁使我明白,PowerShell提供了与命令行参数的工作一个定义良好的方法的所有乡亲。我已经承认这一事实,甚至创建了一个专门的SO问题 - Is there a decent command line parser for powershell?,我已经标记为已回答。再次感谢所有人。
现在,我仍然很想知道如何从PowerShell中调用特定的.NET代码(如果可能的话)。没有连接到命令行参数分析。只是纯粹的追求知识。
你能告诉你想达到什么?或者,甚至可以问一个关于如何在PowerShell中实现这个问题的单独问题?你问如何使用PowerShell *中不应该使用的工具。 –
我编辑了我的文章。 – mark
尝试将第三个参数转换为明确需要的类型(动作?)。另外,在PowerShell(v2,希望v3会有所不同)不太容易使用泛型方法。看看这篇文章(希望,这有助于):http://www.leeholmes.com/blog/2007/06/19/invoking-generic-methods-on-non-generic-classes-in-powershell/ –