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

Consider Supporting CliCommand, CliOption, CliArgument on methods and method parameters

Open lawrencek76 opened this issue 6 months ago • 1 comments

While working on converting an app to use command-line I discovered that it would be very convenient if this was also a supported syntax.

I tried to define some of the behavers that would be nice for the generator to support on functions below.

[CliCommand()]
internal class FunCommand
{
    [CliCommand()]
    public void RunFunction1(FileInfo inputFile, DriveType driveType = DriveType.Fixed)
    {
        Console.WriteLine($"First parameter by default is an Arg: {inputFile}");
        Console.WriteLine($"Second parameter by default is an option: {driveType}");
        Console.WriteLine("Running fun function1");
    }

    [CliCommand()]
    public void RunFunction2(FileInfo inputFileOpt, DriveType driveTypeArg = DriveType.Fixed)
    {
        Console.WriteLine($"First parameter is set to option by opt suffix on param name: {inputFileOpt}");
        Console.WriteLine($"Second parameter is set to argument by arg suffix on param name: {driveTypeArg}");
        Console.WriteLine("Running fun function2");
    }

    [CliCommand()]
    public void RunFunction3([CliOption()] string name)
    {
        Console.WriteLine($"Full control of param mapping by using the existing attributes on param: {name}");
        Console.WriteLine("Running fun function3");
    }

    [CliCommand()]
    public void RunFunctionFlag(bool flag)
    {
        Console.WriteLine($"bool is always treated as a option by default: {flag}");
        Console.WriteLine("Running fun function-flag");
    }

    [CliCommand(Name="FunctionNumber")]
    public void RunFunction3(int number)
    {
        Console.WriteLine($"if multiple functions in the same method group are clicommands then names will have to be provided to disambiguate them: {number}");
        Console.WriteLine("Running fun function-number");
    }
}

lawrencek76 avatar Jun 26 '25 14:06 lawrencek76

We already support methods in Delegate Model but not in classes:

using System;
using DotMake.CommandLine;

Cli.Run(([CliArgument]string arg1, bool opt1) =>
{
    Console.WriteLine($@"Value for {nameof(arg1)} parameter is '{arg1}'");
    Console.WriteLine($@"Value for {nameof(opt1)} parameter is '{opt1}'");
});

So it would not be hard to implement it also for class methods. I will check.

calacayir avatar Jun 26 '25 14:06 calacayir