Skip to content

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

Description

@MiloszKrajewski

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?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions