2

Using the CommandLineParser NuGet, when I run my application with an option like this.

myapplication.exe --searchfolder "c:\my great path\"

How can I use that options more than once? For example, if I want to pass in two folders...

  1. "c:\my great path"
  2. "c:\my other great path"

Currently, I use it like this for the single path given...

if (options.Verbose)
{
    m_Verbose = true;
    Console.WriteLine("Verbose mode on.");
}

if (options.SearchFolder != null && options.SearchFolder != "")
{
  Console.WriteLine("Searching folder '{0}'...", options.SearchFolder);
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109
  • What happens if you write two pathes? It's not clear from your code how you obtain even the first path. What is `options`? Please refer to [mcve]. – Sinatr Mar 05 '21 at 14:41
  • Is there a reason you want to use the `CommandLineParser` package? For something like this it may be easier to process `string[] args` yourself. – Dai Mar 05 '21 at 14:57

1 Answers1

2

You might want to use something like this:

class Options
{
  [Option('r', "read", Required = true, HelpText = "Input files to be processed.")]
  public IEnumerable<string> InputFiles { get; set; }

Check the official net fiddle here: https://dotnetfiddle.net/wrcAxr for the IEnumerable usage.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Awesome! Exactly what I was looking for. Thanks – Arvo Bowen Mar 05 '21 at 15:27
  • I would add to this answer for clarity that at the time of writing you could only use the flag once, and then repeat the parameter so for example: execute.exe -flag "a=b" "c=d" you can't use execute.exe -flag "a=b" -flag "c=d" from what I've tried. – The Senator Jul 27 '22 at 13:46