/// <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="CommandLine.CommandLineParser"/> class. /// </param> /// <param name='errDelegate'> /// A delegate used to customize the text block for reporting parsing errors. /// </param> public static HelpText AutoBuild(object options, HandleParsingErrorsDelegate errDelegate) { var title = ReflectionUtil.GetAttribute<AssemblyTitleAttribute>(); if (title == null) throw new InvalidOperationException("HelpText::AutoBuild() requires that you define AssemblyTitleAttribute."); var version = ReflectionUtil.GetAttribute<AssemblyInformationalVersionAttribute>(); if (version == null) throw new InvalidOperationException("HelpText::AutoBuild() requires that you define AssemblyInformationalVersionAttribute."); var copyright = ReflectionUtil.GetAttribute<AssemblyCopyrightAttribute>(); if (copyright == null) throw new InvalidOperationException("HelpText::AutoBuild() requires that you define AssemblyCopyrightAttribute."); var auto = new HelpText { Heading = new HeadingInfo(Path.GetFileNameWithoutExtension(title.Title), version.InformationalVersion), Copyright = copyright.Copyright, AdditionalNewLineAfterOption = true, AddDashesToOption = true }; if (errDelegate != null) { var typedTarget = options as CommandLineOptionsBase; if (typedTarget != null) { errDelegate(auto); } } var license = ReflectionUtil.GetAttribute<AssemblyLicenseAttribute>(); if (license != null) { //auto.AddPreOptionsLine(license.FullText); license.AddToHelpText(auto, true); } var usage = ReflectionUtil.GetAttribute<AssemblyUsageAttribute>(); if (usage != null) { //auto.AddPreOptionsLine(usage.FullText); usage.AddToHelpText(auto, true); } auto.AddOptions(options); return auto; }
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); } } } }
/* public string FullText { get { if (string.IsNullOrEmpty(_fullText)) { if (!string.IsNullOrEmpty(_line1)) { var builder = new StringBuilder(_line1); builder.AppendLineIfNotNullOrEmpty(_line2); builder.AppendLineIfNotNullOrEmpty(_line3); builder.AppendLineIfNotNullOrEmpty(_line4); builder.AppendLineIfNotNullOrEmpty(_line5); _fullText = builder.ToString(); } } return _fullText; } } */ internal void AddToHelpText(HelpText helpText, bool before) { if (before) { if (!string.IsNullOrEmpty(_line1)) helpText.AddPreOptionsLine(_line1); if (!string.IsNullOrEmpty(_line2)) helpText.AddPreOptionsLine(_line2); if (!string.IsNullOrEmpty(_line3)) helpText.AddPreOptionsLine(_line3); if (!string.IsNullOrEmpty(_line4)) helpText.AddPreOptionsLine(_line4); if (!string.IsNullOrEmpty(_line5)) helpText.AddPreOptionsLine(_line5); } else { if (!string.IsNullOrEmpty(_line1)) helpText.AddPostOptionsLine(_line1); if (!string.IsNullOrEmpty(_line2)) helpText.AddPostOptionsLine(_line2); if (!string.IsNullOrEmpty(_line3)) helpText.AddPostOptionsLine(_line3); if (!string.IsNullOrEmpty(_line4)) helpText.AddPostOptionsLine(_line4); if (!string.IsNullOrEmpty(_line5)) helpText.AddPostOptionsLine(_line5); } }