Inheriting options does not work
Closed this issue · 2 comments
PatrickBig commented
I have some commands that have some shared options.
Eg: I have 5 commands that all require a --username
and --password
option.
I would expect that I could do something like this:
public abstract class CredentialCommandBase
{
[CliOption(Description = "Username of the identity performing the command")]
public string Username { get; set; }
[CliOption(Description = "Password of the identity performing the command")]
public string Password { get; set; }
}
Then implement thusly
[CliCommand]
public MyCommand : CredentialCommandBase
{
public void Run(InvocationContext context)
{
Console.WriteLine("I am " + Username);
}
}
Compiles just fine, but when ran you will not get any help information, nor can you set the Username property.
I should also add that it would be nice if it could pickup annotations from interfaces to generate as well.
calacayir commented
You are right, this feature is needed. It will be added shortly.
I should also add that it would be nice if it could pickup annotations from interfaces to generate as well.
I guess this could be also done, as interfaces allow properties.
calacayir commented
This is now implemented in v1.5.8, please check and let me know.
- Added inheritance support for CliCommand classes, i.e. CliOption and CliArgument from inherited classes or interfaces will be included.
The property attribute and the property initializer from the most derived class in the hierarchy will be used (they will override the base ones).
The command handler (Run or RunAsync) will be also inherited.
This is useful when you have repeating options or arguments for your commands, i.e. you can define them once in a base class and then share them by inheriting that base class.
[CliCommand]
public class InheritanceCliCommand : CredentialCommandBase, IDepartmentCommand
{
public string Department { get; set; } = "Accounting";
}
public abstract class CredentialCommandBase
{
[CliOption(Description = "Username of the identity performing the command")]
public string Username { get; set; } = "admin";
[CliOption(Description = "Password of the identity performing the command")]
public string Password { get; set; }
public void Run()
{
Console.WriteLine($@"I am {Username}");
}
}
public interface IDepartmentCommand
{
[CliOption(Description = "Department of the identity performing the command (interface)")]
string Department { get; set; }
}