Skip to content

Commit

Permalink
GetOption, GetVersionText methods and unittest updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
orakist committed Jan 30, 2024
1 parent add494a commit 944a8b6
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Oaksoft.ArgumentParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<AssemblyName>Oaksoft.ArgumentParser</AssemblyName>
<RootNamespace>Oaksoft.ArgumentParser</RootNamespace>
<Nullable>enable</Nullable>
<VersionPrefix>1.5.0</VersionPrefix>
<VersionPrefix>1.5.1</VersionPrefix>
<Version>$(VersionPrefix)</Version>
<Authors>orakist</Authors>
<Title>Oaksoft.ArgumentParser</Title>
Expand Down
7 changes: 3 additions & 4 deletions src/Oaksoft.ArgumentParser/Options/BaseOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ internal abstract class BaseOption : IBaseOption

public (int Min, int Max) ValueArity { get; protected set; }

public bool IsValid { get; protected set; }

public bool IsParsed => IsValid && OptionCount + ValueCount > 0;
public bool IsParsed => _isValid && OptionCount + ValueCount > 0;

public bool IsHidden { get; private set; }

Expand All @@ -33,6 +31,7 @@ internal abstract class BaseOption : IBaseOption

public PropertyInfo KeyProperty { get; private set; } = default!;

protected bool _isValid;
protected IArgumentParser? _parser;

public void SetKeyProperty(PropertyInfo property)
Expand Down Expand Up @@ -139,7 +138,7 @@ public virtual void Validate()

public virtual void Clear()
{
IsValid = false;
_isValid = false;
}

protected void ParserInitializedGuard()
Expand Down
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Options/CounterOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public override void Validate()
{
base.Validate();

IsValid = true;
_isValid = true;
}

public override void ApplyOptionResult(object appOptions, PropertyInfo keyProperty)
Expand Down
2 changes: 0 additions & 2 deletions src/Oaksoft.ArgumentParser/Options/IBaseOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public interface IBaseOption

int ValueCount { get; }

bool IsValid { get; }

bool IsParsed { get; }

bool IsHidden { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Options/ScalarNamedOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public override void Validate()
{
base.Validate();

IsValid = true;
_isValid = true;
}

public override void ApplyOptionResult(object appOptions, PropertyInfo keyProperty)
Expand Down
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Options/ScalarValueOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public override void Validate()
{
base.Validate();

IsValid = true;
_isValid = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public override void Validate()
{
base.Validate();

IsValid = true;
_isValid = true;
}

public override void Clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public override void Validate()
{
base.Validate();

IsValid = true;
_isValid = true;
}
}
13 changes: 12 additions & 1 deletion src/Oaksoft.ArgumentParser/Parser/BaseArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ internal abstract class BaseArgumentParser : IArgumentParser
_reader = Console.In;
}

public string GetVersionText()
{
return AssemblyHelper.GetAssemblyVersion() ?? string.Empty;
}

public List<IBaseOption> GetOptions()
{
return _baseOptions
Expand All @@ -68,6 +73,11 @@ public List<IBaseOption> GetOptions()
.ToList();
}

public IBaseOption? GetOption(string nameOrAlias)
{
return GetOptionByName(nameOrAlias) ?? GetOptionByAlias(nameOrAlias);
}

