Argu icon indicating copy to clipboard operation
Argu copied to clipboard

feat: correct way to specify a list that's allowed to be empty?

Open Gastove opened this issue 8 years ago • 3 comments

This isn't particularly an issue so much as point of confusion on my part. I need, effectively, a optional list. Specifying an argument as having type string list option throws an error, and the docs do specify Note that arguments that use optional or list must have precisely one parameter -- so, alright. But if I specify that a parameter has type string list, it's now required. What I'm looking for is, "if the user doesn't specify a list, the list will be empty list", say. Perhaps the correct approach is a default value in the App.config? Would be very curious if there's a "recommended" way to do this.

Gastove avatar Sep 14 '17 17:09 Gastove

You can use the GetResults method which returns a list of all arguments. So if you pass the args

-f 1 -f 2 -f 3

Then GetResults <@ f @> would give back [1 ; 2 ; 3]. Assuming -f works with lists, then you'd be given a list of lists, so concatenating that result should be sufficient to get the desired result.

eiriktsarpalis avatar Sep 25 '17 06:09 eiriktsarpalis

I ran into a similar issue. let's say I want something like

type Arguments =
    | Numbers of numbers : int list
    interface IArgParserTemplate with
        member s.Usage =
            match s with
            | Numbers _ -> "Optional list of numbers to process (e.g. 10042 10043)"

So that I could invoke the app in two ways

app.exe --numbers 10042 10043 or app.exe

The former behaves as expected. However, the later throws Unhandled Exception: Argu.ArguParseException: ERROR: missing argument '--numbers'.

Whereas what one would expect to happen is that the parse would pass with the following behaviour

arguments.GetResult(<@ Arguments.Numbers @>) //Returns []

Currently, the workaround is do something like app.exe --numbers

If you agree with the proposed behavior, I should be able to implement it and send a PR

odytrice avatar May 17 '19 15:05 odytrice

Sounds reasonable. It's been ages since I wrote that code, but I have a slight hunch that the restriction might have been motivated by technical reasons.

eiriktsarpalis avatar May 17 '19 16:05 eiriktsarpalis