Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6770807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:21:25+00:00 2026-05-26T15:21:25+00:00

Below is a typical OptionSet construction code: var p = new OptionSet { {

  • 0

Below is a typical OptionSet construction code:

var p = new OptionSet {
  { "h|help", "Show this help", v => { isHelp = (v != null); } },
};

var extra = p.Parse(args);

My Powershell version of the same code is:

$p = New-Object NDesk.Options.OptionSet
$p.Add("h|help", "Show this help", { param([string]$v) $global:isHelp = $true })
$extra = $p.Parse($args)

Unfortunately, it has two problems. When I execute the second line I get this:

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

then executing the next line results in:

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

The relevant portion of OptionSet declaration is:

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);

I do not understand what is going on in the first error at all.

The second is clear – apparently $args is typed as object[], while OptionSet.Parse expects IEnumerable<string>, but I cannot find how to cast to string[].

So, my question is how do I translate the original C# code to Powershell without these nasty exceptions?

Thanks.

EDIT

Thanks to all the folks who have made me understand that PowerShell has a well defined methodology for working with command line arguments. I have acknowledged this fact and have even created a dedicated SO question – Is there a decent command line parser for powershell?, which I have already marked as answered. Again, thanks to all.

Now, I am still interested to know how do I invoke particular .NET code from PowerShell, if at all possible. No connection to command line argument parsing. Just pure quest for knowledge.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:21:25+00:00Added an answer on May 26, 2026 at 3:21 pm

    PowerShell has its own native argument/parameter parser/binder for functions and cmdlets. You can’t override this with NDesk.Options, so you’re pissing in the wind here I’m afraid my good man. Before you say “jeez, that sucks” you should think a bit about the larger goals of powershell. By coming with a baked-in parameter binder, function/command invocation styles are invariant: no wondering if it’s slash or hyphen, doubleslash, equals, colon etc. This is the same reason why cmdlets and functions use familiar verb-noun notation in naming. Instead of guessing how to invoke things and getting it wrong, you can actually get down to the more useful activity of discovering what you can do.

    What are you trying to achieve from a broader perspective? Post the desired parameter binding semantics and I will show you how to declare this in a powershell function.

    Update: if you want to learn more about powershell, have a read through my good friend Keith Hill’s free ebook: Effective Windows PowerShell, available free in PDF: http://rkeithhill.wordpress.com/2009/03/08/effective-windows-powershell-the-free-ebook/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK, so this code below sometimes crashes on the line below //bug!! The error
Background Below is a typical piece of Perl code ( sample.pl for the sake
Below is the code of a simple html with a table layout. In FF
The typical header should be #!/usr/bin/env python But I found below also works when
The below table code in razor in a MVC View, generates a standard table
I have this typical scenario. I have a smartclient application built on .net 2.0
I'm new to android and have been stuck on this problem for a long
Below is the source code of a client and a server. The client just
Ok below is the full code I have written for what is suppose to
I though the below code should skip the SetupType dialog. <Publish Dialog=LicenseAgreementDlg Control=Next Event=NewDialog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.