public IBaseOption? GetOptionByName(string name)
{
return _baseOptions.FirstOrDefault(
Expand All @@ -80,7 +90,8 @@ public List<IBaseOption> GetOptions()
var flag = CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

return _baseOptions.OfType<INamedOption>()
.FirstOrDefault(o => o.Aliases.Any(a => a.Equals(alias, flag)));
.FirstOrDefault(o => o.Aliases.Any(a => a.Equals(alias, flag)) ||
((BaseOption)o).GetAliases().Any(a => a.Equals(alias, flag)));
}

public void SetTextReader(TextReader reader)
Expand Down
4 changes: 4 additions & 0 deletions src/Oaksoft.ArgumentParser/Parser/IArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public interface IArgumentParser

string GetErrorText(bool? enableColoring = default);

string GetVersionText();

List<IBaseOption> GetOptions();

IBaseOption? GetOption(string nameOrAlias);

IBaseOption? GetOptionByName(string name);

INamedOption? GetOptionByAlias(string alias);
Expand Down
1 change: 1 addition & 0 deletions test/Oaksoft.ArgumentParser.Console/CalculatorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class CalculatorOptions
public IEnumerable<double>? Numbers { get; set; }

public IEnumerable<int>? Integers { get; set; }

public IEnumerable<decimal>? Decimals { get; set; }

public OperatorType? Operator { get; set; }
Expand Down
26 changes: 8 additions & 18 deletions test/Oaksoft.ArgumentParser.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ private static void Main(string[] args)
{
try
{
var parser = CommandLine.CreateParser<CalculatorOptions>()
CommandLine.CreateParser<CalculatorOptions>()
.ConfigureOptions()
.Build();

parser.Run(EvaluateOptions, args);
.Run(EvaluateOptions, args);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -75,23 +73,15 @@ private static void EvaluateOptions(IArgumentParser<CalculatorOptions> parser, C
Console.WriteLine();
}

private static IArgumentParserBuilder<CalculatorOptions> ConfigureOptions(this IArgumentParserBuilder<CalculatorOptions> builder)
private static IArgumentParser<CalculatorOptions> ConfigureOptions(this IArgumentParserBuilder<CalculatorOptions> builder)
{
return builder
.AddNamedOption(p => p.LeftOperand,
o => o.WithDescription("Left operand of the operation."))

.AddNamedOption(p => p.RightOperand,
o => o.WithDescription("Right operand of the operation."))

.AddNamedOption(p => p.Numbers,
o => o.WithDescription("Defines numbers for the operation."))

.AddValueOption(p => p.Integers)
.AddValueOption(p => p.Decimals)

.AddNamedOption(o => o.Operator,
o => o.WithDescription("Sets the operator type."),
mandatoryOption: true);
.AddNamedOption(p => p.LeftOperand, o => o.WithDescription("Left operand of the operation."))
.AddNamedOption(p => p.RightOperand, o => o.WithDescription("Right operand of the operation."))
.AddNamedOption(p => p.Numbers, o => o.WithDescription("Defines numbers for the operation."))
.AddNamedOption(o => o.Operator, o => o.WithDescription("Sets the operator type."), mandatoryOption: true)
.Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void ShouldBuild_WhenDescriptionUsed()
// Arrange
const string description = "Cats have an adorable face with a tiny nose.";
var sut = CommandLine.CreateParser<IntAppOptions>()
.ConfigureSettings(s => s.Description = "Test description!")
.AddNamedOption(s => s.NullValue, o => o.WithDescription(description))
.AddCounterOption(s => s.NullValueCount, o => o.WithDescription(description))
.AddNamedOption(s => s.Values, o => o.WithDescription(description))
Expand All @@ -23,10 +24,14 @@ public void ShouldBuild_WhenDescriptionUsed()

// Act
var parser = sut.Build();
var text = parser.GetHelpText(false);
var header = parser.GetHeaderText();

// Assert
parser.GetOptions().Count.ShouldBe(6);
var text = parser.GetHelpText(false);

text.ShouldContain(parser.Settings.Description!);
header.ShouldContain(parser.Settings.Description!);

var option = parser.GetOptionByName(nameof(IntAppOptions.NullValue));
option.ShouldNotBeNull();
Expand Down Expand Up @@ -77,17 +82,17 @@ public void ShouldBuild_WithDefaultDescription()
parser.GetOptions().Count.ShouldBe(5);
var text = parser.GetHelpText(false);

var option = parser.GetOptionByName(nameof(IntAppOptions.Value));
var option = parser.GetOption(nameof(IntAppOptions.Value));
option.ShouldNotBeNull();
option.Description.ShouldBe($"Performs '{option.Name}' option.");
text.ShouldContain(option.Description!);

option = parser.GetOptionByName(nameof(IntAppOptions.NullValue));
option = parser.GetOption(nameof(IntAppOptions.NullValue));
option.ShouldNotBeNull();
option.Description.ShouldBe($"Performs '{option.Name}' option.");
text.ShouldContain(option.Description!);

option = parser.GetOptionByName(nameof(IntAppOptions.Values));
option = parser.GetOption(nameof(IntAppOptions.Values));
option.ShouldNotBeNull();
option.Description.ShouldBe($"Performs '{option.Name}' option.");
text.ShouldContain(option.Description!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void ShouldParseScalar_WhenAliasArgumentsValid_WithMixedDelimiter(params
// Assert
sut.IsValid.ShouldBeTrue();

var option = sut.GetOptionByAlias("-v");
var option = sut.GetOption("-v");
var valueOption = option as IValueOption;
valueOption.ShouldNotBeNull();
if (valueOption.IsParsed)
Expand All @@ -305,7 +305,7 @@ public void ShouldParseScalar_WhenAliasArgumentsValid_WithMixedDelimiter(params
valueOption.InputValues.ShouldContain("10");
}

option = sut.GetOptionByAlias("-c");
option = sut.GetOption("c");
valueOption = option as IValueOption;
valueOption.ShouldNotBeNull();
if (valueOption.IsParsed)
Expand Down

0 comments on commit 944a8b6

Please sign in to comment.