commandlineparser / commandlineparser/commandline

Common options when using verbs

Abierto
#624 3 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
C#
Estrellas
4.8k
Forks
478
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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
}
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.