commandline icon indicating copy to clipboard operation
commandline copied to clipboard

How to implement an option whose value is optional?

Open EmmaZhu opened this issue 8 years ago • 1 comments

Is there any way to implement an option (let's say with name "option1") that it works like:

  1. --option1 value1, means to set it to "value1"
  2. --option1, means it's set without a value

Our application is a long running one that we'd like to persist a running checkpoint on disk if user wants to. If user indicates a file path, we write the checkpoint to it; If user just indicate the option to persist the checkpoint but doesn't specify a path, we write the checkpoint to the default path.

Is there any way we can do this?

Thanks Emma

EmmaZhu avatar Mar 02 '17 03:03 EmmaZhu

Hi @EmmaZhu unfortunately this library doesn't support "value optional" parameters. By setting a Default value (like below) it will only be set when --option1 is missing. This is a good idea for a feature, though.

public class Options
{
    [Option("option1", Default="value1")]
    public string MyOption { get; set; }
}

Here are a few alternative approaches that come to mind:

  1. Some applications like wget use a - as a sigil indicating to take the input from somewhere else. You could use the same to indicate the default value.
  2. Two separate options (--option1 and --option1default)

nemec avatar Mar 03 '17 16:03 nemec