Skip to content

Unable to use ConsoleAppContext in separately implemented commands. #229

@willnationsdev

Description

@willnationsdev

If I have the following code:

using ConsoleAppFramework;

// Create a CancellationTokenSource that will be cancelled when 'Q' is pressed.
var cts = new CancellationTokenSource();
_ = Task.Run(() =>
{
    while (Console.ReadKey().Key != ConsoleKey.Q) ;
    Console.WriteLine();
    cts.Cancel();
});

var app = ConsoleApp.Create();

app.Add<BasicCommands>();

await app.RunAsync(args, cts.Token);

public class BasicCommands
{
    /// <summary>
    /// Prints a hello world message and cites the command source.
    /// </summary>
    /// <returns>exit code</returns>
    [Command("")]
    public int Run(ConsoleAppContext context)
    {
        Console.WriteLine("Hello world! ({0})", context.CommandName);
        return 0;
    }
}

You get the error:

CS0051: Inconsistent accessibility: parameter type 'ConsoleAppContext' is less accessible than the method 'BasicCommands.Run(ConsoleAppContext)'

If you change the Run method to be internal, instead of public, then you get this error:

CAF012: ConsoleAppBuilder.Add<T> class must have at least one public method.

BUT, if you change await app.RunAsync(args, cts.Token) to become just app.Run(args);, AND you have the method declared as internal, then it all works fine.

This prevents you from having an asynchronous execution in Program.cs if you want to customize the doc comments of the root command in any way. Furthermore, the internal commands won't even get picked up by the ConsoleAppBuilder, so it seems the only way you can use the ConsoleAppBuilder is if you use the standard lambda-based Add method.

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