private static void Main(string[] args) { using (var build = new Build() .Recognize(typeof(DotGraphController)) .Recognize(typeof(GraphController)) .Recognize(typeof(SerializeController))) { try { var parsedMethod = build.Parse(args); if (parsedMethod.UnRecognizedArguments.Any())//Warning: { var unRecognizedArgumentsMessage = string.Format( @"Unrecognized arguments: {0} Did you mean any of these arguments? {1}", String.Join(",", parsedMethod.UnRecognizedArguments.Select(unrec => unrec.Value).ToArray()), String.Join(",", parsedMethod.ArgumentWithOptions.Select(rec => rec.Name).ToArray())); Console.WriteLine(unRecognizedArgumentsMessage); Environment.Exit(1); } else { parsedMethod.Invoke(Console.Out); } } catch (TypeConversionFailedException ex) { Console.WriteLine(String.Format("Could not convert argument {0} with value {1} to type {2}", ex.Argument, ex.Value, ex.TargetType)); if (null != ex.InnerException) { Console.WriteLine("Inner exception: "); Console.WriteLine(ex.InnerException.Message); } Environment.Exit(1); } catch (MissingArgumentException ex) { Console.WriteLine(String.Format("Missing argument(s): {0}", String.Join(", ", ex.Arguments.ToArray()))); if (build.RecognizesHelp) Console.WriteLine(build.Help()); Environment.Exit(1); } #if DEBUG catch (Exception ex1) { Console.WriteLine(string.Join(Environment.NewLine, new object[] { "The invokation failed with exception:", ex1.Message, ex1.StackTrace })); Environment.Exit(1); } #endif } }
public void Can_invoke_configure_method_on_configuration() { var conf = new FullConfiguration(); var parserBuilder = new Build().Configuration(conf); var parsed = parserBuilder.Parse(new[] { "--global", "globalvalue", "--globalrequired", "2", "My", "Action", "--value", "1" }); var cout = new StringWriter(); parsed.Invoke(cout); Assert.That(conf.Global, Is.EqualTo("globalvalue")); }
static void Main(string[] args) { var path = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location) .FullName; using(var parserBuilder = new Build().ConfigurationFrom( path)) { try { var parsedMethod = parserBuilder.Parse(args); if (parsedMethod.UnRecognizedArguments.Any())//Warning: { var unRecognizedArgumentsMessage = string.Format( @"Unrecognized arguments: {0} Did you mean any of these arguments? {1}", String.Join(",", parsedMethod.UnRecognizedArguments.Select(unrec => unrec.Value).ToArray()), String.Join(",", parsedMethod.ArgumentWithOptions.Select(rec => rec.Argument.ToString()).ToArray())); Console.WriteLine(unRecognizedArgumentsMessage); }else { parsedMethod.Invoke(Console.Out); } } catch (TypeConversionFailedException ex) { Console.WriteLine(String.Format("Could not convert argument {0} with value {1} to type {2}", ex.Argument, ex.Value, ex.TargetType)); if (null!=ex.InnerException) { Console.WriteLine("Inner exception: "); Console.WriteLine(ex.InnerException.Message); } } catch (MissingArgumentException ex) { Console.WriteLine(String.Format("Missing argument(s): {0}",String.Join(", ",ex.Arguments.Select(a=>String.Format("{0}: {1}",a.Key,a.Value)).ToArray()))); if (parserBuilder.RecognizesHelp) Console.WriteLine(parserBuilder.Help()); } #if DEBUG catch (Exception ex1) { Console.WriteLine( string.Join(Environment.NewLine, new object[]{ "The invokation failed with exception:", ex1.Message, ex1.StackTrace})); return; } #endif } }
public void It_will_dispose_types_created_by_default_factory_set_to_be_recognized() { var count = 0; DisposeController.StaticOnDispose += c => { count++; }; using (var build = new Build() .SetCulture(CultureInfo.InvariantCulture) .Recognize<DisposeController>()) { build .Parse(new[] { "Dispose", "method" }) .Invoke(new StringWriter()); } Assert.That(count, Is.EqualTo(1)); }
public void It_will_not_dispose_instances_set_to_be_recognized() { var count = 0; var c = new DisposeController(); c.OnDispose+= () => count++; using (var build = new Build() .SetCulture(CultureInfo.InvariantCulture) .Recognize(c)) { build .Parse(new[] { "Dispose", "method" }) .Invoke(new StringWriter()); } Assert.That(count, Is.EqualTo(0)); }
static void Main(string[] args) { var parserBuilder = new Build() .ShouldRecognizeHelp() .Recognize(typeof(MyController)) .Recognize(typeof(CustomerController)); try { var parsedMethod = parserBuilder.Parse(args); if (parsedMethod.UnRecognizedArguments.Any())//Warning: { var unRecognizedArgumentsMessage = string.Format( @"Unrecognized arguments: {0} Did you mean any of these arguments? {1}", String.Join(",", parsedMethod.UnRecognizedArguments.Select(unrec => unrec.Value).ToArray()), String.Join(",", parsedMethod.ArgumentWithOptions.Select(rec => rec.Argument.ToString()).ToArray())); Console.WriteLine(unRecognizedArgumentsMessage); }else { parsedMethod.Invoke(Console.Out); } } catch (TypeConversionFailedException ex) { Console.WriteLine(String.Format("Could not convert argument {0} with value {1} to type {2}", ex.Argument, ex.Value, ex.TargetType)); if (null!=ex.InnerException) { Console.WriteLine("Inner exception: "); Console.WriteLine(ex.InnerException.Message); } } catch (MissingArgumentException ex) { Console.WriteLine(String.Format("Missing argument(s): {0}",String.Join(", ",ex.Arguments.Select(a=>String.Format("{0}: {1}",a.Key,a.Value)).ToArray()))); Console.WriteLine(parserBuilder.Help()); } }
public void It_will_not_dispose_factory_created_instances_set_to_be_recognized() { var count = 0; Func<Type, object> factory = (Type t) => { Assert.That(t, Is.EqualTo(typeof(DisposeController))); var c = new DisposeController(); c.OnDispose+= () => count++; return c; }; using (var build = new Build() .SetCulture(CultureInfo.InvariantCulture) .Recognize<DisposeController>() .SetFactory(factory)) { build .Parse(new[] { "Dispose", "method" }) .Invoke(new StringWriter()); } Assert.That(count, Is.EqualTo(0)); }