Наследование: BaseOptionAttribute
Пример #1
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);
        }
Пример #2
0
        private bool DoParseArguments(string[] args, object options)
        {
            var pair       = ReflectionHelper.RetrieveMethod <HelpOptionAttribute>(options);
            var 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));
        }
Пример #3
0
        /// <summary>
        /// Parses a <see cref="System.String"/> array of command line arguments, setting values in <paramref name="options"/>
        /// parameter instance's public fields decorated with appropriate attributes.
        /// This overload allows you to specify a <see cref="System.IO.TextWriter"/> derived instance for write text messages.
        /// </summary>
        /// <param name="args">A <see cref="System.String"/> array of command line arguments.</param>
        /// <param name="options">An object's instance used to receive values.
        /// Parsing rules are defined using <see cref="CommandLine.BaseOptionAttribute"/> derived types.</param>
        /// <param name="helpWriter">Any instance derived from <see cref="System.IO.TextWriter"/>,
        /// usually <see cref="System.Console.Error"/>. Setting this argument to null, will disable help screen.</param>
        /// <returns>True if parsing process succeed.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="args"/> is null.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="options"/> is null.</exception>
        public virtual bool ParseArguments(string[] args, object options, TextWriter helpWriter)
        {
            Assumes.NotNull(args, "args");
            Assumes.NotNull(options, "options");

            var pair = ReflectionUtil.RetrieveMethod <HelpOptionAttribute>(options);

            if (pair != null && helpWriter != null)
            {
                if (ParseHelp(args, pair.Right) || !DoParseArguments(args, options))
                {
                    string helpText;
                    HelpOptionAttribute.InvokeMethod(options, pair, out helpText);
                    helpWriter.Write(helpText);
                    return(false);
                }
                return(true);
            }

            return(DoParseArguments(args, options));
        }
 private static bool ParseHelp(string[] args, HelpOptionAttribute helpOption)
 {
     for (int i = 0; i < args.Length; i++)
     {
         if (!string.IsNullOrEmpty(helpOption.ShortName))
         {
             if (ArgumentParser.CompareShort(args[i], helpOption.ShortName))
             {
                 return(true);
             }
         }
         if (!string.IsNullOrEmpty(helpOption.LongName))
         {
             if (ArgumentParser.CompareLong(args[i], helpOption.LongName))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        /// <summary>
        /// Parses a <see cref="System.String"/> array of command line arguments,
        /// setting values read in <paramref name="options"/> parameter instance.
        /// This overloads allows you to specify a <see cref="System.IO.TextWriter"/>
        /// derived instance for write text messages.
        /// </summary>
        /// <param name="args">A <see cref="System.String"/> array of command line arguments.</param>
        /// <param name="options">An instance to receive values.
        /// Parsing rules are defined using <see cref="CommandLine.BaseOptionAttribute"/> derived types.</param>
        /// <param name="helpWriter">Any instance derived from <see cref="System.IO.TextWriter"/>,
        /// usually <see cref="System.Console.Out"/>.</param>
        /// <returns>True if parsing process succeed.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="args"/> is null.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="options"/> is null.</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="helpWriter"/> is null.</exception>
        public static bool ParseArguments(string[] args, object options, TextWriter helpWriter)
        {
            Validator.CheckIsNull(args, "args");
            Validator.CheckIsNull(options, "options");
            Validator.CheckIsNull(helpWriter, "helpWriter");

            Pair <MethodInfo, HelpOptionAttribute> pair =
                ReflectionUtil.RetrieveMethod <HelpOptionAttribute>(options);

            if (pair == null)
            {
                throw new InvalidOperationException();
            }
            if (ParseHelp(args, pair.Right) || !ParseArgumentList(args, options))
            {
                string helpText;
                HelpOptionAttribute.InvokeMethod(options, pair, out helpText);
                helpWriter.Write(helpText);
                return(false);
            }
            return(true);
        }
Пример #6
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;
        }
        private bool ParseHelp(string[] args, HelpOptionAttribute helpOption)
        {
            bool caseSensitive = _settings.CaseSensitive;

            for (int i = 0; i < args.Length; i++)
            {
                if (!string.IsNullOrEmpty(helpOption.ShortName))
                {
                    if (ArgumentParser.CompareShort(args[i], helpOption.ShortName, caseSensitive))
                        return true;
                }

                if (!string.IsNullOrEmpty(helpOption.LongName))
                {
                    if (ArgumentParser.CompareLong(args[i], helpOption.LongName, caseSensitive))
                        return true;
                }
            }

            return false;
        }
Пример #8
0
 private static bool ParseHelp(string[] args, HelpOptionAttribute helpOption)
 {
     for (int i = 0; i < args.Length; i++)
     {
         if (!string.IsNullOrEmpty(helpOption.ShortName))
         {
             if (ArgumentParser.CompareShort(args[i], helpOption.ShortName))
             {
                 return true;
             }
         }
         if (!string.IsNullOrEmpty(helpOption.LongName))
         {
             if (ArgumentParser.CompareLong(args[i], helpOption.LongName))
             {
                 return true;
             }
         }
     }
     return false;
 }