Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verbatim values are parsed as options (when they shouldn't be) #159

Closed
ericnewton76 opened this issue Nov 4, 2017 · 5 comments
Closed
Labels

Comments

@ericnewton76
Copy link
Member

Issue by cdmihai
Friday Aug 18, 2017 at 23:32 GMT
Originally opened as gsscoder/commandline#470


Given an options class with an option of type string:

    class Options
    {
        [Option('c', "command", HelpText = "command")]
        public string Command { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var result = Parser.Default.ParseArguments<Options>(args);

            result
                .WithParsed<Options>(
                    o =>
                    {
                        Console.WriteLine($"{o.Command}");
                    })
                .WithNotParsed(
                    e =>
                    {
                        Environment.Exit(1);
                    });
        }
    }

I would expect multi word values surrounded by quotes to be treated verbatim as a value for the string option.

Instead, CommandLineParser may end up parsing the verbatim string when it shouldn't:

MeasureCommand.exe -c "-a -e"
MeasureCommand 1.0.0
Copyright (C) 2017 MeasureCommand

ERROR(S):
  Option 'a' is unknown.

  -c, --command    command

  --help           Display this help screen.

  --version        Display version information.


MeasureCommand.exe -c "-c -a"
MeasureCommand 1.0.0
Copyright (C) 2017 MeasureCommand

ERROR(S):
  Option 'c, command' is defined multiple times.

  -c, --command    command

  --help           Display this help screen.

  --version        Display version information.


MeasureCommand.exe -c " -c -a"
c=[ -c -a]

MeasureCommand.exe -c "foo -c -a"
c=[foo -c -a]
@wexman
Copy link

wexman commented Dec 21, 2017

I have the same problem. I want to specify parameters to my exe that are to be passed on to another .exe. For example:

starter.exe SomeOtherExe -f "this is to=be -unparsed"

@wexman
Copy link

wexman commented Dec 21, 2017

Just found out that it works - kind of - when using a blank after the opening quotes, for example

starter.exe SomeOtherExe -f " this is to=be -unparsed"

(the difference will probably be hard to notice). Only downside is that the resulting string option will include the leading blank, but that should be handable.

@wexman
Copy link

wexman commented Dec 21, 2017

Now I still need to find out how (if at all) to escape quotes when needing to pass on these...

@moh-hassan
Copy link
Collaborator

moh-hassan commented Sep 28, 2019

This is not a bug, it's how CMD handle quotation marks and process commandline to get string[] args.
use triple quote like:

              MeasureCommand.exe -c """-a -e"""

in that case "-a -c" will be passed as one string to the Parser.

@RobThree
Copy link

I'm trying to build a string:

grep {options}

So I'm trying mycommand.exe -g "-i foo" to get grep -i foo. I just don't get this to work; even when I try the suggested triple quotes: mycommand.exe -g """-i foo""" results in grep "-i foo", but I don't want the quotes. I could trim the quotes but then the problem is that I want to be able to quote values too: grep -i "some thing foo" for example. I... just don't see how?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants