Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandLineArguments"/> class and parses
        /// command line arguments to set of switches and parameters.
        /// </summary>
        /// <param name="switches">Collection of known command line switches.</param>
        /// <param name="rawArguments">Command line arguments. The name of the program should be
        /// omitted from the array of arguments.</param>
        public CommandLineArguments(CommandLineSwitchSet switches, string[] rawArguments)
        {
            if (switches == null)
            {
                throw new ArgumentNullException(nameof(switches));
            }

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

            ParseResult result = Parse(switches, rawArguments);

            if (result.ParsingFailed)
            {
                throw new ArgumentsParsingException(result.ErrorText);
            }

            this.arguments = result.Arguments;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parse command line with provided set of command line switches.
        /// </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>
        public void ParseCommandLine(CommandLineSwitchSet switches, string[] arguments)
        {
            if (this.Arguments != null)
            {
                throw new InvalidOperationException("Command line arguments are already parsed.");
            }

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

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

            this.Switches = switches;

            this.Arguments = new CommandLineArguments(switches, arguments);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandLineSwitchSet"/> class
 /// and copies a list of switches from another collection.
 /// </summary>
 /// <param name="other">Collection to copy switches from.</param>
 public CommandLineSwitchSet(CommandLineSwitchSet other)
 {
     this.switches = new List <CommandLineSwitch>(other.switches);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Parses command line arguments to set of switches and parameters.
        /// </summary>
        /// <param name="switches">Collection of known command line switches.</param>
        /// <param name="rawArguments">Command line arguments. The name of the program should be
        /// omitted from the array of arguments.</param>
        /// <returns>Returns <see cref="ParseResult"/> class containing parsing results.</returns>
        private static ParseResult Parse(CommandLineSwitchSet switches, string[] rawArguments)
        {
            var parsedArguments = new Dictionary <CommandLineSwitch, string[]>();

            CommandLineSwitch currentSwitch = null;

            string[] parameters     = null;
            int      parametersLeft = 0;

            foreach (string argument in rawArguments)
            {
                if (parameters == null)
                {
                    if (argument.Length < 1 || argument[0] != CommandLineSwitch.PrefixSymbol)
                    {
                        return(new ParseResult($"Invalid switch: \"{argument}\". " +
                                               "Prefix symbol not found."));
                    }

                    string @switch = argument.Substring(1);

                    if (string.IsNullOrWhiteSpace(@switch))
                    {
                        return(new ParseResult($"Invalid switch: \"{argument}\". " +
                                               "Name not specified."));
                    }

                    foreach (CommandLineSwitch existingSwitch in switches)
                    {
                        if (existingSwitch.IsMatchesWithString(@switch))
                        {
                            currentSwitch = existingSwitch;
                            break;
                        }
                    }

                    if (currentSwitch == null)
                    {
                        return(new ParseResult($"Unknown switch \"{argument}\"."));
                    }

                    if (parsedArguments.ContainsKey(currentSwitch))
                    {
                        return(new ParseResult($"Switch \"{argument}\" provided " +
                                               "more than one time."));
                    }

                    parameters     = new string[currentSwitch.ParametersCount];
                    parametersLeft = currentSwitch.ParametersCount;

                    parsedArguments.Add(currentSwitch, parameters);
                }
                else
                {
                    parameters[currentSwitch.ParametersCount - parametersLeft] = argument;
                    --parametersLeft;
                }

                if (parametersLeft == 0)
                {
                    parameters    = null;
                    currentSwitch = null;
                }
            }

            if (parametersLeft != 0)
            {
                return(new ParseResult(string.Format(
                                           "Incorrect parameters count for switch \"{0}\". {1} parameters required, " +
                                           "but {2} found.",
                                           currentSwitch.FullPrefixedName,
                                           currentSwitch.ParametersCount,
                                           currentSwitch.ParametersCount - parametersLeft)));
            }

            return(new ParseResult(parsedArguments));
        }