Example #1
0
        /// <summary>
        /// Builds a string that contains a parsing error message.
        /// </summary>
        /// <param name="options">The typed instance that collected command line arguments parsed with the <see cref="CommandLine.CommandLineParser"/> class.</param>
        /// <returns>The <see cref="System.String"/> that contains the parsing error message.</returns>
        public string RenderParsingErrorsText(CommandLineOptionsBase options)
        {
            Assumes.NotNull(options, "options");

            PostParsingState state = options.LastPostParsingState;
            var badOption          = state.BadOptionInfo;

            // other violations requires BadOptionInfo to be initialized
            if (badOption == null && !state.ViolatesMutualExclusiveness)
            {
                return(string.Empty);
            }

            var errorsText = new StringBuilder(_builderCapacity);

            if (state.ViolatesRequired)
            {
                errorsText.AppendFormat(GetMessageText(MessageEnum.ParsingErrorViolatesRequired), badOption.NameWithSwitch);
            }
            else if (state.ViolatesFormat)
            {
                errorsText.AppendFormat(GetMessageText(MessageEnum.ParsingErrorViolatesFormat), badOption.NameWithSwitch);
            }
            else
            {
                errorsText.Append(GetMessageText(MessageEnum.ParsingErrorViolatesExclusiveness));
            }

            return(errorsText.ToString());
        }
Example #2
0
        /// <summary>
        /// Builds a string that contains a parsing error message.
        /// </summary>
        /// <param name="options">
        /// An options target <see cref="CommandLineOptionsBase"/> instance that collected command line arguments parsed with the <see cref="CommandLine.CommandLineParser"/> class.
        /// </param>
        /// <param name="indent">The level of indentation.</param>
        /// <returns>
        /// The <see cref="System.String"/> that contains the parsing error message.
        /// </returns>
        public string RenderParsingErrorsText(CommandLineOptionsBase options, int indent)
        {
            if (options.InternalLastPostParsingState.Errors.Count == 0)
            {
                return(string.Empty);
            }
            var text = new StringBuilder();

            foreach (var e in options.InternalLastPostParsingState.Errors)
            {
                var line = new StringBuilder();
                line.Append(StringUtil.Spaces(indent));
                if (!string.IsNullOrEmpty(e.BadOption.ShortName))
                {
                    line.Append('-');
                    line.Append(e.BadOption.ShortName);
                    if (!string.IsNullOrEmpty(e.BadOption.LongName))
                    {
                        line.Append('/');
                    }
                }
                if (!string.IsNullOrEmpty(e.BadOption.LongName))
                {
                    line.Append("--");
                    line.Append(e.BadOption.LongName);
                }
                line.Append(" ");
                if (e.ViolatesRequired)
                {
                    line.Append(_sentenceBuilder.RequiredOptionMissingText);
                }
                else
                {
                    line.Append(_sentenceBuilder.OptionWord);
                }
                if (e.ViolatesFormat)
                {
                    line.Append(" ");
                    line.Append(_sentenceBuilder.ViolatesFormatText);
                }
                if (e.ViolatesMutualExclusiveness)
                {
                    if (e.ViolatesFormat || e.ViolatesRequired)
                    {
                        line.Append(" ");
                        line.Append(_sentenceBuilder.AndWord);
                    }
                    line.Append(" ");
                    line.Append(_sentenceBuilder.ViolatesMutualExclusivenessText);
                }
                line.Append('.');
                text.AppendLine(line.ToString());
            }
            return(text.ToString());
        }
Example #3
0
 public static void DefaultParsingErrorsHandler(CommandLineOptionsBase options, HelpText current)
 {
     if (options.InternalLastPostParsingState.Errors.Count > 0)
     {
         var errors = current.RenderParsingErrorsText(options, 2); // indent with two spaces
         if (!string.IsNullOrEmpty(errors))
         {
             current.AddPreOptionsLine(string.Concat(Environment.NewLine, current.SentenceBuilder.ErrorsHeadingText));
             //current.AddPreOptionsLine(errors);
             var lines = errors.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
             foreach (var line in lines)
             {
                 current.AddPreOptionsLine(line);
             }
         }
     }
 }
