How to implement an option whose value is optional?
Is there any way to implement an option (let's say with name "option1") that it works like:
- --option1 value1, means to set it to "value1"
- --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
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:
- 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. - Two separate options (
--option1and--option1default)