What am I missing about DefaultValue?

Apr 1, 2015 at 9:01 PM
Edited Apr 1, 2015 at 9:16 PM
[Option("i", "input", Required = true, HelpText = "Input file to read.", DefaultValue="test.txt")]
public string InputFile { get; set; }

What am I missing about the DefaultValue option, if I simply kick off the exe with -i ParseArguments returns false. I basically have to specify a file name such as -i file.txt for it to return true. I assumed that's the point of DefaultValue, is that if I don't specify the file name it'll still ParseArguments as true and the value of InputFile will be test.txt

ps. I simply used the example from the Documentation page. In my application I want to be able to specify a flag with an optional parameter. So basically -createFile "file.txt" where the file name is optional. Perhaps I'm approaching this in the wrong way.
Jul 2, 2015 at 6:41 PM
Required = true, property is requiring -i to be present, so you hit invalid input condition when user doesn't provide it. Remove it.

Not sure about order (my DefaultValue is always before HelpText), but I basically swap out DefaultValue or Required property as needed, but never use them together. Having both and setting Required to false may work, just haven't tried it.

This should work for you...
[Option("i", "input", DefaultValue="test.txt", HelpText = "Input file to read.")]
public string InputFile { get; set; }
To better explain what I mean by swapping them out, here are two string properties, one required, one not.
[Option('o', "outputfile", Required = true, HelpText = "blah")]
public string OutputFile { get; set; }

[Option('r', "recostate", DefaultValue = "Global", HelpText = "blah")]
public string RecoState { get; set; }