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

Escaped quote at the end of quoted argument #106

Open
WouterTinus opened this issue Feb 1, 2019 · 1 comment
Open

Escaped quote at the end of quoted argument #106

WouterTinus opened this issue Feb 1, 2019 · 1 comment

Comments

@WouterTinus
Copy link

I ran into a little bug with the latest Nuget package (1.4.3).

--param "something something" is parsed as something something
--param "something \"{4}\" something" is parsed as something \"4\" something

However:

--param "something \"{4}\"" is expected to be something \"4\" but instead gives me something \"4, dropping the final quote.

Thanks for a nice library!

@oleksabor
Copy link

oleksabor commented Sep 19, 2019

The problem is in the UsefulExtension.Remove method:

public static string RemoveAnyWrappingDoubleQuotes(this string str)
{
	return str.IsNullOrWhiteSpace()
		       ? str
		       : str.TrimStart('"').TrimEnd('"');
}

WrapInDoubleQuotes is executed if there is space in the option value. And there is space. So value like something "4" is wrapped to the "something "4"" when CommandLineParserEngineMark2 does its job.
But then StringCommandLineOptionParser.Parse removes the double quote and removes the all double quotes at the end :(

Here is test case

[TestFixture]
public class when_there_is_qoute_in_the_end 
{
	[Test]
	public void parser()
	{
		var args = new[] { "--param", "something \"4\"" };

		var sut = new Fclp.FluentCommandLineParser<Config>();
		sut.Setup(_ => _.Param).As('p', "param");
		var res = sut.Parse(args);

		Assert.AreEqual("something \"4\"", sut.Object.Param);
	}
	[Test]
	public void RemoveAnyWrappingDoubleQuotes()
	{
		var str = "something \"4\"";
		str = str.WrapInDoubleQuotes();
		str = str.RemoveAnyWrappingDoubleQuotes();
		Assert.AreEqual("something \"4\"", str);
	}
}

I've adjusted the RemoveAnyWrappingDoubleQuotes method like below

public static string RemoveAnyWrappingDoubleQuotes(this string str)
{

	if (!str.IsNullOrWhiteSpace())
		if (str.StartsWith("\"") && str.EndsWith("\""))
			return str.Substring(1, str.Length - 2);
	return str;
}

and it works

oleksabor pushed a commit to oleksabor/fluent-command-line-parser that referenced this issue Sep 19, 2019
siywilliams added a commit that referenced this issue Sep 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants