Skip to content

Instantly share code, notes, and snippets.

@dgg
Last active February 16, 2017 14:38
Show Gist options
  • Save dgg/1d54e3248643a2ba275cd4e549e299bd to your computer and use it in GitHub Desktop.
Save dgg/1d54e3248643a2ba275cd4e549e299bd to your computer and use it in GitHub Desktop.
not-last-console-GoCommando
internal class CustomFactory : ICommandFactory
{
public ICommand Create(Type commandType)
{
// do some extremelly poor-man's service locations
if (commandType == typeof(Something))
{
return new Something(Console.Out);
}
if (commandType == typeof(SomethingElse))
{
return new SomethingElse(Console.Out);
}
throw new ArgumentException($"Unknown command type: {commandType}");
}
public void Release(ICommand command)
{
// we could gently free up resources from commands here
}
}
*NotLastConsole_GoCommando*> .\doer.exe something-else -l asd -extra value
The following switches do not have a corresponding parameter:
-extra = value
Invoke with -help <command> to get help for each command.
Exit code: -1
*NotLastConsole_GoCommando*> .\doer.exe -help something-else
Do something else
Type
doer.exe something-else <args>
where <args> can consist of the following parameters:
-locations / -l
places where the something else was done
Examples:
-locations singleLocation
-locations multiple,comma-separated-locations
-awesome / -a (flag/optional)
include if the something else was awesome
*NotLastConsole_GoCommando*> .\doer.exe
Please invoke with a command - the following commands are available:
something - Do something
something-else - Do something else
Invoke with -help <command> to get help for each command.
Exit code: -1
[Command("something")]
[Description("Do something")]
public class Something : ICommand
{
private readonly ADoerOfSomething _command;
public Something(TextWriter writer)
{
_command = new ADoerOfSomething(writer);
}
[Parameter("location", "l")]
[Description("place where the something was done")]
public string Location { get; set; }
[Parameter("times", "t", optional: true, defaultValue: "1")]
[Description("number of times something was done")]
public int Times { get; set; }
[Parameter("awesome", "a", optional: true)]
[Description("include if the something was awesome")]
public bool Awesome { get; set; }
public void Run()
{
var options = new OptionsForSomething
{
Location = Location,
Awesome = Awesome,
Times = Times
};
_command.Do(options);
}
}
[Command("something-else")]
[Description("Do something else")]
public class SomethingElse : ICommand
{
private readonly ADoerOfSomethingElse _command;
public SomethingElse(TextWriter writer)
{
_command = new ADoerOfSomethingElse(writer);
}
[Parameter("locations", "l")]
[Description("places where the something else was done")]
[Example("singleLocation")]
[Example("multiple,comma-separated-locations")]
public string Locations { get; set; }
[Parameter("awesome", "a", optional: true)]
[Description("include if the something else was awesome")]
public bool Awesome { get; set; }
private static readonly char[] _comma = {','};
public void Run()
{
var options = new OptionsForSomethingElse
{
Locations = Locations.Split(_comma, StringSplitOptions.RemoveEmptyEntries),
NotSoAwesome = !Awesome
};
_command.Do(options);
}
}
*NotLastConsole_GoCommando*> .\doer.exe something
The following required parameters are missing:
-location - place where the something was done
Invoke with -help <command> to get help for each command.
Exit code: -1
-locations / -l
places where the something else was done
Examples:
-locations singleLocation
-locations multiple,comma-separated-locations
*NotLastConsole_GoCommando*> .\doer.exe something -l here -times x
System.FormatException: Could not set value 'x' on property named 'Times' on DgonDotNet.Blog.Samples.NotLastConsole_GoCommando.Something ---> System.FormatException: Input string was not in a correct
format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType)
at GoCommando.Internals.Parameter.SetValue(Object commandInstance, String value)
--- End of inner exception stack trace ---
at GoCommando.Internals.Parameter.SetValue(Object commandInstance, String value)
at GoCommando.Internals.CommandInvoker.ResolveParametersFromSwitches(IEnumerable`1 switches, ICommand commandInstance, ISet`1 setParameters)
at GoCommando.Internals.CommandInvoker.InnerInvoke(IEnumerable`1 switches, EnvironmentSettings environmentSettings)
at GoCommando.Internals.CommandInvoker.Invoke(IEnumerable`1 switches, EnvironmentSettings environmentSettings)
at GoCommando.Go.InnerRun(ICommandFactory commandFactory)
at GoCommando.Go.Run(ICommandFactory commandFactory)
Exit code: -2
class Program
{
static void Main(string[] args)
{
Go.Run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment