Beispiel #1
0
        public void CreateShellCommandTest()
        {
            TestCommand command = (TestCommand)ShellCommand.CreateShellCommand(Assembly.GetExecutingAssembly(), "test", new[] { "-Argument", "Foo" }, 0);

            Assert.IsNotNull(command);
            Assert.AreEqual("Foo", command.Argument);

            command = (TestCommand)ShellCommand.CreateShellCommand(Assembly.GetExecutingAssembly(), new[] { "test", "-Argument", "Bar" }, 0);
            Assert.IsNotNull(command);
            Assert.AreEqual("Bar", command.Argument);

            AnotherCommand command2 = (AnotherCommand)ShellCommand.CreateShellCommand(Assembly.GetExecutingAssembly(), "anothercommand", new[] { "skip", "-Value", "42" }, 1);

            Assert.IsNotNull(command2);
            Assert.AreEqual(42, command2.Value);

            CustomParsingCommand command3 = (CustomParsingCommand)ShellCommand.CreateShellCommand(Assembly.GetExecutingAssembly(), new[] { "custom", "hello" }, 0);

            Assert.IsNotNull(command3);
            Assert.AreEqual("hello", command3.Value);
        }
        static int Main(string[] args)
        {
            // Create a shell command based on the arguments. The CreateShellCommand method will catch any command line errors
            // and print error details and usage information on the console, so we don't have to worry about that here.
            CreateShellCommandOptions options = new CreateShellCommandOptions();

            options.UsageOptions.IncludeDefaultValueInDescription = true;
            options.UsageOptions.IncludeAliasInDescription        = true;
            ShellCommand command = ShellCommand.CreateShellCommand(Assembly.GetExecutingAssembly(), args, 0, options);

            if (command != null)
            {
                // The command line arguments were successfully parsed, so run the command.
                command.Run();
                // When using shell commands, it's good practice to return the value of the ExitStatus property to the operating system.
                // The application or script that invoked your application can check the exit code from your application to
                // see if you were successful or not. Error codes are completely application specific, but usually 0 is used to
                // indicate success, and any other value indicates an error.
                return(command.ExitCode);
            }

            return(1); // Return an error status if the command couldn't be created.
        }