8000 Enum as Option is not working · Issue #1016 · dotnet/command-line-api · GitHub
[go: up one dir, main page]

Skip to content
Enum as Option is not working #1016
Closed
Closed
@markusr

Description

@markusr

I want to use enum with getDefaultValue. However, it does not work as expected.
My understanding is that AppFunc should output Fatal.

$ dotnet run
Verbose

Also the parsing does not seem to work at all:

$ dotnet run -- -v Fatal
Verbose

See the following code:

using System;
using System.CommandLine;
using System.CommandLine.Invocation;

namespace daemon_limitcalc
{
    public static class Program
    {
        public enum LogEventLevel
        {
            Verbose = 0,
            Debug = 1,
            Information = 2,
            Warning = 3,
            Error = 4,
            Fatal = 5
        }

        public static int AppFunc(LogEventLevel loglevel)
        {
            Console.WriteLine(loglevel);
            return 0;
        }
        public static int Main(string[] args)
        {
            // Create a root command with some options
            var rootCommand = new RootCommand
            {
                new Option(new[] {"-v", "--verbose" })
                {
                    IsRequired = false,
                    Argument = new Argument<LogEventLevel>(
                        getDefaultValue: () => LogEventLevel.Fatal
                    )
                },
            };

            rootCommand.Description = "Demo program";

            // Note that the parameters of the handler method are matched according to the names of the options
            rootCommand.Handler = CommandHandler.Create<LogEventLevel>(AppFunc);

            return rootCommand.InvokeAsync(args).Result;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0