commandlineparser / commandlineparser/commandline

Common options when using verbs

Aperta
#624 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
C#
Stelle
4.8k
Fork
478
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

What is the recommended way to have common options when using verbs for commands?
For example a log option that should be available for all commands.

My solution at the moment.
Calling ConfigureLogging(...) in all the command functions is easy to forget when adding a new command.
```csharp
public class CommonOptions
{
[Option("log")]
public int LogLevel { get; set; }
}

[Verb("config")]
class ConfigurationOptions : CommonOptions
{
[Option("reboot")]
public bool Reboot { get; set; }
}

[Verb("update")]
class UpdateOptions : CommonOptions
{
[Option]
public int Number { get; set; }
}

static void Main(string[] args)
{
Parser.Default.ParseArguments(args)
.WithParsed(LoadConfiguration)
.WithParsed(UpdateSetting);
}

private static void ConfigureLogging(CommonOptions options)
{
Log.Logger = CreateLogger(options.LogLevel);
}

private static void LoadConfiguration(ConfigurationOptions options)
{
ConfigureLogging(options);
// Load stuff
}

private static void UpdateSetting(UpdateOptions options)
{
ConfigureLogging(options);
// Update stuff
}
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.