0

Is there a C# commandline arguments processor that allows unknown arguments and spaces in arguments. I've been digging for a while but cant find any. The heavyweight CommandLineParser doesnt appear to have the ability to allow unknown parameters and the Microsoft extensions CommandLine doesnt allow spaces.

Say I have the command line;

text.exe -param1:'It has spaces'

I want to be able to use this as so;

var params = ProcessCommandLine(args);

Console.Writeline ("Param1 = [" + params["param1"] + "]");


And get;

Param1 = [It has spaces]

Mat Walker
  • 45
  • 1
  • 7
  • Did you try to wrap the parameter with double quotes? – VRoxa Apr 05 '20 at 23:31
  • 2
    Spaces are a problem of the command line itself. If you do not use double quotes the appliction doesn't receive the arguments correctly in the first place. In your example the application receives the arguments as an array with the entries "-param1:It'", "has" and "spaces'" – Gargo Apr 05 '20 at 23:36
  • @Gargo Windows applications have access to the raw command line string and the parsing is done in the app (or simulated somewhere therein), not in the shell. (Or, minimal is done in the shell - really depends on the shell insofar as what it will forward and how.) – user2864740 Apr 05 '20 at 23:37
  • 1
    @user2864740 you are right. With `Environment.CommandLine` when you cut away the path to the executable. My guess was that most libraries will use the args argument or `Environment.GetCommandLineArgs()` where the arguments are split the wrong way with single quotes. At least on Windows. – Gargo Apr 05 '20 at 23:43
  • In windows, command line is split on spaces. IE. "text.exe", "-param1:'It", "has", "spaces'". @VRoxa - yes. It is the Command line processer used (IE CommandLineParser etc) that processes the arguments. – Mat Walker Apr 05 '20 at 23:47
  • Asking for recommendations for libraries, etc. is off-topic for Stack Overflow. In any case, it is always possible to format the command line such that you can correctly pass text with spaces in it. If your arguments are being passed to the program with incorrect tokenization, it's because you didn't type the command line arguments correctly, not because the parser didn't support spaces. – Peter Duniho Apr 06 '20 at 02:42

0 Answers0