Skip to content

Commit

Permalink
fixed some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mookid8000 committed Oct 8, 2015
1 parent eca545f commit 2b47f9f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
1 change: 1 addition & 0 deletions GoCommando.Tests/GoCommando.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="TestArgParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestCommand.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
36 changes: 36 additions & 0 deletions GoCommando.Tests/TestCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using GoCommando.Internals;
using NUnit.Framework;

namespace GoCommando.Tests
{
[TestFixture]
public class TestCommand
{
[TestCase("-switch:value2")]
[TestCase("-switch=value2")]
[TestCase(@"-switch""value2""")]
public void CanCorrectlyHandleDifferentAlternativeSwitchFormatsFoundInOneSingleTokenOnly(string switchText)
{
var settings = new Settings();
var invoker = new CommandInvoker("bimse", settings, new Bimse());
var arguments = Go.Parse(new[] {switchText}, settings);

invoker.Invoke(arguments.Switches);

var bimseInstance = (Bimse)invoker.CommandInstance;

Assert.That(bimseInstance.Switch, Is.EqualTo("value2"));
}

[Command("bimse")]
class Bimse : ICommand
{
[Parameter("switch")]
public string Switch { get; set; }

public void Run()
{
}
}
}
}
10 changes: 7 additions & 3 deletions GoCommando/Go.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void Run()
Environment.ExitCode = -1;
Console.WriteLine(friendlyException.Message);
Console.WriteLine();
Console.WriteLine("Invoke with -? or -help to get help.");
Console.WriteLine("Invoke with -help <command> to get help for each command.");
Console.WriteLine();
Console.WriteLine("Exit code: -1");
Console.WriteLine();
Expand Down Expand Up @@ -128,9 +128,13 @@ static void InnerRun()

if (commandToRun == null)
{
var availableCommands = string.Join(Environment.NewLine, commandTypes.Select(c => " " + c.Command));
var errorText = !string.IsNullOrWhiteSpace(arguments.Command)
? $"Could not find command '{arguments.Command}'"
: "Please invoke with a command";

throw new GoCommandoException($@"Could not find command '{arguments.Command}' - the following commands are available:
var availableCommands = string.Join(Environment.NewLine, commandTypes.Select(c => $" {c.Command} - {c.Description}"));

throw new GoCommandoException($@"{errorText} - the following commands are available:
{availableCommands}");
}
Expand Down
25 changes: 17 additions & 8 deletions GoCommando/Internals/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@ namespace GoCommando.Internals
class CommandInvoker
{
readonly Settings _settings;
readonly ICommand _commandInstance;

public CommandInvoker(string command, Type type, Settings settings)
: this(command, settings, CreateInstance(type))
{
_settings = settings;
}

if (!typeof(ICommand).IsAssignableFrom(type))
{
throw new ApplicationException($"Command tyep {type} does not implement {typeof(ICommand)} as it should!");
}
public CommandInvoker(string command, Settings settings, ICommand commandInstance)
{
_settings = settings;
_commandInstance = commandInstance;

Command = command;
Type = type;
Parameters = GetParameters(Type);
}

static ICommand CreateInstance(Type type)
{
return (ICommand)Activator.CreateInstance(type);
}

IEnumerable<Parameter> GetParameters(Type type)
{
return type
Expand Down Expand Up @@ -53,16 +59,19 @@ static TAttribute GetSingleAttributeOrNull<TAttribute>(PropertyInfo p) where TAt
}

public string Command { get; }
public Type Type { get; }

public Type Type => _commandInstance.GetType();

public IEnumerable<Parameter> Parameters { get; }

public string Description => Type.GetCustomAttribute<DescriptionAttribute>()?.DescriptionText ??
"(no help text for this command)";

public ICommand CommandInstance => _commandInstance;

public void Invoke(IEnumerable<Switch> switches)
{
var commandInstance = (ICommand)Activator.CreateInstance(Type);
var commandInstance = _commandInstance;

var requiredParametersMissing = Parameters
.Where(p => !p.Optional && !p.HasDefaultValue && !switches.Any(s => s.Key == p.Name))
Expand Down
28 changes: 26 additions & 2 deletions GoCommando/Internals/Switch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace GoCommando.Internals
// ReSharper disable LoopCanBeConvertedToQuery
namespace GoCommando.Internals
{
class Switch
{
static readonly char[] AcceptedQuoteCharacters = { '"', '\'' };

public static Switch KeyValue(string key, string value)
{
return new Switch(key, value);
Expand All @@ -15,7 +18,28 @@ public static Switch Flag(string key)
Switch(string key, string value)
{
Key = key;
Value = value;
Value = Unquote(value);
}

static string Unquote(string value)
{
if (value == null) return null;

// can't be quoted
if (value.Length < 2) return value;

foreach (var quoteChar in AcceptedQuoteCharacters)
{
var quote = quoteChar.ToString();

if (value.StartsWith(quote)
&& value.EndsWith(quote))
{
return value.Substring(1, value.Length - 2);
}
}

return value;
}

public string Key { get; }
Expand Down
1 change: 1 addition & 0 deletions TestApp/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace TestApp.Commands
{
[Command("new")]
[Description("Does something new")]
public class NewCommand : ICommand
{
[Parameter("qid")]
Expand Down

0 comments on commit 2b47f9f

Please sign in to comment.