Skip to content

Commit

Permalink
Applying fix to HelpText (#48)
Browse files Browse the repository at this point in the history
* Applying fix to HelpText

* Update Unosquare.Swan.csproj
  • Loading branch information
geoperez committed Nov 30, 2017
1 parent 48c1a82 commit 387e3f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
28 changes: 12 additions & 16 deletions src/Unosquare.Swan/Components/ArgumentParser.cs
Expand Up @@ -70,11 +70,20 @@ public bool ParseArguments<T>(IEnumerable<string> args, T instance)

if (properties.Any(x => x.GetCustomAttributes(typeof(VerbOptionAttribute), false).Any()))
{
var selectedVerb = properties.FirstOrDefault(x =>
x.GetCustomAttribute<VerbOptionAttribute>().Name.Equals(args.ToArray()[0]));
var selectedVerb = !args.Any()
? null
: properties.FirstOrDefault(x =>
x.GetCustomAttribute<VerbOptionAttribute>().Name.Equals(args.First()));

if (selectedVerb == null)
{
WriteVerbsUsage(properties);
"No verb was specified".WriteLine(ConsoleColor.Red);
"Valid verbs:".WriteLine(ConsoleColor.Cyan);
properties.Select(x => x.GetCustomAttribute<VerbOptionAttribute>()).Where(x => x != null)
.Select(x => $" {x.Name}\t\t{x.HelpText}")
.ToList()
.ForEach(x => x.WriteLine(ConsoleColor.Cyan));

return false;
}

Expand Down Expand Up @@ -233,19 +242,6 @@ private List<string> PopulateInstance<T>(IEnumerable<string> args, T instance, P
private static IEnumerable<PropertyInfo> GetTypeProperties(Type type)
=> Runtime.PropertyTypeCache.Value.Retrieve(type, PropertyTypeCache.GetAllPublicPropertiesFunc(type));

private static void WriteVerbsUsage( IEnumerable<PropertyInfo> verbs)
{
var options = verbs.Select(p => p.GetCustomAttribute<VerbOptionAttribute>())
.Where(x => x != null);

foreach (var option in options)
{
string.Empty.WriteLine();

$" {option.Name}\t\t{option.HelpText}".WriteLine(ConsoleColor.Cyan);
}
}

private static void WriteUsage(IEnumerable<PropertyInfo> properties)
{
var options = properties.Select(p => p.GetCustomAttribute<ArgumentOptionAttribute>())
Expand Down
2 changes: 1 addition & 1 deletion src/Unosquare.Swan/Unosquare.Swan.csproj
Expand Up @@ -10,7 +10,7 @@
<PackageId>Unosquare.Swan</PackageId>
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
<DebugType>Full</DebugType>
<Version>0.21.0</Version>
<Version>0.21.1</Version>
<Authors>Unosquare</Authors>
<PackageIconUrl>https://github.com/unosquare/swan/raw/master/swan-logo-32.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/unosquare/swan</PackageProjectUrl>
Expand Down
48 changes: 29 additions & 19 deletions test/Unosquare.Swan.Test/ArgumentParserTest.cs
Expand Up @@ -15,7 +15,7 @@ public void BasicArguments_ReturnsEquals()
var options = new OptionMock();
Assert.IsFalse(options.Verbose);

var dumpArgs = new[] {"-n", "babu", "--verbose"};
var dumpArgs = new[] { "-n", "babu", "--verbose" };
var result = Runtime.ArgumentParser.ParseArguments(dumpArgs, options);

Assert.IsTrue(result);
Expand All @@ -28,7 +28,7 @@ public void BasicArguments_ReturnsEquals()
public void InvalidDataConversion_ReturnsFalse()
{
var options = new OptionIntRequiredMock();
var result = Runtime.ArgumentParser.ParseArguments(new[] {"-n", "babu"}, options);
var result = Runtime.ArgumentParser.ParseArguments(new[] { "-n", "babu" }, options);

Assert.IsFalse(result);
}
Expand All @@ -39,15 +39,15 @@ public void ObjectCollection_ThrowsInvalidOperationException()
Assert.Throws<InvalidOperationException>(() =>
{
var options = new OptionObjectCollectionMock();
Runtime.ArgumentParser.ParseArguments(new[] {"--options", "1", null, "0"}, options);
Runtime.ArgumentParser.ParseArguments(new[] { "--options", "1", null, "0" }, options);
});
}

[Test]
public void ObjectArray_ReturnsTrue()
{
var options = new OptionObjectArrayMock();
var result = Runtime.ArgumentParser.ParseArguments(new[] {"--options", "1,null,0"}, options);
var result = Runtime.ArgumentParser.ParseArguments(new[] { "--options", "1,null,0" }, options);

Assert.IsTrue(result);
Assert.AreEqual(3, options.Options.Length);
Expand All @@ -57,8 +57,8 @@ public void ObjectArray_ReturnsTrue()
public void CaseSensitiveArguments_ReturnsFalse()
{
var options = new OptionMock();
var dumpArgs = new[] {"-N", "babu", "-V"};
var parser = new ArgumentParser(new ArgumentParserSettings {CaseSensitive = true});
var dumpArgs = new[] { "-N", "babu", "-V" };
var parser = new ArgumentParser(new ArgumentParserSettings { CaseSensitive = true });
var result = parser.ParseArguments(dumpArgs, options);

Assert.IsFalse(result, "Parsing is not valid");
Expand All @@ -68,8 +68,8 @@ public void CaseSensitiveArguments_ReturnsFalse()
public void UnknwownArguments_ReturnsFalse()
{
var options = new OptionMock();
var dumpArgs = new[] {"-XOR"};
var parser = new ArgumentParser(new ArgumentParserSettings {IgnoreUnknownArguments = false});
var dumpArgs = new[] { "-XOR" };
var parser = new ArgumentParser(new ArgumentParserSettings { IgnoreUnknownArguments = false });
var result = parser.ParseArguments(dumpArgs, options);

Assert.IsFalse(result, "Argument is unknown");
Expand All @@ -83,7 +83,7 @@ public void EnumArguments_ReturnsTrue()

const ConsoleColor newColor = ConsoleColor.White;

var dumpArgs = new[] {"-n", "babu", "--color", newColor.ToString().ToLowerInvariant()};
var dumpArgs = new[] { "-n", "babu", "--color", newColor.ToString().ToLowerInvariant() };
var result = Runtime.ArgumentParser.ParseArguments(dumpArgs, options);

Assert.IsTrue(result);
Expand All @@ -96,7 +96,7 @@ public void ListArguments_ReturnsTrue()
var options = new OptionMock();
Assert.IsNull(options.Options);

var dumpArgs = new[] {"-n", "babu", "--options", string.Join(",", DefaultStringList)};
var dumpArgs = new[] { "-n", "babu", "--options", string.Join(",", DefaultStringList) };
var result = Runtime.ArgumentParser.ParseArguments(dumpArgs, options);

Assert.IsTrue(result);
Expand Down Expand Up @@ -125,7 +125,7 @@ public void TypeInvalid_ThrowsInvalidOperationException()
[Test]
public void PropertiesEmpty_ThrowsInvalidOperationException()
{
var dumpArgs = new[] {"--options", string.Join(",", DefaultStringList)};
var dumpArgs = new[] { "--options", string.Join(",", DefaultStringList) };

Assert.Throws<InvalidOperationException>(() =>
Runtime.ArgumentParser.ParseArguments(dumpArgs, new OptionMockEmpty()));
Expand All @@ -142,36 +142,46 @@ public void InstanceNull_ThrowsArgumentNullException()
[TestFixture]
public class ParseVerbs : TestFixtureBase
{
[Test]
public void EmptyArray_ReturnsFalse()
{
var verbOptions = new CliVerbs();
var arguments = new string[0];
var expected = Runtime.ArgumentParser.ParseArguments(arguments, verbOptions);

Assert.IsFalse(expected);
}

[Test]
public void BasicVerbParsing_ReturnsTrue()
{
var verbOptions = new CliVerbs();
var arguments = new string[] { "monitor", "-v" };
var arguments = new[] { "monitor", "-v" };
var expected = Runtime.ArgumentParser.ParseArguments(arguments, verbOptions);

Assert.AreEqual(expected, true);
Assert.IsTrue(expected);
}

[Test]
public void BasicVerbParsing_InstantiatesSelectedVerbOptionProperty()
{
var verbOptions = new CliVerbs();
var arguments = new string[] { "verb", "-u", "user", "--host", "129.168.1.1", "-p", "5556" };
var arguments = new[] { "monitor", "-v" };
var expected = Runtime.ArgumentParser.ParseArguments(arguments, verbOptions);

Assert.AreEqual(expected, true);
Assert.IsNull(verbOptions.MonitorVerboptions);
Assert.IsNotNull(verbOptions.PushVerbOptions);
Assert.IsTrue(expected);
Assert.IsNotNull(verbOptions.MonitorVerboptions);
Assert.IsNull(verbOptions.PushVerbOptions);
}

[Test]
public void NoValidVerbOptionSelected_ReturnsFalse()
{
var verbOptions = new CliVerbs();
var arguments = new string[] { "option", "-v" };
var arguments = new[] { "option", "-v" };
var expected = Runtime.ArgumentParser.ParseArguments(arguments, verbOptions);

Assert.AreEqual(expected, false);
Assert.IsFalse(expected);
Assert.IsNull(verbOptions.MonitorVerboptions);
Assert.IsNull(verbOptions.PushVerbOptions);
}
Expand Down

0 comments on commit 387e3f5

Please sign in to comment.