Skip to content

Commit

Permalink
parameter objects now have sufficient information for runner to outpu…
Browse files Browse the repository at this point in the history
…t pretty help texts
  • Loading branch information
mookid8000 committed Sep 3, 2010
1 parent ca64bb7 commit f522de5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/GoCommando.ConsoleApplication/Program.cs
Expand Up @@ -11,6 +11,7 @@ class Program : ICommando
{
[PositionalArgument]
[Description("The path to the file you wish to do stuff to")]
[Example("c:\\temp\\somefile.txt")]
public string Path { get; set; }

[PositionalArgument]
Expand Down
44 changes: 34 additions & 10 deletions src/GoCommando.Tests/Helpers/TestHelper.cs
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using GoCommando.Attributes;
using GoCommando.Helpers;
using NUnit.Framework;
Expand All @@ -18,18 +20,37 @@ public void CanRepresentParametersAndDescriptionsAsExpected()
{
var parameters = helper.GetParameters(new SomeClass());

var firstParameter = parameters[0];
Assert.AreEqual("hello there!", firstParameter.Description);
Assert.AreEqual(1, firstParameter.Position);
AssertPositionalParameter(parameters[0], "hello there!", 1, "FirstPositional", "ex1", "ex2");
AssertPositionalParameter(parameters[1], "hello there!", 2, "SecondPositional", "yet another lengthy example");
AssertNamedParameter(parameters[2], "hello again! there!", "AnotherString", "name", "nm");
}

void AssertPositionalParameter(Parameter parameter, string expectedDescription, int expectedPosition, string expectedPropertyName, params string[] expectedExamples)
{
AssertParameter(expectedDescription, parameter, expectedPropertyName);
Assert.AreEqual(expectedPosition, parameter.Position);
Assert.IsTrue(parameter.ArgumentAttribute is PositionalArgumentAttribute, "Expected PositionalArgumentAttribute, was {0}", parameter.ArgumentAttribute.GetType().Name);
AssertExamples(parameter, expectedExamples);
}

void AssertNamedParameter(Parameter parameter, string expectedDescription, string expectedPropertyName, string expectedArgumentName, string expectedArgumentShorthand, params string[] expectedExamples)
{
AssertParameter(expectedDescription, parameter, expectedPropertyName);
Assert.IsTrue(parameter.ArgumentAttribute is NamedArgumentAttribute, "Expected NamedArgumentAttribute, was {0}", parameter.ArgumentAttribute.GetType().Name);
Assert.AreEqual(expectedArgumentName, ((NamedArgumentAttribute)parameter.ArgumentAttribute).Name);
Assert.AreEqual(expectedArgumentShorthand, ((NamedArgumentAttribute) parameter.ArgumentAttribute).ShortHand);
AssertExamples(parameter, expectedExamples);
}

var secondParameter = parameters[1];
Assert.AreEqual("hello there!", secondParameter.Description);
Assert.AreEqual(2, secondParameter.Position);
void AssertExamples(Parameter parameter, IEnumerable<string> examples)
{
Assert.IsTrue(parameter.Examples.OrderBy(e => e.Text).Select(e => e.Text).SequenceEqual(examples.OrderBy(s => s)));
}

var thirdParameter = parameters[2];
Assert.AreEqual("hello again! there!", thirdParameter.Description);
Assert.AreEqual("name", thirdParameter.Name);
Assert.AreEqual("nm", thirdParameter.Shorthand);
void AssertParameter(string expectedDescription, Parameter para, string expectedPropertyName)
{
Assert.AreEqual(expectedDescription, para.Description);
Assert.AreEqual(expectedPropertyName, para.PropertyInfo.Name);
}

class SomeClass
Expand All @@ -38,10 +59,13 @@ class SomeClass

[PositionalArgument]
[Attributes.Description("hello there!")]
[Example("ex1")]
[Example("ex2")]
public string FirstPositional { get; set; }

[PositionalArgument]
[Attributes.Description("hello there!")]
[Example("yet another lengthy example")]
public string SecondPositional { get; set; }

[NamedArgument("name", "nm")]
Expand Down
4 changes: 2 additions & 2 deletions src/GoCommando/Go.cs
Expand Up @@ -93,7 +93,7 @@ static void ShowHelpText(ICommando commando)
if (thereAreOptionalArguments)
{
Write();
Write("Optional arguments:");
Write("Additional arguments:");
Write();

foreach (var parameter in parameters.Where(p => p.Position == 0))
Expand All @@ -106,7 +106,7 @@ static void ShowHelpText(ICommando commando)
static bool ShouldShowHelpText(string[] strings)
{
return strings.Length == 1
&& new List<string> {"-h", "--h", "/h", "-?", "/?"}.Contains(strings[0].ToLowerInvariant());
&& new List<string> {"-h", "--h", "/h", "-?", "/?", "?"}.Contains(strings[0].ToLowerInvariant());
}

static ICommando CreateInstance<TCommando>()
Expand Down
14 changes: 5 additions & 9 deletions src/GoCommando/Helpers/Binder.cs
Expand Up @@ -4,7 +4,6 @@
using System.Reflection;
using GoCommando.Attributes;
using GoCommando.Exceptions;
using GoCommando.Extensions;
using GoCommando.Parameters;

namespace GoCommando.Helpers
Expand All @@ -14,10 +13,12 @@ public class Binder
public void Bind(object targetObjectWithAttributes, IEnumerable<CommandLineParameter> parametersToBind)
{
var context = new BindingContext();
foreach (var property in targetObjectWithAttributes.GetType().GetProperties().Where(ShouldBeBound))
var helper = new Helper();

foreach (var parameter in helper.GetParameters(targetObjectWithAttributes))
{
Bind(targetObjectWithAttributes, property, parametersToBind.ToList(),
property.GetAttributes<ArgumentAttribute>().Single(),
Bind(targetObjectWithAttributes, parameter.PropertyInfo, parametersToBind.ToList(),
parameter.ArgumentAttribute,
context);
}
}
Expand Down Expand Up @@ -83,10 +84,5 @@ CommandoException Ex(string message, params object[] objs)
{
return new CommandoException(message, objs);
}

bool ShouldBeBound(PropertyInfo info)
{
return info.HasAttribute<ArgumentAttribute>();
}
}
}
21 changes: 20 additions & 1 deletion src/GoCommando/Helpers/Helper.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -23,7 +24,7 @@ public List<Parameter> GetParameters(object obj)
.ToList();
}

Parameter CreateParameter(ICustomAttributeProvider propertyInfo, HelperContext context)
Parameter CreateParameter(PropertyInfo propertyInfo, HelperContext context)
{
var attributes = propertyInfo.GetAttributes<DescriptionAttribute>();

Expand All @@ -48,6 +49,14 @@ Parameter CreateParameter(ICustomAttributeProvider propertyInfo, HelperContext c
parameter.Position = context.Position;
}

parameter.PropertyInfo = propertyInfo;
parameter.ArgumentAttribute = argumentAttribute;

foreach(var example in propertyInfo.GetAttributes<ExampleAttribute>())
{
parameter.Examples.Add(example);
}

return parameter;
}

Expand All @@ -59,11 +68,21 @@ bool ShouldBeIncluded(PropertyInfo info)

public class Parameter
{
public Parameter()
{
Examples = new List<ExampleAttribute>();
}

public string Description { get; set; }

public string Name { get; set; }
public string Shorthand { get; set; }

public int Position { get; set; }

public PropertyInfo PropertyInfo { get; set; }
public ArgumentAttribute ArgumentAttribute { get; set; }

public List<ExampleAttribute> Examples { get; set; }
}
}

0 comments on commit f522de5

Please sign in to comment.