Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require non-empty Argument of collection type #2513

Open
Rast1234 opened this issue Jan 27, 2025 · 2 comments
Open

Require non-empty Argument of collection type #2513

Rast1234 opened this issue Jan 27, 2025 · 2 comments

Comments

@Rast1234
Copy link

Rast1234 commented Jan 27, 2025

If i specify an Argument like this:

private readonly Argument<List<string>> inputArg = new("input", "Files to process");

I don't get any validation if the list is empty, as with single-token arguments. Right now i'm doing this to check for emptiness and print help to replicate default behavior which is not straightforward too:

private async Task<int> Handle(InvocationContext context, CancellationToken token)
    {
        var input = context.ParseResult.GetValueForArgument(inputArg);
        if (!input.Any())
        {
            var name = GetType().Name.ToLowerInvariant();
            await using var sw = new StringWriter();
            await sw.WriteLineAsync($"Required argument missing for command: '{name}'.\n");
            context.HelpBuilder.Write(this, sw);
            context.Console.Write(sw.ToString());
            return 0;
        }
        // rest of the handler
    }
  • Is there a way to specify that collection-type argument should be not empty?
  • Is there a way to trigger validation failure, simpler than HelpBuilder.Write?
@baronfel
Copy link
Member

baronfel commented Jan 27, 2025

What happens if you set the Arity property on inputArg to ArgumentArity.OneOrMore?

@Rast1234
Copy link
Author

Thanks, this works! Totally missed it. Was searching for something like "system.commandline list|collection not empty|required".

Anyway, any validation and help output tips? Maybe i missed something too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants