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.

Simple switches end up on "remaining" list even if parsed properly

See original GitHub issue

Information

  • OS: Windows 10
  • Version: 0.36 + 0.36.1-preview0.9
  • Terminal: Cmder / Powershell

Describe the bug

What I mean by simple switch:

I assume we can pass bool values in two ways:

[CommandOption("-a <BOOL>")]
public bool A { get; set; }

[CommandOption("-b")]
public bool B { get; set; }

and use them as:

app -a true -b

By simple switch I mean -b (but NOT -a <bool>)

So, even if parsed properly simple switches are also reported on context.Remaining.Parsed list

To Reproduce

using System;
using System.Linq;
using Newtonsoft.Json;
using Spectre.Console.Cli;

namespace SpectreBug
{
	class Program
	{
		static void Main(string[] args)
		{
			var testArgs = new[] {
				"-a", "true", 
				"-b"
			};
			new CommandApp<MyCommand>().Run(testArgs);
		}
	}

	public class MyCommand: Command<MyCommand.Settings>
	{
		public class Settings: CommandSettings
		{
			[CommandOption("-a <BOOL>")]
			public bool A { get; set; }
			
			[CommandOption("-b")]
			public bool B { get; set; }
		}

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

Expected behavior

Both A and B are set to true, and context.Remaining.Parsed is empty.

{"A":true,"B":true}
{"Parsed":{},"Raw":[]}

Actual behaviour

Both A and B are set to true, but context.Remaining.Parsed still contains -b like it was not recognized.

{"A":true,"B":true}
{"Parsed":{"b":[null]},"Raw":[]}

Additional context

…or maybe I don’t understand how bool options should be defined?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
phil-scott-78commented, Sep 26, 2022

One thing I noticed here is that if you enable strict parsing, you get the expected behavior.

var app = new CommandApp<MyCommand>();
app.Configure(i => i.UseStrictParsing());
app.Run(testArgs);

I’m not sure if this makes it a bug or expected behavior. But here’s the theoretically guilty line if this isn’t what should be happening

0reactions
FrankRay78commented, Nov 16, 2022

FYI. I’ve raised PR https://github.com/spectreconsole/spectre.console/pull/1070 which fixes this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I check that a switch block is exhaustive in ...
You will get an error if your switch statement is non-exhaustive as there will be a case where nothing is returned. From your...
Read more >
Handy one-liners for SED
In method 2, spaces at # the beginning of the line are discarded in centering the line, and # no trailing spaces appear...
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
If no default clause is found, the program resumes execution at the statement following the end of switch . (By convention, the default...
Read more >
What a very bad day at work taught me about building ...
I'm the new Director of Public Q&A at Stack Overflow. ... Even kind feedback can come off as caustic and mean when there...
Read more >
optparse — Parser for command line options
As it parses the command line, optparse sets attributes of the options object ... args , the list of positional arguments leftover after...
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