0

I'm testing out System.CommandLine.

There will be several options passed to my executable, but I only care about one of them. If I don't define them all, I will get errors:

Unrecognized command or argument '-i'.
Unrecognized command or argument '3'.

Is there any way that I can tell System.CommandLine to ignore any options that aren't specified by my code? E.g.:

var root = New RootCommand("Test application");
var cmd = new Command("--example", "example command");
root.Add(cmd);
await root.InvokeAsync(args);

Then if I try calling my program via:

test example --startDir=C:\temp --FileCnt=7 --Delay=10

I'll get 6 of those 'Unrecognized command or argument' errors.

The reason that I would like to not have to specify all the options is that my program is called from another program. The parameters are being passed from the calling program, and I don't know what all of them will be.

I do know what the two important ones I need to parse are, and the rest I don't care about.

briddums
  • 1,826
  • 18
  • 31
  • Not really clear what the issue is. Sounds like a simple if-block or switch-block should take care of it. Just handle the arguments you care about. – LarsTech Feb 24 '23 at 20:48
  • System.CommandLine automatically generates those error messages when it finds options that weren't specified. I haven't been able to find a flag telling it to not create those error messages. – briddums Feb 24 '23 at 20:58
  • Didn't realize `System.CommandLine` was a new package. See [How to specify C# System.Commandline behaviour when no arguments are provided?](https://stackoverflow.com/q/74111111/719186), it might have some bits that relate to your issue. – LarsTech Feb 24 '23 at 21:22

1 Answers1

0

I recently run into similar problems. For my workaround, I try to call parse first then invoke. Like this:

var processedRes = Commands.RootCommand.Parse(args);
await processedRes.InvokeAsync();

This will ignore all unknown tokens.

YukiShu
  • 1
  • 1