commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Use keyword to set positional argument

Open nycki93 opened this issue 7 years ago • 4 comments

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?

nycki93 avatar Oct 01 '19 00:10 nycki93

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; }   
}

moh-hassan avatar Oct 09 '19 22:10 moh-hassan

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

nycki93 avatar Oct 30 '19 01:10 nycki93

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; }   
}

moh-hassan avatar Oct 30 '19 18:10 moh-hassan

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.Files property will have "input.txt" and "output.txt".
  • app -i "input.txt" -- Options.InputFile property will have "input.txt"
  • app "input.txt" -- Options.Files property will have "input.txt"
  • app "input.txt" -i "input2.txt" -- Options.Files property will have "input.txt", Options.InputFile will have a "input2.txt". You choose how to resolve this conflict.
  • app -t "output.txt" = Options.OutputFile will 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.

UnnDunn avatar Feb 05 '20 17:02 UnnDunn