Unable to use `ConsoleAppContext` in separately implemented commands.
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.
The solution is to change public class to internal class.
However, I see, ConsoleAppContext might be fine as public rather than internal.
I'll consider making the change after thinking about whether there would be any side effects.
I tried changing public class to internal class, but it was still giving me the CAF012 error.
Edit: Ah, nevermind. It works once you have internal class and a public method.