0

I have to parse an imposed command line which mix single/double dash args, args with = or as separator,etc (not very proper....)

The command line is like myexe.exe -Url=https://blabla.com/ --TestValue=val1 -monoapp -Ctx "default ctx" Open -Test2=CO0ZJP6f-ca

I'm trying to do that with commandlineparser. (I suppose it's able to manage that, right ? )

So first I'm trying to parse the first parameter (myexe.exe -Url=https://blabla.com/).


public class CommandLineOptions
{
   [Option("Url", Required = true)]
   public string Url { get; set; }
}

....... In another file but in the  same  assembly

static void Main(string[] args) // args[0] = "-Url=https://blabla.com/" 
{

   var commandLineOptions = new CommandLineOptions();


   var parseResult = Parser.Default.ParseArguments<CommandLineOptions>(args).WithParsed(result => commandLineOptions = result);

   System.Console.WriteLine(parseResult.Tag); //NotParsed
   System.Console.WriteLine(commandLineOptions.Url);
}

With that code, I have 2 errors CommandLine.MissingRequiredOptionError and CommandLine.UnknownOptionError.

(The MissingRequiredOptionError is produced beacause it cannot find the Url parameter)

So do you know where is my mistake ?

Thanks in advance for your help ;)

David
  • 1,177
  • 3
  • 11
  • 26

1 Answers1

0

So final dev from commandlineparser said it isn't possible to parse it. A single line option must be one char option. The only way to bypass that is to preprocess the arguments.

I did this, it's working but it not allow short option composition (like in tar -xvf for example)

args = args.Select(arg => Regex.IsMatch(arg, "^-\\w{2,}") ? "-" + arg : arg ).ToArray();
David
  • 1,177
  • 3
  • 11
  • 26