Пример #1
0
 private void HandleUnexpectedArg(CommandLineApplication command, string[] args, int index, string argTypeName)
 {
     if (command._throwOnUnexpectedArg)
     {
         command.ShowHint();
         throw new CommandParsingException(command, String.Format(LocalizableStrings.UnexpectedArgumentError, argTypeName, args[index]));
     }
     else
     {
         // All remaining arguments are stored for further use
         command.RemainingArguments.AddRange(new ArraySegment <string>(args, index, args.Length - index));
     }
 }
Пример #2
0
        private ParseOptionResult ParseOption(
            bool isLongOption,
            CommandLineApplication command,
            string[] args,
            ref int index,
            out CommandOption option)
        {
            option = null;
            ParseOptionResult result = ParseOptionResult.Succeeded;
            var arg = args[index];

            int optionPrefixLength = isLongOption ? 2 : 1;

            string[] optionComponents = arg.Substring(optionPrefixLength).Split(new[] { ':', '=' }, 2);
            string   optionName       = optionComponents[0];

            if (isLongOption)
            {
                option = command.Options.SingleOrDefault(
                    opt => string.Equals(opt.LongName, optionName, StringComparison.Ordinal));
            }
            else
            {
                option = command.Options.SingleOrDefault(
                    opt => string.Equals(opt.ShortName, optionName, StringComparison.Ordinal));

                if (option == null)
                {
                    option = command.Options.SingleOrDefault(
                        opt => string.Equals(opt.SymbolName, optionName, StringComparison.Ordinal));
                }
            }

            if (option == null)
            {
                if (isLongOption && string.IsNullOrEmpty(optionName) &&
                    !command._throwOnUnexpectedArg && AllowArgumentSeparator)
                {
                    // a stand-alone "--" is the argument separator, so skip it and
                    // handle the rest of the args as unexpected args
                    index++;
                }

                HandleUnexpectedArg(command, args, index, argTypeName: "option");
                result = ParseOptionResult.UnexpectedArgs;
            }
            else if (command.OptionHelp == option)
            {
                result = ParseOptionResult.ShowHelp;
            }
            else if (command.OptionVersion == option)
            {
                result = ParseOptionResult.ShowVersion;
            }
            else
            {
                if (optionComponents.Length == 2)
                {
                    if (!option.TryParse(optionComponents[1]))
                    {
                        command.ShowHint();
                        throw new CommandParsingException(command,
                                                          String.Format(LocalizableStrings.UnexpectedValueForOptionError, optionComponents[1], optionName));
                    }
                }
                else
                {
                    if (option.OptionType == CommandOptionType.NoValue ||
                        option.OptionType == CommandOptionType.BoolValue)
                    {
                        // No value is needed for this option
                        option.TryParse(null);
                    }
                    else
                    {
                        index++;

                        if (index < args.Length)
                        {
                            arg = args[index];
                            if (!option.TryParse(arg))
                            {
                                command.ShowHint();
                                throw new CommandParsingException(
                                          command,
                                          String.Format(LocalizableStrings.UnexpectedValueForOptionError, arg, optionName));
                            }
                        }
                        else
                        {
                            command.ShowHint();
                            throw new CommandParsingException(
                                      command,
                                      String.Format(LocalizableStrings.OptionRequiresSingleValueWhichIsMissing, arg, optionName));
                        }
                    }
                }
            }

            return(result);
        }