commandlineparser / commandlineparser/commandline
Common options when using verbs
- 主要言語
- C#
- スター
- 4.8k
- フォーク
- 478
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
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
}
```
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
評価
この issue はまだ評価されていません。