Example #1
0
        public static IEnumerable <object[]> GetCorrectCommandLineArguments()
        {
            var emptySwitches = new CommandLineSwitchSet();

            var switch1 = new CommandLineSwitch("switch1", 's', 0, "Test switch.");
            var switch2 = new CommandLineSwitch("switch2", 'p', 1, "Test switch.");

            var switches = new CommandLineSwitchSet(new CommandLineSwitch[] { switch1, switch2 });

            var emptyArguments = new string[] { };
            var arguments1     = new string[] { "-switch1" };
            var arguments2     = new string[] { "-switch2", "param" };
            var arguments3     = new string[] { "-s" };
            var arguments4     = new string[] { "-p", "param" };
            var arguments5     = new string[] { "-switch1", "-p", "param" };
            var arguments6     = new string[] { "-switch2", "param", "-s" };

            yield return(new object[] { emptySwitches, emptyArguments });

            yield return(new object[] { switches, emptyArguments });

            yield return(new object[] { switches, arguments1 });

            yield return(new object[] { switches, arguments2 });

            yield return(new object[] { switches, arguments3 });

            yield return(new object[] { switches, arguments4 });

            yield return(new object[] { switches, arguments5 });

            yield return(new object[] { switches, arguments6 });
        }
Example #2
0
        public void CreateSwicthCollectionWithSwitches(
            CommandLineSwitch firstSwitch, CommandLineSwitch secondSwitch)
        {
            var switchArray = new CommandLineSwitch[] { firstSwitch, secondSwitch };

            var switches = new CommandLineSwitchSet(switchArray);
        }
        /// <summary>
        /// Prepares ConsoleUtilityContext to execute payload.
        /// </summary>
        /// <param name="switches">Collection of known command line switches.</param>
        /// <param name="arguments">Command line arguments. The name of the program should be
        /// omitted from the array of arguments.</param>
        /// <returns>true if payload can be executed; otherwise, false.</returns>
        public bool Initialize(CommandLineSwitchSet switches, string[] arguments)
        {
            if (switches == null)
            {
                throw new ArgumentNullException(nameof(switches));
            }

            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }

            try
            {
                this.context.ParseCommandLine(switches, arguments);
            }
            catch (ArgumentsParsingException ex)
            {
                this.ExitCode = DefaultExitCodes.CommandLineParseError;
                this.OutputWelcomeMessage();
                this.context.Output.Stream.WriteLine(
                    $"Error parsing command line arguments: {ex.Message}");
                this.context.Output.Stream.WriteLine();
                this.OutputHelpMessage();
                return(false);
            }

            this.context.SetupVerboseLevel(DefaultSwitches.Silent, DefaultSwitches.Verbose);

            try
            {
                this.context.SetupOutput(DefaultSwitches.Output);
            }
            catch (StreamOutputException ex)
            {
                this.ExitCode = DefaultExitCodes.CreateOutputFileError;
                this.OutputWelcomeMessage();
                this.context.Output.Stream.WriteLine($"Error opening output file: {ex.Message}");
                this.context.Output.Stream.WriteLine();
                this.OutputHelpMessage();
                return(false);
            }

            this.OutputWelcomeMessage();

            if (this.context.Arguments.GetArgument(DefaultSwitches.Help).isSpecified)
            {
                this.OutputHelpMessage();
                this.ExitCode = DefaultExitCodes.Success;
                return(false);
            }

            if (this.context.Verbose)
            {
                this.context.Output.Stream.WriteLine(this.context.Arguments.ToString());
            }

            return(true);
        }
Example #4
0
        public void AddCorrectSwitchesToCollection(CommandLineSwitch[] switches)
        {
            var switchArray = new CommandLineSwitch[] { switches[0], switches[1] };

            var switchesCollection = new CommandLineSwitchSet(switchArray);

            switchesCollection.AddSwitch(switches[2]);
        }
Example #5
0
        public void AddCorrectSwitchesToEmptyCollection(
            CommandLineSwitch firstSwitch, CommandLineSwitch secondSwitch)
        {
            var switches = new CommandLineSwitchSet();

            switches.AddSwitch(firstSwitch);
            switches.AddSwitch(secondSwitch);
        }
Example #6
0
 public void CreateArgumentsWithIncorrectCommandLine(
     CommandLineSwitchSet switches, string[] rawArguments)
 {
     Assert.Throws <ArgumentsParsingException>(() =>
     {
         var arguments = new CommandLineArguments(switches, rawArguments);
     });
 }
Example #7
0
        public void AddCorrectSwitchesToCopiedCollection(CommandLineSwitch[] switches)
        {
            var switchArray = new CommandLineSwitch[] { switches[0], switches[1] };

            var sourceSwitches = new CommandLineSwitchSet(switchArray);
            var copySwitches   = new CommandLineSwitchSet(sourceSwitches);

            copySwitches.AddSwitch(switches[2]);
        }
Example #8
0
            // It's not required to setup switches in static constructor.
            // You can do it in another place.
            static Switches()
            {
                // Create a new set of switches and fill it with default switches.
                Set = new CommandLineSwitchSet(DefaultSwitches.Collection);

                // Add custom switches to set.
                Set.AddSwitch(UserName);
                Set.AddSwitch(Greeting);
            }
