Ejemplo n.º 1
0
 public static void RunCommandWithRequiredStringParameterNotSet()
 {
     Assert.Throws <MissingCommandParameterException>(() =>
     {
         CmdLinery.Run(typeof(TestCommands1), new string[] { "CommandWithRequiredStringParameter" }, new TestApplicationInfo());
     });
 }
Ejemplo n.º 2
0
        private static int Run(string[] args, BootStrapper bootStrapper, ILog logger)
        {
            var exitCode        = 0;
            var applicationInfo = bootStrapper.Container.Resolve <IApplicationInfo>();

            try
            {
                applicationInfo.Authors = @"_S_Authors_S_";
                // ReSharper disable once CoVariantArrayConversion
                object[] commandTargets = bootStrapper.Container.ResolveAll <CommandDefinition>();
                logger.Info($"Start: {applicationInfo.Name}.{applicationInfo.Version}. Command line: {Environment.CommandLine}");
                CmdLinery.RunEx(commandTargets, args, applicationInfo, bootStrapper.Container.Resolve <IMessenger>())
                .OnFailure(exception =>
                {
                    exitCode = 1;
                    logger.Error(exception.Message);
                });
            }
            catch (Exception ex)
            {
                logger.ErrorFormat($"Error when exeuting command.{Environment.NewLine}{ex}");
                exitCode = 2;
            }
            finally
            {
                logger.Info($"Stop: {applicationInfo.Name}.{applicationInfo.Version}. Return value: {exitCode}");
#if DEBUG
                // ReSharper disable once RedundantNameQualifier
                System.Console.WriteLine("Terminating in 5 seconds...");
                Thread.Sleep(5000);
#endif
            }
            return(exitCode);
        }
Ejemplo n.º 3
0
        public static void RunNonStaticAndStaticCommands()
        {
            var nonStaticAndStaticCommands = new NonStaticAndStaticTestCommands8();

            var testLoggerMoc = new Mock <ITestLogger>();

            NonStaticAndStaticTestCommands8.TestLogger = testLoggerMoc.Object;
            const string logMessage1 = "Running NonStaticCommand(\"parameter 1 value\")";
            const string logMessage2 = "Running StaticCommand(\"parameter 1 value\")";

            testLoggerMoc.Setup(logger => logger.Write(logMessage1));
            testLoggerMoc.Setup(logger => logger.Write(logMessage2));

            int nonStaticResult = CmdLinery.Run(new object[] { nonStaticAndStaticCommands },
                                                new string[]
            {
                "NonStaticCommand",
                "/parameter1=\"parameter 1 value\""
            }, new TestApplicationInfo(), new ConsoleMessenger());

            int staticResult = CmdLinery.Run(new object[] { nonStaticAndStaticCommands },
                                             new string[]
            {
                "StaticCommand",
                "/parameter1=\"parameter 1 value\""
            }, new TestApplicationInfo(), new ConsoleMessenger());

            Assert.AreEqual(1, nonStaticResult);
            Assert.AreEqual(2, staticResult);
            testLoggerMoc.Verify(logger => logger.Write(logMessage1), Times.Once);
        }
Ejemplo n.º 4
0
        public static void RunCreditsCommand()
        {
            var nonStaticTestCommands = new NonStaticTestCommands7();

            CmdLinery.Run(new object[] { nonStaticTestCommands },
                          new string[]
            {
                "Credits",
            }, new TestApplicationInfo(), new ConsoleMessenger(), new HelpProvider(() => new ConsoleMessenger()));
        }
