Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting optional values using NDesk.Options

Using the accepted answer in Best way to parse command line arguments in C#? as your example, how can I make the 'r' or 'repeat' option only OPTIONALLY take a value and not REQUIRE it, and how can I set a default value if the value is not provided? Thanks!

like image 643
Wes Avatar asked Mar 02 '26 21:03

Wes


1 Answers

Apparently you can do something like:

{ "r|repeat:", 
       "the number of {TIMES} to repeat the greeting.\n" + 
          "this must be an integer.",
        (int v) => repeat = v ?? 1 },

Where the default value is 1 if the value is not provided.

like image 87
Wes Avatar answered Mar 05 '26 12:03

Wes