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
}
```
貢獻指南
這個儲存庫沒有索引到貢獻指南
研究方向
從 Parser.Default.ParseArguments 以及 issue 中 ConfigurationOptions、UpdateOptions 和 CommonOptions 的範例開始。確定是否支援跨 verb 類型的通用選項,或者是否需要提出設計方案,並將 done 定義為一種經過同意和測試的方法,避免在每個命令處理常式中重複 ConfigureLogging。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- csharp
- 領域
- cli
- Issue 類型
- 功能
- 難度
- 5/5
- 預估耗時
- 一週以上
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 25/100