Beispiel #1
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="CommandLineParser"/> class.
 /// </param>
 /// <param name="indent"> </param>
 /// <returns>
 /// The <see cref="System.String"/> that contains the parsing error message.
 /// </returns>
 public string RenderParsingErrorsText(CommandLineOptionsBase options, int indent)
 {
     if (options == null) throw new ArgumentNullException("options");
     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(new String(' ', indent));
         if (!string.IsNullOrWhiteSpace(e.BadOption.ShortName))
         {
             line.Append('-');
             line.Append(e.BadOption.ShortName);
             if (!string.IsNullOrWhiteSpace(e.BadOption.LongName)) line.Append('/');
         }
         if (!string.IsNullOrWhiteSpace(e.BadOption.LongName))
         {
             line.Append("--");
             line.Append(e.BadOption.LongName);
         }
         line.Append(" ");
         line.Append(e.ViolatesRequired
                         ? SentenceBuilder.RequiredOptionMissingText
                         : 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();
 }
Beispiel #2
0
        public static void DefaultParsingErrorsHandler(CommandLineOptionsBase options, HelpText current)
        {
            if (options == null) throw new ArgumentNullException("options");
            if (current == null) throw new ArgumentNullException("current");

            if (options.InternalLastPostParsingState.Errors.Count <= 0)
                return;

            var errors = current.RenderParsingErrorsText(options, 2); // indent with two spaces
            if (string.IsNullOrWhiteSpace(errors))
                return;

            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); }
        }