Handling quoted parameter values

Jul 12, 2011 at 4:10 PM

Hi there, I can't seem to find any documentation on this...

My option definition:

class Options
{
        [OptionArray("d", "directories", Required = false, HelpText = "A space seperated list of directories ot upload.")]
        public string[] InputDirectories = null;
}

The user enters:

 

MyCLIProgram.exe --directories "C:\Program Files\"

 

The result in code:

options.InputDirectories[0] == "C:\Program Files\""

Note that there are two double quotes at the end and a single double quote at the beginning. I'd like to receive the value

C:\Program Files\
(no surrounding quotes). How do I do this without removing them myself?

 

Jul 12, 2011 at 4:23 PM

Sorry my mistake. There is only one trailing double quote at the end. It looks like if the user enters a slash as the last character of a quoted string at the command line that the options value gets set with a trailing double quote because it gets escaped.

options.InputDirectories[0] == C:\Program Files\"
Coordinator
Jul 13, 2011 at 7:44 AM

Dear antize,

are you sure? May be that you are invoking your program using the following command line: 

MyCLIProgram.exe --directories "C:\Program Files\""

 

Another thing, do you use Command Prompt or PowerShell? Things may change.

Please check if you add a trailing quote or tell me what terminal app you use...

Thank you for using this software!

Regards,

Giacomo

Jul 13, 2011 at 6:28 PM

Same behavior in both.

Command 1:

TestCommandLine.exe -d "foo bar"

Output 1:

foo bar

 

Command 2:

TestCommandLine.exe -d "foo bar\"

Output 2:

foo bar"

Jul 13, 2011 at 6:31 PM

To reproduce create a C# console app with a reference to CommandLine.dll 1.8.0.0.

 

Options.cs

 

using System;
using System.Collections.Generic;
using System.Text;
using CommandLine;
using CommandLine.Text;

namespace TestCommandLine
{
    class Options
    {
        [OptionArray("d", "directories", Required = false, HelpText = "A space seperated list of directories ot upload.")]
        public string[] InputDirectories = null;

        [HelpOption(HelpText = "Dispaly this help screen.")]
        public string GetUsage()
        {
            HelpText help = new HelpText(string.Format(
                    "CommandLine Tester {0}",
                    System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
                ));

            help.AdditionalNewLineAfterOption = true;

            help.AddOptions(this);
            return help;
        }
    }
}

 

Program.cs

 

using System;
using System.Collections.Generic;
using System.Text;
using CommandLine;
using System.IO;

namespace TestCommandLine
{
    class Program
    {
        static void Main(string[] args)
        {
            Options options = new Options();
            ICommandLineParser parser = new CommandLineParser();

            // Container for the directories to process.
           
            List<DirectoryInfo> dirsToUpload = new List<DirectoryInfo>();

   
            if (parser.ParseArguments(args, options))
            {
                if (options.InputDirectories != null)
                {
                    string trimmedDirectory = null;

                    foreach (string directory in options.InputDirectories)
                    {
                        trimmedDirectory = directory.TrimEnd('"');

                        Console.WriteLine(directory);
                        Console.WriteLine(trimmedDirectory);
                    }
                }
            }
        }
    }
}

Coordinator
Jul 15, 2011 at 4:40 AM
Edited Jul 15, 2011 at 4:41 AM

Antize,

I've compiled your code with a reference to the 1.8 stable version of the library.

The result of this command

YourApp.exe -d aaa bbb ccc "Hello Shell"

is the same in cmd.exe or powershell:

aaa
aaa
bbb
bbb
ccc
ccc
Hello Shell
Hello Shell

As you can see no difference between:

Console.WriteLine(directory);
Console.WriteLine(trimmedDirectory);

So I don't know what's wrong with your test made in your computer... Sorry!

Without more informations, I can't help you...

Regards,

Giacomo

Jul 15, 2011 at 6:00 AM

Hi there, I guess you missed this part:

TestCommandLine.exe -d "foo bar\"


To replicate the issue in your example:

YourApp.exe -d aaa bbb ccc "Hello Shell\"

 

The output is the same in cmd.exe and powershell:

foo bar"

Hello Shell"

Notice the unexpected trailing double quote and the missing trailing back slash. The expected result is:

for bar\

Hello Shell\

Coordinator
Jul 15, 2011 at 3:11 PM

antize,

why you need to add trailing slash? Because you're managing directory? Maybe that for get

foo bar\

you need

to input

YoutApp.exe -d "foo bar\\"

Please verify it...

For the shell "foo bar\" (with one slash) means that you want to input a double quote. Two slash -> one slash. OK?

Regards,

Giacomo