Search code examples
c#.netcommand-line-interfacesystem.commandline

How to check for specific parameter in Microsoft System.Commandline CLI


I am currently configuring a CLI command with the package System.CommandLine.

In my CommandClass I have added an option for the user to select if he wants to do a dry-run. How do I check if the user has given that specific paramter?

Snippets of my code:

using System.CommandLine;
namespace MyApp.Cli.commands;

public class MyCommandClass : Command 
{
    public MyCommandClass()
        : base("start-my-command", "test the functionalities of system.commandline")
    {
        AddOption(new Option<bool>(new string[] { "--dry-run", "-d" }, "Simulate the actions without making any actual changes"));
    }

    public new class Handler : ICommandHandler
    {
        private readonly ILogger<MyCommandClass> _log;

        public Handler(ILogger<MyCommandClass> log)
        {
            _log = log;
        }

        public async Task<int> InvokeAsync(InvocationContext context)
        {
            var isDryRun = /* check if the user has provided the parameter "--dry-run" */

            if (isDryRun)
            {
                _log.LogInformation("is dry run");
            }
            else
            {
                _log.LogInformation("is no dry run");
            }
        }
    }
    
}

I already tried to do var isDryRun = context.ParseResult.GetValueForOption<bool>("--dry-run"); but this just gives me the following error: Argument 1: cannot convert from 'string' to 'System.CommandLine.Option'.

Please help, thanks.


Solution

  • Instead of using context.ParseResult.GetValueForOption("--dry-run") it's better to refer directly to the Option object created when adding the option to the command.

    public class MyCommandClass : Command 
    {
        // Declaring the dry-run option at the class level
        private Option<bool> dryRunOpt = new Option<bool>(
            aliases: new[] { "--dry-run", "-d" },
            description: "Run in dry mode without actual changes"
        );
    
        public MyCommandClass() : base("start-my-command", "Testing the functionalities")
        {
            // Adding the dry-run option to our command
        // This line is important :)
            AddOption(dryRunOpt);
        }
    
        public class Handler : ICommandHandler
        {
            private readonly ILogger<MyCommandClass> logger;
    
            public Handler(ILogger<MyCommandClass> logger)
            {
                this.logger = logger;
            }
    
            public async Task<int> InvokeAsync(InvocationContext context)
            {
                // Checking the dry-run option value
                var isDryRun = context.ParseResult.GetValueForOption(dryRunOpt);
    
                if (isDryRun)
                {
                    logger.LogInformation("Running in dry-run mode.");
                }
                else
                {
                    logger.LogInformation("Executing normal operation.");
                }
    
                // ...
    
                return 0;
            }
        }
    }