Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,16 @@ public void WithShortName_Returns_Self()

underTest.WithShortName("alpha").Should().BeSameAs(underTest);
}

[Test]
public void WithDescription_Should_Set_Description()
{
string value = "testDescription";
var parameter = new OptionalParameter();
var underTest = new ParameterBindingConfigurer<string>(() => parameter, _ => { });

underTest.WithDefaultDescription(value);

parameter.Description.Should().Be(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
using FluentValidation.TestHelper;

using ModernRonin.FluentArgumentParser.Definition;
using ModernRonin.FluentArgumentParser.Validation;

using NUnit.Framework;

using System;

namespace ModernRonin.FluentArgumentParser.Tests.Validation;

[TestFixture]
public class OptionalParameterValidatorTests
{
[Test]
public void HasDefaultBeenSet_must_be_true() =>
new OptionalParameterValidator().TestValidate(new OptionalParameter { Type = typeof(string) })
new OptionalParameterValidator()
.TestValidate(new OptionalParameter { Type = typeof(string) })
.ShouldHaveValidationErrorFor(p => p.Default);

[Test]
public void Description_Should_Have_Error_When_Description_Empty_For_Reference_Type() =>
new OptionalParameterValidator()
.TestValidate(new OptionalParameter { Type = typeof(string) })
.ShouldHaveValidationErrorFor(p => p.Description);

[Test]
public void Description_Should_Not_Have_Error_When_Description_Empty_For_Value_Type() =>
new OptionalParameterValidator()
.TestValidate(new OptionalParameter { Type = typeof(int) })
.ShouldHaveValidationErrorFor(p => p.Description);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be .ShouldNotHaveValidationErrors() or some such in this test-case?

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class OptionalParameter : AnIndexableParameter
{
object _default;
public bool HasDefaultBeenSet { get; private set; }
public string Description { get; set; }

public object Default
{
Expand Down
19 changes: 14 additions & 5 deletions ModernRonin.FluentArgumentParser/Help/HelpMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ModernRonin.FluentArgumentParser.Definition;
using ModernRonin.FluentArgumentParser.Extensibility;
using ModernRonin.FluentArgumentParser.Parsing;

using MoreLinq.Extensions;

namespace ModernRonin.FluentArgumentParser.Help;
Expand Down Expand Up @@ -184,7 +186,14 @@ Row indexableToRow(AnIndexableParameter indexable)
Row optionalToRow(OptionalParameter opt)
{
var result = indexableToRow(opt);
result.RightLines.Add($"default: {opt.Default}");
if (opt.Default == null)
{
result.RightLines.Add(opt.Description);
}
else
{
result.RightLines.Add($"Default: {opt.Default}");
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be simplified to result.RightLines.Add(opt.Default is null ? opt.Description : $"Default: {opt.Default}"

return result;
}

Expand Down Expand Up @@ -219,10 +228,10 @@ void VerbList(string preamble, IEnumerable<Verb> verbs, Action addDescription)
addDescription();
Line("Available commands:");
Table(materialized.Select(v => new Row
{
LeftText = v.Name,
RightLines = { v.HelpText }
})
{
LeftText = v.Name,
RightLines = { v.HelpText }
})
.ToArray());
LineFeed();
Line("use help <command> for more detailed help on a specific command");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,23 @@ public ParameterBindingConfigurer<TProperty> WithHelp(string helpText)
Parameter.HelpText = helpText;
return this;
}

/// <summary>
/// <para>
/// Set default description for optional parameter.
/// </para>
/// Default description is required for reference types with a default value of null
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
public ParameterBindingConfigurer<TProperty> WithDefaultDescription(string text)
{
if (!(Parameter is OptionalParameter optional))
{
throw new InvalidOperationException(
$"Default are only settable for optional parameters - maybe you forgot a call to {nameof(MakeOptional)}?");
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to have a test for the throw branch, too


optional.Description = text;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentValidation;

using ModernRonin.FluentArgumentParser.Definition;

namespace ModernRonin.FluentArgumentParser.Validation;
Expand All @@ -9,5 +10,14 @@ public OptionalParameterValidator()
{
Include(new IndexableParameterValidator());
RuleFor(p => p.Default).Must((p, _) => p.HasDefaultBeenSet);
RuleFor(p => p.Description).Must((p, _) =>
{
if (p.Default is null && string.IsNullOrEmpty(p.Description))
{
return false;
}

return true;
}).WithMessage("Reference types (with a null default) requires a description to be set.");
}
}