dotnet / dotnet/command-line-api

Require non-empty Argument of collection type

Open
#2,513 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

If i specify an Argument like this:

```cs
private readonly Argument> 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:

```cs
private async Task 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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.