Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to enforce required command-line options with NDesk.Options?
    primarykey
    data
    text
    <p>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?</p> <p>I see in the <a href="http://www.ndesk.org/Options" rel="nofollow noreferrer">docs</a> that:</p> <blockquote> <p>options with a required value (append '=' to the option name) or an optional value (append ':' to the option name).</p> </blockquote> <p>However, when I put a <code>=</code> at the end of the option name there is no difference in behavior. Ideally the Parse method would throw an exception.</p> <p>Is there something else I need to do?</p> <p>Here is my test code:</p> <pre><code>class Program { static void Main(string[] args) { bool show_help = false; string someoption = null; var p = new OptionSet() { { "someoption=", "Some String Option", v =&gt; someoption = v}, { "h|help", "show this message and exit", v =&gt; show_help = v != null } }; List&lt;string&gt; 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload