question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remaining arguments gets reparsed and split on spaces

See original GitHub issue

Forked from spectresystems/spectre.cli#108 as requested

Scenario

My application wraps around execution of other applications so I need to pass all arguments as is, for example, this:

app run cmd -- /c "set && pause"

tells my app (app) to run (run) other application (cmd) and pass "other arguments “as is” (/c "set && pause"). I can see though that single argument “set && pause” get’s reparsed as 3 “set”, “&&”, and “pause” (in context.Remaining.Raw) and I lose all clues that it ever was a single one.

Code

class Program
{
	static void Main(string[] args)
	{
		var testArgs = new[] {
			"run",
			"cmd",
			"-e", "A=7",
			"-e", "B=10",
			"--",
			"/c", "set && pause"
		};
		var app = new CommandApp();
		app.Configure(config => config.AddCommand<RunCommand>("run"));
		app.Run(testArgs);
	}
}

public class RunCommand: Command<RunCommand.Settings>
{
	public class Settings: CommandSettings
	{
		[CommandArgument(0, "<EXECUTABLE>")]
		public string Executable { get; set; } = null!;

		[CommandOption("-e <VAR=VAL>")]
		public string[] Environment { get; set; } = Array.Empty<string>();
	}

	public override int Execute(CommandContext context, Settings settings)
	{
		Console.WriteLine(JsonConvert.SerializeObject(settings));
		Console.WriteLine(JsonConvert.SerializeObject(new {
			Parsed = context.Remaining.Parsed.ToArray(),
			Raw = context.Remaining.Raw.ToArray()
		}));
		return 0;
	}
}

Expected

{"Executable":"cmd","Environment":["A=7","B=10"]}
{"Parsed":[],"Raw":["/c","set && pause"]}

Actual

{"Executable":"cmd","Environment":["A=7","B=10"]}
{"Parsed":[],"Raw":["/c","set","&&","pause"]}

(See: Raw array, expected 2 arguments, received 4 arguments)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
patriksvenssoncommented, Jan 1, 2021

@MiloszKrajewski Btw, I notice that you’re trying to parse the form -e FOO=BAR -e BAZ=QUX. This is already supported. Just set the type of your Environment property to ILookup<string, string>, and it will be mapped automatically.

public class Settings : CommandSettings
{
	[CommandArgument(0, "<ARGUMENT>")]
	public string[] Arguments { get; set; } = null!;

	[CommandOption("-e <VAR=VAL>")]
	public ILookup<string, string> Environment { get; set; }
}

The types ILookup<string, string>, IDictionary<string, string> and IReadOnlyDictionary<string, string> will work here, but the two latter will, of course, not support multiple values for the same key.

0reactions
MiloszKrajewskicommented, Jan 1, 2021

Yes it is what I would expect. Although… I by accident discovered unrelated bug (I managed to freeze/infinite loop parser). I’ll create new issue (#186)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Split a string containing json w/spaces into arguments
I am implementing a command shell using cmd2, and I wanted to setup a few commands that allows a user to type in...
Read more >
shell - Is it possible for a program to get number of spaces ...
In general, no. Command line parsing is done by the shell which does not make the unparsed line available to the called program....
Read more >
Why does xargs split on spaces even with -0?
No, it doesn't split on spaces. The shell splits the output of the $(…) expansion on spaces, and xargs can do absolutely nothing...
Read more >
String.prototype.split() - JavaScript - MDN Web Docs - Mozilla
The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the...
Read more >
Split text by empty spaces
Hello all,. I'm trying to split a text (e.g. "MR. JOHN SMITH" or "MRS. JANE AUSTIN SMITH") into chunks in order to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found