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

  • SEARCH
  • Home
  • 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 1084209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:32:00+00:00 2026-05-16T22:32:00+00:00

I was just writing a console utility and decided to use NDesk.Options for command-line

  • 0

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);
    }
}
  • 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-16T22:32:00+00:00Added an answer on May 16, 2026 at 10:32 pm

    The problem is that documentation isn’t as clear as it apparently needs to be. 🙁

    Specifically, as per:

    http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionValueType.html#F:NDesk.Options.OptionValueType.Required

    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", and b is null. 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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a console application which is rapidly gaining many command line arguments
I am writing a simple java console game. I use the scanner to read
Just that. I found a similar question here : c# console, Console.Clear problem but
I have just finished writing my c# console application, and I am contemplating embedding
I am just writing a simple plugin for my own use in a web
Scenario - I am writing a console application. Need to use a certain DLL
Just have a quick question... any help would be greatly appreciate! I'm writing a
Im just writing a small Ajax framework for re-usability in small projects and i've
I was just writing some quick code and noticed this complier error Using the
I was just writing a procedure that is looking for a newline and I

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.