Esempio n. 1
0
        private bool ParseHelp(string[] args, HelpOptionAttribute helpOption)
        {
            bool caseSensitive = _settings.CaseSensitive;

            foreach (string arg in args)
            {
                if (helpOption.ShortName != null)
                {
                    if (ArgumentParser.CompareShort(arg, helpOption.ShortName, caseSensitive))
                    {
                        return(true);
                    }
                }

                if (string.IsNullOrEmpty(helpOption.LongName))
                {
                    continue;
                }

                if (ArgumentParser.CompareLong(arg, helpOption.LongName, caseSensitive))
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a text block with options usage informations.
        /// </summary>
        /// <param name="options">The instance that collected command line arguments parsed with the <see cref="CommandLine.Parser"/> class.</param>
        /// <param name="requiredWord">The word to use when the option is required.</param>
        /// <exception cref="System.ArgumentNullException">Thrown when parameter <paramref name="options"/> is null.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown when parameter <paramref name="requiredWord"/> is null or empty string.</exception>
        public void AddOptions(object options, string requiredWord)
        {
            Validator.CheckIsNull(options, "options");
            Validator.CheckIsNullOrEmpty(requiredWord, "requiredWord");

            IList <BaseOptionAttribute> optionList =
                ReflectionUtil.RetrieveFieldAttributeList <BaseOptionAttribute>(options);

            HelpOptionAttribute optionHelp =
                ReflectionUtil.RetrieveMethodAttributeOnly <HelpOptionAttribute>(options);

            if (optionHelp != null)
            {
                optionList.Add(optionHelp);
            }

            if (optionList.Count == 0)
            {
                return;
            }

            int maxLength = GetMaxLength(optionList);

            this.optionsHelp = new StringBuilder(builderCapacity);

            foreach (BaseOptionAttribute option in optionList)
            {
                this.optionsHelp.Append("  ");
                StringBuilder optionName = new StringBuilder(maxLength);
                if (option.HasShortName)
                {
                    optionName.Append(option.ShortName);
                    if (option.HasLongName)
                    {
                        optionName.Append(", ");
                    }
                }
                if (option.HasLongName)
                {
                    optionName.Append(option.LongName);
                }
                if (optionName.Length < maxLength)
                {
                    this.optionsHelp.Append(optionName.ToString().PadRight(maxLength));
                }
                else
                {
                    this.optionsHelp.Append(optionName.ToString());
                }
                this.optionsHelp.Append("\t");
                if (option.Required)
                {
                    this.optionsHelp.Append(requiredWord);
                    this.optionsHelp.Append(' ');
                }
                this.optionsHelp.Append(option.HelpText);
                this.optionsHelp.Append(Environment.NewLine);
            }
        }
Esempio n. 3
0
        private bool DoParseArguments(string[] args, object options)
        {
            Pair <MethodInfo, HelpOptionAttribute> pair = ReflectionHelper.RetrieveMethod <HelpOptionAttribute>(options);
            TextWriter helpWriter = _settings.HelpWriter;

            if (pair != null && helpWriter != null)
            {
                // If help can be handled is displayed if is requested or if parsing fails
                if (ParseHelp(args, pair.Right) || !DoParseArgumentsCore(args, options))
                {
                    string helpText;
                    HelpOptionAttribute.InvokeMethod(options, pair, out helpText);
                    helpWriter.Write(helpText);
                    return(false);
                }

                return(true);
            }

            return(DoParseArgumentsCore(args, options));
        }
Esempio n. 4
0
        private bool ParseHelp(string[] args, HelpOptionAttribute helpOption)
        {
            var caseSensitive = _settings.CaseSensitive;
            foreach (var arg in args)
            {
                if (helpOption.ShortName != null)
                {
                    if (ArgumentParser.CompareShort(arg, helpOption.ShortName, caseSensitive))
                    {
                        return true;
                    }
                }

                if (string.IsNullOrEmpty(helpOption.LongName))
                {
                    continue;
                }

                if (ArgumentParser.CompareLong(arg, helpOption.LongName, caseSensitive))
                {
                    return true;
                }
            }

            return false;
        }