commandlineparser / commandlineparser/commandline

Common options when using verbs

Open
#624 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.8k
Forks
478
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.