Пример #1
0
        void ProcessOption(CommandLineOption option, String[] args, Boolean optionIsLastOrAlone, ref Int32 originalArgIndex)
        {
            // Check for duplicates
            if (option.set)
            {
                throw new CommandLineException(String.Format("Found option '{0}' twice", option));
            }
            option.set = true;

            if (option.hasArg)
            {
                if (!optionIsLastOrAlone)
                {
                    throw new CommandLineException(String.Format("Option '{0}' requires an argument but it is not the last letter in a list of options", option));
                }

                originalArgIndex++;
                if (originalArgIndex >= args.Length)
                {
                    throw new CommandLineException(String.Format("Option '{0}' requires an argument but there was none", option));
                }

                option.ParseArg(args[originalArgIndex]);
            }
        }
Пример #2
0
        public void Add(CommandLineOption option)
        {
            if (option.letter == '\0' && option.name == null)
            {
                throw new ArgumentException(String.Format("Invalid Option '{0}' has no letter and no name", option), "option");
            }

            CommandLineOption otherOption;

            if (option.letter != '\0')
            {
                if (optionLetters.TryGetValue(option.letter, out otherOption))
                {
                    throw new InvalidOperationException(String.Format("You've added two options with same letter '{0}' ({1}) and ({2})",
                                                                      option.letter, otherOption, option));
                }
                optionLetters.Add(option.letter, option);
            }

            if (option.name != null)
            {
                if (optionNames.TryGetValue(option.name, out otherOption))
                {
                    throw new InvalidOperationException(String.Format("You've added two options with same name '{0}' ({1}) and ({2})",
                                                                      option.name, otherOption, option));
                }
                optionNames.Add(option.name, option);
            }

            this.options.Add(option);

            //
            // Update maximum usage left column length
            //
            if (option.usageLeftColumn.Length > maxUsageLeftColumnWidth)
            {
                maxUsageLeftColumnWidth = option.usageLeftColumn.Length;
                if (maxUsageLeftColumnWidth >= consoleWidth - 40)
                {
                    consoleWidth = maxUsageLeftColumnWidth + 40;
                }
            }
        }