commandlineparser / commandline

The best C# command line parser that brings standardized *nix getopt style, for .NET. Includes F# support
MIT License
4.56k stars 477 forks source link

Allow no value #674

Open biosmanager opened 4 years ago

biosmanager commented 4 years ago

I'm trying to implement a string option that can have three states:

  1. Not supplied
  2. No value
  3. Empty or value

I want to do the following: MyApp.exe writes unformatted output to standard output MyApp.exe --json writes JSON formatted output to standard output MyApp.exe --json file.json writes JSON formatted output to file.json

Is this currently possible?

rmunn commented 4 years ago

Try this:

[Option("json")]
bool JsonFormat { get; set; }

[Value(0)]
string OutputFilename { get; set; }

Now --json is a Boolean option, and whether or not it's set, OutputFilename contains the output filename (and if it's null or empty, use stdout).

BTW, why not use - as the filename that means "standard output", the way Unix commands have done for ages? Then your options class would look like this:

[Option("json")]
bool JsonFormat { get; set; }

[Value(0, DefaultValue="-")]
string OutputFilename { get; set; }

And your code would check for the filename "-" rather than an empty string as the signal to use stdout.

PaulDotNet commented 3 years ago

I have the same question. Value is OK but then it can occur anywhere in command line like this:

MyApp -json file.json
MyApp file.json -json 
MyApp -json -otheroption -anotheroption file.json

It is not even clear what this value is for. The help text for value that is not an option is quite user-unfriendly in my opinion. Having option with an optional value would be much cleaner solution.