Use keyword to set positional argument
I don't think this issue has been submitted before, but #56 seems to want the same sort of thing.
I want this syntax to be the normal usage:
app inputfile outputfile
But I want both arguments to be optional and fallback on defaults:
app
Which means that it would be nice to specify one but not the other:
app inputfile
app -t outputfile
Therefore, I'd like to be able to set outputfile via either a positional or a keyword argument. Is this supported?
You can use Default and declare your option as:
class Options
{
[Value(0, Default="abc")] //positional argument
public string inputfile { get; set; }
[Option('t', Default="xyz")] //named argument
public string outputfile { get; set; }
}
Right, I understand I can do that -- but I want to know if something like this is possible:
class Options
{
// positional argument
[Value(0)]
public string inputfile { get; set; }
// positional OR named argument
[Value(1)]
[Option('t')]
public string outputfile { get; set; }
}
This way, if I'm supplying both arguments, I don't need the -t:
app inputfile outputfile
Applying both Option and Value attributes isn't applicable. Use only one Attribute. Options can be:
class Options
{
// positional argument
[Value(0)]
public string inputfile { get; set; }
// second positional argument
[Value(1)]
public string outputfile { get; set; }
}
You can probably do something like this:
class Options
{
[Value(0, Min = 1, Max = 2, MetaName = "Input and output files", Required = false, HelpText = "Specify both input and output files")]
public List<string> Files { get; set; }
[Option('i', "inputFile", Required = false, Default = "input.txt", HelpText = "Input file to use")]
public string InputFile { get; set; }
[Option('t', "outputFile", Required = false, Default = "output.txt", HelpText = "Output file to use")]
public string OutputFile { get; set; }
}
Examples:
app "input.txt" "output.txt"--Options.Filesproperty will have "input.txt" and "output.txt".app -i "input.txt"--Options.InputFileproperty will have "input.txt"app "input.txt"--Options.Filesproperty will have "input.txt"app "input.txt" -i "input2.txt"--Options.Filesproperty will have "input.txt",Options.InputFilewill have a "input2.txt". You choose how to resolve this conflict.app -t "output.txt"=Options.OutputFilewill have "output.txt"
In all cases, Options.InputFile and Options.OutputFile will have values, either the what the user specified, or the defaults. So the values present in Options.Files should override the values in Options.InputFile and Options.OutputFile.