Ejemplo n.º 5
0
        public static void RunCommandWithNoParametersSuccess()
        {
            var testLoggerMoc = new Mock <ITestLogger>();

            TestCommands1.TestLogger = testLoggerMoc.Object;
            const string logMessage = "Running CommandWithNoParameters";

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            CmdLinery.Run(typeof(TestCommands1), new string[] { "CommandWithNoParameters" }, new TestApplicationInfo());
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 6
0
 private static TryAsync <int> TryRun(string[] args) => () =>
 {
     AppDomain.CurrentDomain.UnhandledException += Logging.CurrentDomainOnUnhandledException;
     IApplicationInfo applicationInfo = new NCmdLiner.ApplicationInfo
     {
         Authors     = "github.trondr",
         Copyright   = "Copyright © github.trondr 2020",
         Description = "Notify end user about non-compliance."
     };
     var returnValue = CmdLinery.Run(typeof(CommandDefinitions), args, applicationInfo, new NotepadMessenger());
     return(returnValue);
 };
Ejemplo n.º 7
0
        public static void RunCommandWithRequiredStringParameterSet()
        {
            var testLoggerMoc = new Mock <ITestLogger>();

            TestCommands1.TestLogger = testLoggerMoc.Object;
            const string logMessage = "Running CommandWithRequiredStringParameter(\"required parameter1 value\")";

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            CmdLinery.Run(typeof(TestCommands1),
                          new string[] { "CommandWithRequiredStringParameter", "/parameter1=\"required parameter1 value\"" },
                          new TestApplicationInfo());
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 8
0
        public static void RunCommandWithNoParametersThrowingException()
        {
            var testLoggerMoc = new Mock <ITestLogger>();

            TestCommands1.TestLogger = testLoggerMoc.Object;
            const string logMessage = "Running CommandWithNoParametersThrowingException";

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            Assert.Throws <NCmdLinerException>(() =>
            {
                CmdLinery.Run(typeof(TestCommands1), new string[] { "CommandWithNoParametersThrowingException" }, new TestApplicationInfo());
            });
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 9
0
        public static void RunCommandWithNullOptionalDefaultValue()
        {
            var testLoggerMoc = new Mock <ITestLogger>();

            TestCommands6.TestLogger = testLoggerMoc.Object;
            const string logMessage    = "Running CommandWithNullOptionalDefaultValue(\"parameter 1 value\")";
            var          commandString = new string[]
            {
                "CommandWithNullOptionalDefaultValue",
                "/parameter1=\"parameter 1 value\""
            };

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            CmdLinery.Run(typeof(TestCommands6), commandString, new TestApplicationInfo());
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 10
0
        public static void RunCommandWithOneRequiredAndOptionalStringParameterNotSet()
        {
            var testLoggerMoc = new Mock <ITestLogger>();

            TestCommands1.TestLogger = testLoggerMoc.Object;
            const string logMessage    = "Running CommandWithOneRequiredAndOptionalStringParameter(\"parameter 1 value\",\"parameter 2 default value\")";
            var          commandString = new string[]
            {
                "CommandWithOneRequiredAndOptionalStringParameter", "/parameter1=\"parameter 1 value\""
            };

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            CmdLinery.Run(typeof(TestCommands1),
                          commandString,
                          new TestApplicationInfo());
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 11
0
        public static void CommandsFromMultipleNamespacesDuplicateCommandThrowDuplicateCommandException()
        {
            var testLoggerMoc = new Mock <ITestLogger>();

            TestCommandsMulti2Duplicate.TestLogger = testLoggerMoc.Object;
            const string logMessage    = "Running FirstCommand()";
            var          commandString = new string[]
            {
                "FirstCommand"
            };

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            Assert.Throws <DuplicateCommandException>(() =>
            {
                CmdLinery.Run(new Type[] { typeof(TestCommandsMulti1Duplicate), typeof(TestCommandsMulti2Duplicate) }, commandString, new TestApplicationInfo());
            });
        }
Ejemplo n.º 12
0
        public static void RunCommandWithNoOptionalDefaultValueThrowMissingExampleValueException()
        {
            var testLoggerMoc = new Mock <ITestLogger>();

            TestCommands5.TestLogger = testLoggerMoc.Object;
            const string logMessage    = "Running CommandWithNoOptionalDefaultValue(\"parameter 1 value\")";
            var          commandString = new string[]
            {
                "CommandWithNoOptionalDefaultValue",
                "/parameter1=\"parameter 1 value\""
            };

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            Assert.Throws <MissingDefaultValueException>(() =>
            {
                CmdLinery.Run(typeof(TestCommands5), commandString, new TestApplicationInfo());
            });
        }
Ejemplo n.º 13
0
        public static void CommandsFromMultipleNamespaces()
        {
            const int expected      = 10;
            var       testLoggerMoc = new Mock <ITestLogger>();

            TestCommandsMulti2.TestLogger = testLoggerMoc.Object;
            const string logMessage    = "Running SecondCommand()";
            var          commandString = new string[]
            {
                "SecondCommand"
            };

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            int actual = CmdLinery.Run(new Type[] { typeof(TestCommandsMulti1), typeof(TestCommandsMulti2) }, commandString, new TestApplicationInfo());

            Assert.AreEqual(expected, actual);
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 14
0
        public static void RunHelpCommandWithCustomHelperProvider()
        {
            var nonStaticTestCommands = new NonStaticTestCommands7();
            var testLoggerMoc         = new Mock <ITestLogger>();

            nonStaticTestCommands.TestLogger = testLoggerMoc.Object;
            const string logMessage    = "Running NonStaticCommand()";
            var          commandString = new string[]
            {
                "Help"
            };

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            Assert.Throws <CustomTestHelpProviderException>(() =>
            {
                CmdLinery.Run(new object[] { nonStaticTestCommands }, commandString, new TestApplicationInfo(), new ConsoleMessenger(), new CustomTestHelpProvider());
            });
        }
Ejemplo n.º 15
0
        public static void RunCommandWithNoParametersThrowingExceptionCheckStackTrace()
        {
            var testLoggerMoc = new Mock <ITestLogger>();

            TestCommands1.TestLogger = testLoggerMoc.Object;
            const string logMessage = "Running CommandWithNoParametersThrowingException";

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            try
            {
                CmdLinery.Run(typeof(TestCommands1), new string[] { "CommandWithNoParametersThrowingException" }, new TestApplicationInfo());
            }
            catch (Exception ex)
            {
                Assert.Contains("TestCommands1.CommandWithNoParametersThrowingException", ex.StackTrace);
            }
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 16
0
        static int Main(string[] args)
        {
            var returnValue = 0;

            try
            {
                var logger          = LogManager.GetLogger <Program>();
                var applicationInfo = BootStrapper.Container.Resolve <IApplicationInfo>();
                try
                {
                    applicationInfo.Authors = @"*****@*****.**";
                    // ReSharper disable once CoVariantArrayConversion
                    object[] commandTargets = BootStrapper.Container.ResolveAll <CommandDefinition>();
                    logger.InfoFormat("Start: {0}.{1}. Command line: {2}", applicationInfo.Name, applicationInfo.Version, Environment.CommandLine);
                    CmdLinery.RunEx(commandTargets, args, applicationInfo, BootStrapper.Container.Resolve <IMessenger>())
                    .OnFailure(exception =>
                    {
                        logger.Error(exception.Message);
                        returnValue = 1;
                    })
                    .OnSuccess(i => returnValue = i);
                    return(returnValue);
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat("Error when exeuting command. {0}", ex.ToString());
                    returnValue = 2;
                }
                finally
                {
                    logger.InfoFormat("Stop: {0}.{1}. Return value: {2}", applicationInfo.Name, applicationInfo.Version, returnValue);
#if DEBUG
                    System.Console.WriteLine("Press any key...");
                    Console.ReadLine();
#endif
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Fatal error when wiring up the application.{0}{1}", Environment.NewLine, ex);
                returnValue = 3;
            }
            return(returnValue);
        }
Ejemplo n.º 17
0
        private static int WireUpAndRun(string[] args)
        {
            var returnValue = 0;
            var logger      = GetMainLogger();

            using (var bootStrapper = new BootStrapper())
            {
                var applicationInfo = bootStrapper.Container.Resolve <IApplicationInfo>();
                try
                {
                    applicationInfo.Authors = @"_S_Authors_S_";
                    // ReSharper disable once CoVariantArrayConversion
                    object[] commandTargets = bootStrapper.Container.ResolveAll <CommandDefinition>();
                    logger.InfoFormat("Start: {0}.{1}. Command line: {2}", applicationInfo.Name, applicationInfo.Version, Environment.CommandLine);
                    returnValue = CmdLinery.Run(commandTargets, args, applicationInfo, bootStrapper.Container.Resolve <IMessenger>());
                    return(returnValue);
                }
                catch (MissingCommandException ex)
                {
                    logger.ErrorFormat("Missing command. {0}", ex.Message);
                    returnValue = 1;
                }
                catch (UnknownCommandException ex)
                {
                    logger.Error(ex.Message);
                    returnValue = 1;
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat("Error when exeuting command. {0}", ex.ToString());
                    returnValue = 2;
                }
                finally
                {
                    logger.InfoFormat("Stop: {0}.{1}. Return value: {2}", applicationInfo.Name, applicationInfo.Version, returnValue);
#if DEBUG
                    // ReSharper disable once RedundantNameQualifier
                    System.Console.WriteLine("Terminating in 5 seconds...");
                    Thread.Sleep(5000);
#endif
                }
            }
            return(returnValue);
        }
Ejemplo n.º 18
0
        public static void CommandWithReturnValueTest()
        {
            const int expected      = 10;
            var       testLoggerMoc = new Mock <ITestLogger>();

            TestCommands4.TestLogger = testLoggerMoc.Object;
            const string logMessage    = "Running CommandWithReturnValue(\"parameter 1 value\")";
            var          commandString = new string[]
            {
                "CommandWithReturnValue",
                "/parameter1=\"parameter 1 value\""
            };

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            int actual = CmdLinery.Run(typeof(TestCommands4), commandString, new TestApplicationInfo());

            Assert.AreEqual(expected, actual);
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 19
0
        static int Main(string[] args)
        {
            var returnValue = 0;

            try
            {
                var logger          = LogManager.GetCurrentClassLogger();
                var applicationInfo = BootStrapper.Container.Resolve <IApplicationInfo>();
                try
                {
                    applicationInfo.Authors = @"github.com/trondr";
                    // ReSharper disable once CoVariantArrayConversion
                    object[] commandTargets = BootStrapper.Container.ResolveAll <CommandDefinition>();
                    logger.InfoFormat("Start: {0} ({1}). Command line: {2}", applicationInfo.Name, applicationInfo.Version, logger.IsDebugEnabled? Environment.CommandLine: "***hidden***command***line***");
                    return(CmdLinery.Run(commandTargets, args, applicationInfo, new NotepadMessenger()));
                }
                catch (MissingCommandException ex)
                {
                    logger.ErrorFormat("Missing command. {0}", ex.Message);
                    returnValue = 1;
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat("Error when exeuting command. {0}", ex.ToString());
                    returnValue = 2;
                }
                finally
                {
                    logger.InfoFormat("Stop: {0} ({1}). Return value: {2}", applicationInfo.Name, applicationInfo.Version, returnValue);
#if DEBUG
                    Console.WriteLine("Press ENTER...");
                    Console.ReadLine();
#endif
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal error when wiring up the application.{0}{1}", Environment.NewLine, ex);
                returnValue = 3;
            }
            return(returnValue);
        }
Ejemplo n.º 20
0
        public static void RunNonStaticCommandWithParamenterHavingEqualCharacter()
        {
            var       nonStaticTestCommands = new NonStaticTestCommands7();
            const int expected      = 10;
            var       testLoggerMoc = new Mock <ITestLogger>();

            nonStaticTestCommands.TestLogger = testLoggerMoc.Object;
            const string logMessage    = "Running NonStaticCommand(\"LDAP://OU=TST,OU=Groups,DC=tst,DC=local\")";
            var          commandString = new string[]
            {
                "NonStaticCommand",
                "/parameter1=\"LDAP://OU=TST,OU=Groups,DC=tst,DC=local\""
            };

            testLoggerMoc.Setup(logger => logger.Write(logMessage));
            int actual = CmdLinery.Run(new object[] { nonStaticTestCommands }, commandString, new TestApplicationInfo(), new ConsoleMessenger());

            Assert.AreEqual(expected, actual);
            testLoggerMoc.Verify(logger => logger.Write(logMessage), Times.Once);
        }
Ejemplo n.º 21
0
        private static void Main(string[] args)
        {
            try
            {
                //Optional: Override some application info to modify the header of the auto documentation
                IApplicationInfo exampleApplicationInfo = new ApplicationInfo();
                //Set the optional authors property
                exampleApplicationInfo.Authors = "[email protected], [email protected]";
                //Override the assembly info copyright property
                exampleApplicationInfo.Copyright = "Copyright © examplecompany 2013";

                //Optional: Extend the default console messenger to show the help text in a form as well as the default console
                IMessenger messenger = new MyDialogMessenger(new ConsoleMessenger());

                //Parse and run the command line using specified target types
                //Type[] targetTypes = new Type[] { typeof(ExampleCommands1), typeof(ExampleCommands2) };
                //CmdLinery.Run(targetTypes, args, exampleApplicationInfo, messenger);

                //Parse and run the command line using specified assembly, CommandLinery will find all commands using reflection.
                CmdLinery.Run(Assembly.GetEntryAssembly(), args, exampleApplicationInfo, messenger);

                //By default he application info will be exctracted from the executing assembly meta data (assembly info)
                //and the help text will be output using the default ConsoleMessenger. If the default behaviour
                //is ok, the call to CmdLinery.Run(...) can be simplified to the following:
                //CmdLinery.Run(typeof(ExampleCommands1), args);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                Console.WriteLine("Press ENTER to terminate...");
                Console.ReadLine();
            }
        }
Ejemplo n.º 22
0
 public static void ShowHelpTest()
 {
     CmdLinery.Run(typeof(TestCommands1), new string[] { "Help" }, new TestApplicationInfo());
 }