1

I am using the C# CommandLineParser to handle my command-line arguments.

https://github.com/commandlineparser/commandline

The only options I allow on the command-line are:

myprogram.exe -a 4 -b -c value

If I accidentally forget a dash on an optional Option (argument) like:

myprogram.exe -a b -c

The program still runs and doesn't complain about "b". How can I report an error if an expected argument like this is specified? I've tried using:

var parser = new CommandLine.Parser(s =>
{
    s.IgnoreUnknownArguments = false;
});

But this doesn't seem to flag any error. Ideas?

roapp
  • 530
  • 6
  • 17
paultechguy
  • 2,318
  • 4
  • 25
  • 34

2 Answers2

0

I am not familiar with C# CommandLinePArser class, however from the documentation of it looks very similar to Apache Commons CLI, so I shall offer a suggestion based on the similarity (this should not be considered as a complete answer). In your case, the program will not complain about 'b', since it is considered as an argument of option 'a'. The only way is to handle it in the parsing stage in your program. One way is to query the command line for presence of an option and its value and then check if the value is within permissible space for that option, refer the CLI command line querying. Hope it helps.

Ironluca
  • 3,402
  • 4
  • 25
  • 32
  • Thanks, unfortunately the option 'a' is a bool option so it wouldn't expect an argument for it anyway. I'm starting to think there must be a way that just ask CommandLineParser, are there any positional arguments...and if so, I have to flag an error. But, I can't find a way to see any positional arguments unless I add them to my POCO class, which doesn't make sense since there are no positional arguments. :-( – paultechguy Jun 23 '19 at 18:21
0

FluentArgs (see: https://github.com/kutoga/FluentArgs) has an option to control this behaviour. It uses a predefined error message, but it can be customized (search for RegisterParsingErrorPrinter on the Github page). Your code would look like (assuming all arguments are optional):

using FluentArgs;
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            FluentArgsBuilder.New()
                .DisallowUnusedArguments()
                .Parameter("-a").IsOptional()
                .Parameter("-b").IsOptional()
                .Parameter("-c").IsOptional()
                .Call(c => b => a =>
                {
                    Console.WriteLine($"a={a ?? "null"}");
                    Console.WriteLine($"b={b ?? "null"}");
                    Console.WriteLine($"c={c ?? "null"}");
                })
                .Parse(args);
        }
    }
}

I assumed a, b and c are string-arguments. If they are flags, it can be done like this:

using FluentArgs;
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            args = new[] { "-a", "hey", "du" };
            FluentArgsBuilder.New()
                .DisallowUnusedArguments()
                .Flag("-a")
                .Flag("-b")
                .Flag("-c")
                .Call(c => b => a =>
                {
                    Console.WriteLine($"a={a}");
                    Console.WriteLine($"b={b}");
                    Console.WriteLine($"c={c}");
                })
                .Parse(args);
        }
    }
}
Kevin Meier
  • 2,339
  • 3
  • 25
  • 52