Tyrrrz / CliFx

Class-first framework for building command-line interfaces
MIT License
1.48k stars 60 forks source link

Add option for adding multiple items when we specify List<object>? #70

Closed kibernetik542 closed 4 years ago

kibernetik542 commented 4 years ago

In my api I can use something like api/projects?statuses=1,2,3 it would be perfect also use this feature.

Example code:

    ```
    Another command options (working fine)

    [CommandOption("statuses", IsRequired = false)]
    public List<ProjectStatus?> Statuses { get; set; }

    public async ValueTask ExecuteAsync(IConsole console)
    {
        await console.Output.WriteLineAsync("Getting projects as yaml");
        var json = await Helpers.Transformer($"{TaikunURLs.getProjects}?statuses={Statuses}");
        var swaggerDocument = Helpers.ConvertJTokenToObject(JsonConvert.DeserializeObject<JToken>(json));

        var serializer = new YamlDotNet.Serialization.Serializer();

        using var writer = new StringWriter();
        serializer.Serialize(writer, swaggerDocument);
        var yaml = writer.ToString();
        await console.Output.WriteLineAsync(yaml);
    }


P.S. tried IReadOnlyList and it was not working in this case.
Tyrrrz commented 4 years ago

I'm not sure what exactly you are asking?

This should work: --statuses 1 2 3

kibernetik542 commented 4 years ago

Can't convert value "1,2,3" to type 'ProjectStatus' for option --statuses. Requested value '1,2,3' was not found.

Can't convert value "1,2,3" to type 'Nullable1' for option --statuses. Can't convert value "1,2,3" to type 'ProjectStatus' for option --statuses. Requested value '1,2,3' was not found. `

I am getting this errors, 1st without nullable enum second is nullable enum.

P.S. With postman and with swagger is working fine. I mean there is no issue in api.

Tyrrrz commented 4 years ago

Are you separating with spaces or commas?

Tyrrrz commented 4 years ago

Also, looks like you're formatting your list directly here: $"{TaikunURLs.getProjects}?statuses={Statuses}". That's not going to work. Maybe what you want is string and not List<string>.

kibernetik542 commented 4 years ago

In api, I am using comma for seperation something like ?statuses=1,2,3, btw I also tried string instead of list string but it does not failed but again filtering is not worked, in case of list it is failing

Tyrrrz commented 4 years ago

The way the arguments are bound has nothing to do with the API.

kibernetik542 commented 4 years ago

I mean by default it is not possible to pass multiple arguments as query params in aspnet core api, I created QueryStringValueProvider for this and after this I used multiple values with comma seperator, postman, fiddler, swagger, curl is working fine. Just wanted to know is it possible with CLI.

Tyrrrz commented 4 years ago

Are you asking if it's possible to send a request to a web server using a CLI? That seems completely unrelated.

kibernetik542 commented 4 years ago

I am asking is it possible to send list of string using CLI

Tyrrrz commented 4 years ago

It doesn't matter if it's a CLI or not, whatever you're sending it to doesn't care where it came from.

kibernetik542 commented 4 years ago

Agree,but somehow it works in other case which I mentioned but not working in CLI, If with CLI does not matter, it is something wrong in my code which I don't know what

Tyrrrz commented 4 years ago

Either change to public string Statuses { get; set; } and use --statuses 1,2,3 or keep this way and change await Helpers.Transformer($"{TaikunURLs.getProjects}?statuses={Statuses}"); to await Helpers.Transformer($"{TaikunURLs.getProjects}?statuses={string.Join(",", Statuses)}");