Search code examples
c#.netcommand-line-arguments

C#: CommandLineParser, how to pass an array of strings?


I'm using CommandLineParser and I want to pass multiple arguments which should end up in a single array. Here's my class:

public class ClientConfig : Config
{

    [Option("load")]
    public string[]? Load { get; set; }

}

When I use the following command line:

my.exe --load=1 --load=2 --load=3

When parsed my "Load" property should be the following: ["1", "2", "3"]. However when I try this I get the following exception:

System.InvalidOperationException: 'Sequence contains no elements'

How can I fix this?


Solution

  • In the docs it uses IEnumerable for array of string so by this ClientConfig:

     public class ClientConfig
        {
            [Option("load")]
            public IEnumerable<string> Load { get; set; }
    
        }
    

    this worked for me:

    my.exe --load 1 2 3