コード例 #1
0
        /// <summary>
        /// Creates a new instance of the <see cref="CommandLine.Text.HelpText"/> class using common defaults.
        /// </summary>
        /// <returns>
        /// An instance of <see cref="CommandLine.Text.HelpText"/> class.
        /// </returns>
        /// <param name='options'>The instance that collected command line arguments parsed with <see cref="Parser"/> class.</param>
        /// <param name='onError'>A delegate used to customize the text block for reporting parsing errors.</param>
        /// <param name="verbsIndex">If true the output style is consistent with verb commands (no dashes), otherwise it outputs options.</param>
        public static HelpText AutoBuild(object options, Action<HelpText> onError, bool verbsIndex = false)
        {
            var auto = new HelpText
            {
                Heading = HeadingInfo.Default,
                Copyright = CopyrightInfo.Default,
                AdditionalNewLineAfterOption = true,
                AddDashesToOption = !verbsIndex
            };

            if (onError != null)
            {
                var list = ReflectionHelper.RetrievePropertyList<ParserStateAttribute>(options);
                if (list != null)
                {
                    onError(auto);
                }
            }

            var license = ReflectionHelper.GetAttribute<AssemblyLicenseAttribute>();
            if (license != null)
            {
                license.AddToHelpText(auto, true);
            }

            var usage = ReflectionHelper.GetAttribute<AssemblyUsageAttribute>();
            if (usage != null)
            {
                usage.AddToHelpText(auto, true);
            }

            auto.AddOptions(options);

            return auto;
        }
コード例 #2
0
        /// <summary>
        /// Supplies a default parsing error handler implementation.
        /// </summary>
        /// <param name="options">The instance that collects parsed arguments parsed and associates <see cref="CommandLine.ParserStateAttribute"/>
        /// to a property of type <see cref="IParserState"/>.</param>
        /// <param name="current">The <see cref="CommandLine.Text.HelpText"/> instance.</param>
        public static void DefaultParsingErrorsHandler(object options, HelpText current)
        {
            var list = ReflectionHelper.RetrievePropertyList<ParserStateAttribute>(options);
            if (list.Count == 0)
            {
                return;
            }

            var parserState = (IParserState)list[0].Left.GetValue(options, null);
            if (parserState == null || parserState.Errors.Count == 0)
            {
                return;
            }

            var errors = current.RenderParsingErrorsText(options, 2); // indent with two spaces
            if (!string.IsNullOrEmpty(errors))
            {
                current.AddPreOptionsLine(string.Concat(Environment.NewLine, current.SentenceBuilder.ErrorsHeadingText));
                var lines = errors.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                foreach (var line in lines)
                {
                    current.AddPreOptionsLine(line);
                }
            }
        }
コード例 #3
0
 internal void AddToHelpText(HelpText helpText, bool before)
 {
     // before flag only distinguishes which action is called,
     // so refactor common code and call with appropriate action
     if (before)
     {
         AddToHelpText(helpText.AddPreOptionsLine);
     }
     else
     {
         AddToHelpText(helpText.AddPostOptionsLine);
     }
 }