Example #9
0
        public void CreateSwitchCollectionWithIncompatibleSwitches(
            CommandLineSwitch firstSwitch, CommandLineSwitch secondSwitch)
        {
            var switchArray = new CommandLineSwitch[] { firstSwitch, secondSwitch };

            Assert.Throws <InvalidOperationException>(() =>
            {
                var switches = new CommandLineSwitchSet(switchArray);
            });
        }
Example #10
0
        public void AddConflictingSwitchesToEmptyCollection(
            CommandLineSwitch firstSwitch, CommandLineSwitch secondSwitch)
        {
            var switches = new CommandLineSwitchSet();

            switches.AddSwitch(firstSwitch);

            Assert.Throws <InvalidOperationException>(() =>
            {
                switches.AddSwitch(secondSwitch);
            });
        }
Example #11
0
        public static IEnumerable <object[]> GetIncorrectArgumentsConstructorParameters()
        {
            var switch1 = new CommandLineSwitch("switch1", 's', 0, "Test switch.");
            var switch2 = new CommandLineSwitch("switch2", 'p', 0, "Test switch.");

            var switches  = new CommandLineSwitchSet(new CommandLineSwitch[] { switch1, switch2 });
            var arguments = new string[] { "-switch1", "-p" };

            yield return(new object[] { null, null });

            yield return(new object[] { switches, null });

            yield return(new object[] { null, arguments });
        }
Example #12
0
        public void CreateArgumentsAndGetArguments()
        {
            var switch1 = new CommandLineSwitch("switch1", 'a', 0, "Test argument.");
            var switch2 = new CommandLineSwitch("switch2", 'b', 0, "Test argument.");
            var switch3 = new CommandLineSwitch("switch3", null, 0, "Another test argument.");
            var switch4 = new CommandLineSwitch("switch4", null, 1, "Another test argument.");
            var switch5 = new CommandLineSwitch(null, 'c', 0, "Another test argument.");
            var switch6 = new CommandLineSwitch(null, 'd', 1, "Another test argument.");
            var switch7 = new CommandLineSwitch(null, 'e', 1, "Another test argument.");

            var switches = new CommandLineSwitchSet(
                new CommandLineSwitch[]
            {
                switch1, switch2, switch3, switch4, switch5, switch6, switch7
            });

            var rawArguments = new string[] { "-a", "-switch3", "-d", "param_d", "-e", "param_e" };

            var arguments = new CommandLineArguments(switches, rawArguments);

            var switch1Result = arguments.GetArgument(switch1);
            var switch2Result = arguments.GetArgument(switch2);
            var switch3Result = arguments.GetArgument(switch3);
            var switch4Result = arguments.GetArgument(switch4);
            var switch5Result = arguments.GetArgument(switch5);
            var switch6Result = arguments.GetArgument(switch6);
            var switch7Result = arguments.GetArgument(switch7);

            Assert.True(switch1Result.isSpecified);
            Assert.Empty(switch1Result.parameters);

            Assert.False(switch2Result.isSpecified);
            Assert.Null(switch2Result.parameters);

            Assert.True(switch3Result.isSpecified);
            Assert.Empty(switch3Result.parameters);

            Assert.False(switch4Result.isSpecified);
            Assert.Null(switch4Result.parameters);

            Assert.False(switch5Result.isSpecified);
            Assert.Null(switch5Result.parameters);

            Assert.True(switch6Result.isSpecified);
            Assert.Single(switch6Result.parameters, "param_d");

            Assert.True(switch7Result.isSpecified);
            Assert.Single(switch7Result.parameters, "param_e");
        }
        /// <summary>
        /// Creates a new instance of the <see cref="SimpleConsoleUtility"/> class, initializes
        /// it with provided <see cref="CommandLineSwitchSet"/> and command line arguments
        /// and executes payload function if needed.\n When payload execution is finished,
        /// Environment.Exit() is called.
        /// </summary>
        /// <param name="switches">Collection of known command line switches.</param>
        /// <param name="arguments">Command line arguments. The name of the program should be
        /// omitted from the array of arguments.</param>
        /// <param name="payload">Function that accepts <see cref="ConsoleUtilityContext"/>
        /// as an argument and returns exit code.</param>
        public static void RunAndQuit(
            CommandLineSwitchSet switches,
            string[] arguments,
            Func <ConsoleUtilityContext, int> payload)
        {
            var utility = new SimpleConsoleUtility();

            bool allowedToExecute = utility.Initialize(switches, arguments);

            if (allowedToExecute)
            {
                utility.Execute(payload);
            }

            Environment.Exit(utility.ExitCode);
        }
Example #14
0
 public void CreateAndCopyEmptySwicthCollection()
 {
     var sourceSwitches = new CommandLineSwitchSet();
     var copySwitches   = new CommandLineSwitchSet(sourceSwitches);
 }
Example #15
0
 public void CreateEmptySwicthCollection()
 {
     var switches = new CommandLineSwitchSet();
 }
Example #16
0
 public void CreateArgumentsWithСorrectCommandLine(
     CommandLineSwitchSet switches, string[] rawArguments)
 {
     var arguments = new CommandLineArguments(switches, rawArguments);
 }