command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

Cannot parse char argument

Open TiTiKy441 opened this issue 2 months ago • 0 comments

.NET 9 with System.CommandLine 2.0.0-rc.2.25502.107 installed via nuget in Visual Studio, win 10 x64

When I try to use a char type option, I always get Cannot parse argument 'b' for option '--char-option' as expected type 'System.Char'.

Test code:

using System.CommandLine;
using System.CommandLine.Parsing;

namespace TestApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Option<char> charOption = new Option<char>("--char-option");
            charOption.Description = "Character option";
            charOption.Required = true;
            charOption.DefaultValueFactory = charOptionValue => 'a';

            RootCommand rootCommand = new RootCommand("Test application");
            rootCommand.Add(charOption);
            rootCommand.Parse(args);

            ParseResult parseResults = rootCommand.Parse(args);

            foreach (ParseError error in parseResults.Errors)
            {
                Console.WriteLine(error.Message);
                return;
            }

            Console.WriteLine("Character option is {0}", parseResults.GetValue(charOption));
        }
    }
}

Results:

TestApp
Character option is a

TestApp --char-option b
Cannot parse argument 'b' for option '--char-option' as expected type 'System.Char'.

TestApp --char-option 'b'
Cannot parse argument ''b'' for option '--char-option' as expected type 'System.Char'.

TestApp --char-option "b"
Cannot parse argument 'b' for option '--char-option' as expected type 'System.Char'.

TestApp --char-option 1
Cannot parse argument '1' for option '--char-option' as expected type 'System.Char'.

TestApp --char-option 2
Cannot parse argument '2' for option '--char-option' as expected type 'System.Char'.

Please let me know if I am doing something wrong here

TiTiKy441 avatar Oct 18 '25 12:10 TiTiKy441