Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Spectre.Console.Cli.CommandSettings as Interface #1749

Open
BigPapa314 opened this issue Jan 31, 2025 · 0 comments
Open

Provide Spectre.Console.Cli.CommandSettings as Interface #1749

BigPapa314 opened this issue Jan 31, 2025 · 0 comments
Labels

Comments

@BigPapa314
Copy link

Context

I'm implementing a CLI app that shares some configuration settings among multiple different commands. One example is the output specification (file name or STDOUT). I'd like to create a separate ComandSettings class for just the output setting so that I can write some code like:

if (settings is OutputSettings outputSettings) {
    appContext.Ouput = File.Open(outputSettings.Output);
}

I also have some other command line settings that occur multiple times like DatabaseSettings, LoggingSettings, ....

The different commands whould need to compose these settings as they need. Like

public class ListDatabaseSettings : LoggingSettings, OutputSettings
{
}
public class ReadDatabaseSettings : LoggingSettings, OutputSettings, DatabaseConnectionSettings, DatabaseKeySettings
{
}
public class WriteDatabaseSettings : LoggingSettings, DatabaseConnectionSettings, DatabaseKeySettings
{
    [Description("The value to write to the database.")]
    [CommandOption("-v|--value")]
    public string Value { get; init; }
}

This does not work because C# is only able to derive from one base class.

Possible Solution

If the settings could be a C# Interfaces like in commandline then it would be possible to inherit from multiple Interfaces and compose the command line parameters more freely. A possible drawback is that the final classes have to implement the interfaces.

public interface IOutputSettings {
    [Description("The output to store the result.")]
    [CommandOption("-o|--output")]
    public string? Output { get; init; }
}

public class ListDatabaseSettings : ComandSettings, ILoggingSettings, IOutputSettings 
{
    public string? LogLevel { get; init; }
    public string? Output { get; init; }
}
public class ReadDatabaseSettings : ComandSettings, ILoggingSettings, IOutputSettings, IDatabaseConnectionSettings, IDatabaseKeySettings
{
    public string? LogLevel { get; init; }
    public string? Output { get; init; }
    public string DatabaseUrl { get; init; }
    public string Key { get; init; }
}
public class WriteDatabaseSettings : ComandSettings, ILoggingSettings, IDatabaseConnectionSettings, IDatabaseKeySettings
{
    public string? LogLevel { get; init; }
    public string DatabaseUrl { get; init; }
    public string Key { get; init; }

    [Description("The value to write to the database.")]
    [CommandOption("-v|--value")]
    public string Value { get; init; }
}

Please upvote 👍 this issue if you are interested in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Todo 🕑
Development

No branches or pull requests

2 participants