Skip to content

Commit

Permalink
Add more overloads to simplify defining options
Browse files Browse the repository at this point in the history
  • Loading branch information
nhedlund committed Jul 26, 2023
1 parent ba9abe0 commit 24dc0cf
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/NiceCli.Tests/Scenarios/GlobalOptionArgsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static CliApp CreateCliApp(params string[] args)
.DefaultCommand<MyDefaultRunCommand>("Runs the service")
.Command<MyGlobalOptionsCommand>("Tests global options constructor dependency")
.GlobalOptions<MyGlobalOptions>(c => c
.Option(o => o.Number, "App number example", "value", 'n'));
.Option(o => o.Number, "App number example", 'n'));
}

private static CliApp CreateCliAppWithConfigure(Action<MyGlobalOptions> configure, params string[] args)
Expand Down
112 changes: 102 additions & 10 deletions src/NiceCli/CliGlobalOptionsDefinitionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace NiceCli;

public static class CliGlobalOptionsDefinitionExtensions
{
private const string DefaultValue = "value";

public static CliGlobalOptions<TGlobalOptions> Flag<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, bool>> property,
Expand All @@ -27,7 +29,16 @@ public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, string>> property,
string description,
string parameter = "value",
char shortName) where TGlobalOptions : class, new()
{
return app.Option(property, description, DefaultValue, value => value, shortName);
}

public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, string>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.Option(property, description, parameter, value => value, shortName);
Expand All @@ -37,7 +48,16 @@ public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, string>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, DefaultValue, value => value, shortName);
}

public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, string>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, parameter, value => value, shortName);
Expand All @@ -47,7 +67,16 @@ public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, long>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.Option(property, description, DefaultValue, CliValueConversion.ToLong, shortName);
}

public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, long>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.Option(property, description, parameter, CliValueConversion.ToLong, shortName);
Expand All @@ -57,7 +86,16 @@ public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, long>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, DefaultValue, CliValueConversion.ToLong, shortName);
}

public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, long>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, parameter, CliValueConversion.ToLong, shortName);
Expand All @@ -67,7 +105,16 @@ public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, double>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.Option(property, description, DefaultValue, CliValueConversion.ToDouble, shortName);
}

public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, double>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.Option(property, description, parameter, CliValueConversion.ToDouble, shortName);
Expand All @@ -77,7 +124,16 @@ public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, double>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, DefaultValue, CliValueConversion.ToDouble, shortName);
}

public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, double>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, parameter, CliValueConversion.ToDouble, shortName);
Expand All @@ -87,7 +143,16 @@ public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, decimal>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.Option(property, description, DefaultValue, CliValueConversion.ToDecimal, shortName);
}

public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, decimal>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.Option(property, description, parameter, CliValueConversion.ToDecimal, shortName);
Expand All @@ -97,7 +162,16 @@ public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, decimal>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, DefaultValue, CliValueConversion.ToDecimal, shortName);
}

public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, decimal>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, parameter, CliValueConversion.ToDecimal, shortName);
Expand All @@ -107,7 +181,16 @@ public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, DateTime>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.Option(property, description, DefaultValue, CliValueConversion.ToDateTime, shortName);
}

public static CliGlobalOptions<TGlobalOptions> Option<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, DateTime>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.Option(property, description, parameter, CliValueConversion.ToDateTime, shortName);
Expand All @@ -117,7 +200,16 @@ public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, DateTime>> property,
string description,
string parameter,
char shortName) where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, DefaultValue, CliValueConversion.ToDateTime, shortName);
}

public static CliGlobalOptions<TGlobalOptions> HiddenOption<TGlobalOptions>(
this CliGlobalOptions<TGlobalOptions> app,
Expression<Func<TGlobalOptions, DateTime>> property,
string description,
string parameter = DefaultValue,
char shortName = ' ') where TGlobalOptions : class, new()
{
return app.HiddenOption(property, description, parameter, CliValueConversion.ToDateTime, shortName);
Expand Down

0 comments on commit 24dc0cf

Please sign in to comment.