Skip to content

Commit

Permalink
Added Inherited feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Fazlani committed Jan 3, 2018
1 parent e435ebb commit e9add62
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
38 changes: 38 additions & 0 deletions CommandDotNet.Tests/InheritedTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CommandDotNet.Attributes;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace CommandDotNet.Tests
{
public class InheritedTests : TestBase
{
public InheritedTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

[Fact]
public void CanInheriOptions()
{
AppRunner<InheritedApp> appRunner = new AppRunner<InheritedApp>();
appRunner.Run("--value", "10", "GetValue").Should().Be(10, "10 is passed as root option");
appRunner.Run("GetValue", "--value", "10").Should().Be(10, "10 is passed as command option");
appRunner.Run("GetValue").Should().Be(0, "no option is passed");
}
}

public class InheritedApp
{
private readonly int _value;

public InheritedApp([Option(Inherited = true)]int value)
{
_value = value;
}

public int GetValue()
{
return _value;
}
}
}
2 changes: 2 additions & 0 deletions CommandDotNet/Attributes/OptionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public class OptionAttribute : ParameterBaseAttribute
public string LongName { get; set; }

public BooleanMode BooleanMode { get; set; }

public bool Inherited { get; set; }
}
}
12 changes: 8 additions & 4 deletions CommandDotNet/CommandCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public void CreateCommands()
{
option.SetValue(command.Option(option.Template,
option.EffectiveDescription,
option.CommandOptionType));
option.CommandOptionType,
option.Inherited));
}

foreach (var parameter in argumentValues.OfType<CommandParameterInfo>())
Expand Down Expand Up @@ -122,14 +123,17 @@ private List<ArgumentInfo> GetOptionValuesForConstructor()
switch (argumentInfo)
{
case CommandParameterInfo parameterInfo:
parameterInfo.SetValue(_app.Argument(parameterInfo.Name,
parameterInfo.EffectiveDescription, parameterInfo.IsMultipleType));
parameterInfo.SetValue(_app.Argument(
parameterInfo.Name,
parameterInfo.EffectiveDescription,
parameterInfo.IsMultipleType));
break;
case CommandOptionInfo optionInfo:
optionInfo.SetValue(_app.Option(
optionInfo.Template,
optionInfo.EffectiveDescription,
optionInfo.CommandOptionType));
optionInfo.CommandOptionType,
optionInfo.Inherited));
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions CommandDotNet/Models/CommandOptionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public CommandOptionInfo(ParameterInfo parameterInfo, AppSettings settings) : ba
public string LongName { get; }

public string ShortName { get; }

public bool Inherited => ParameterInfo.GetCustomAttribute<OptionAttribute>()?.Inherited ?? false;

private string GetShortName()
{
Expand Down

0 comments on commit e9add62

Please sign in to comment.