Example #4
0
 /// <summary>
 /// Builds a string that contains a parsing error message.
 /// </summary>
 /// <param name="options">
 /// An options target <see cref="CommandLineOptionsBase"/> instance that collected command line arguments parsed with the <see cref="CommandLine.CommandLineParser"/> class.
 /// </param>
 /// <returns>
 /// The <see cref="System.String"/> that contains the parsing error message.
 /// </returns>
 public string RenderParsingErrorsText(CommandLineOptionsBase options, int indent)
 {
     if (options.InternalLastPostParsingState.Errors.Count == 0)
     {
         return string.Empty;
     }
     var text = new StringBuilder();
     foreach (var e in options.InternalLastPostParsingState.Errors)
     {
         var line = new StringBuilder();
         line.Append(StringUtil.Spaces(indent));
         if (!string.IsNullOrEmpty(e.BadOption.ShortName))
         {
             line.Append('-');
             line.Append(e.BadOption.ShortName);
             line.Append('/');
         }
         line.Append("--");
         line.Append(e.BadOption.LongName);
         line.Append(" ");
         if (e.ViolatesRequired)
         {
             line.Append(_sentenceBuilder.RequiredOptionMissingText);
         }
         else
         {
             line.Append(_sentenceBuilder.OptionWord);
         }
         if (e.ViolatesFormat)
         {
             line.Append(_sentenceBuilder.ViolatesFormatText);
         }
         if (e.ViolatesMutualExclusiveness)
         {
             if (e.ViolatesFormat || e.ViolatesRequired)
             {
                 line.Append(" ");
                 line.Append(_sentenceBuilder.AndWord);
             }
             line.Append(" ");
             line.Append(_sentenceBuilder.ViolatesMutualExclusivenessText);
         }
         line.Append('.');
         text.AppendLine(line.ToString());
     }
     return text.ToString();
 }
 public static void DefaultParsingErrorsHandler(CommandLineOptionsBase options, HelpText current)
 {
     if (options.InternalLastPostParsingState.Errors.Count > 0)
     {
         var errors = current.RenderParsingErrorsText(options, 2); // indent with two spaces
         if (!string.IsNullOrEmpty(errors))
         {
             current.AddPreOptionsLine(string.Concat(Environment.NewLine, current.SentenceBuilder.ErrorsHeadingText));
             //current.AddPreOptionsLine(errors);
             var lines = errors.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
             foreach (var line in lines) { current.AddPreOptionsLine(line); }
         }
     }
 }
Example #6
0
        /// <summary>
        /// Builds a string that contains a parsing error message.
        /// </summary>
        /// <param name="options">The typed instance that collected command line arguments parsed with the <see cref="CommandLine.CommandLineParser"/> class.</param>
        /// <returns>The <see cref="System.String"/> that contains the parsing error message.</returns>
        public string RenderParsingErrorsText(CommandLineOptionsBase options)
        {
            Assumes.NotNull(options, "options");

            PostParsingState state = options.LastPostParsingState;
            var badOption = state.BadOptionInfo;
            // other violations requires BadOptionInfo to be initialized
            if (badOption == null && !state.ViolatesMutualExclusiveness)
                return string.Empty;

            var errorsText = new StringBuilder(_builderCapacity);

            if (state.ViolatesRequired)
                errorsText.AppendFormat(GetMessageText(MessageEnum.ParsingErrorViolatesRequired), badOption.NameWithSwitch);
            else if (state.ViolatesFormat)
                errorsText.AppendFormat(GetMessageText(MessageEnum.ParsingErrorViolatesFormat), badOption.NameWithSwitch);
            else
                errorsText.Append(GetMessageText(MessageEnum.ParsingErrorViolatesExclusiveness));

            return errorsText.ToString();
        }