BadScript 2
Loading...
Searching...
No Matches
CommandLine.Text.HelpText Class Reference

Public Member Functions

 HelpText ()
 Initializes a new instance of the CommandLine.Text.HelpText class.
 
 HelpText (SentenceBuilder sentenceBuilder)
 Initializes a new instance of the CommandLine.Text.HelpText class specifying the sentence builder.
 
 HelpText (string heading)
 Initializes a new instance of the CommandLine.Text.HelpText class specifying heading string.
 
 HelpText (SentenceBuilder sentenceBuilder, string heading)
 Initializes a new instance of the CommandLine.Text.HelpText class specifying the sentence builder and heading string.
 
 HelpText (string heading, string copyright)
 Initializes a new instance of the CommandLine.Text.HelpText class specifying heading and copyright strings.
 
 HelpText (SentenceBuilder sentenceBuilder, string heading, string copyright)
 Initializes a new instance of the CommandLine.Text.HelpText class specifying heading and copyright strings.
 
HelpText AddPreOptionsLine (string value)
 Adds a text line after copyright and before options usage strings.
 
HelpText AddPostOptionsLine (string value)
 Adds a text line at the bottom, after options usage string.
 
HelpText AddPreOptionsLines (IEnumerable< string > lines)
 Adds text lines after copyright and before options usage strings.
 
HelpText AddPostOptionsLines (IEnumerable< string > lines)
 Adds text lines at the bottom, after options usage string.
 
HelpText AddPreOptionsText (string text)
 Adds a text block of lines after copyright and before options usage strings.
 
HelpText AddPostOptionsText (string text)
 Adds a text block of lines at the bottom, after options usage string.
 
HelpText AddOptions< T > (ParserResult< T > result)
 Adds a text block with options usage string.
 
HelpText AddVerbs (params Type[] types)
 Adds a text block with verbs usage string.
 
HelpText AddOptions< T > (int maximumLength, ParserResult< T > result)
 Adds a text block with options usage string.
 
HelpText AddVerbs (int maximumLength, params Type[] types)
 Adds a text block with verbs usage string.
 
override string ToString ()
 Returns the help screen as a System.String.
 

Static Public Member Functions

static HelpText AutoBuild< T > (ParserResult< T > parserResult, Func< HelpText, HelpText > onError, Func< Example, Example > onExample, bool verbsIndex=false, int maxDisplayWidth=DefaultMaximumLength)
 Creates a new instance of the CommandLine.Text.HelpText class using common defaults.
 
static HelpText AutoBuild< T > (ParserResult< T > parserResult, int maxDisplayWidth=DefaultMaximumLength)
 Creates a default instance of the CommandLine.Text.HelpText class, automatically handling verbs or options scenario.
 
static HelpText AutoBuild< T > (ParserResult< T > parserResult, Func< HelpText, HelpText > onError, int maxDisplayWidth=DefaultMaximumLength)
 Creates a custom instance of the CommandLine.Text.HelpText class, automatically handling verbs or options scenario.
 
static HelpText DefaultParsingErrorsHandler< T > (ParserResult< T > parserResult, HelpText current)
 Supplies a default parsing error handler implementation.
 
static implicit operator string (HelpText info)
 Converts the help instance to a System.String.
 
static string RenderParsingErrorsText< T > (ParserResult< T > parserResult, Func< Error, string > formatError, Func< IEnumerable< MutuallyExclusiveSetError >, string > formatMutuallyExclusiveSetErrors, int indent)
 Builds a string that contains a parsing error message.
 
static IEnumerable< string > RenderParsingErrorsTextAsLines< T > (ParserResult< T > parserResult, Func< Error, string > formatError, Func< IEnumerable< MutuallyExclusiveSetError >, string > formatMutuallyExclusiveSetErrors, int indent)
 Builds a sequence of string that contains a parsing error message.
 
static string RenderUsageText< T > (ParserResult< T > parserResult)
 Builds a string with usage text block created using CommandLine.Text.UsageAttribute data and metadata.
 
static string RenderUsageText< T > (ParserResult< T > parserResult, Func< Example, Example > mapperFunc)
 Builds a string with usage text block created using CommandLine.Text.UsageAttribute data and metadata.
 
static IEnumerable< string > RenderUsageTextAsLines< T > (ParserResult< T > parserResult, Func< Example, Example > mapperFunc)
 Builds a string sequence with usage text block created using CommandLine.Text.UsageAttribute data and metadata.
 

Static Public Attributes

static Comparison< ComparableOptionRequiredThenAlphaComparison
 

Static Package Functions

static void AddLine (StringBuilder builder, string value, int maximumLength)
 

Properties

string Heading [get, set]
 Gets or sets the heading string. You can directly assign a CommandLine.Text.HeadingInfo instance.
 
string Copyright [get, set]
 Gets or sets the copyright string. You can directly assign a CommandLine.Text.CopyrightInfo instance.
 
int MaximumDisplayWidth [get, set]
 Gets or sets the maximum width of the display. This determines word wrap when displaying the text.
 
bool AddDashesToOption [get, set]
 Gets or sets a value indicating whether the format of options should contain dashes. It modifies behavior of AddOptions<T>(ParserResult<T>) method.
 
bool AdditionalNewLineAfterOption [get, set]
 Gets or sets a value indicating whether to add an additional line after the description of the specification.
 
bool AddNewLineBetweenHelpSections [get, set]
 Gets or sets a value indicating whether to add newlines between help sections.
 
bool AddEnumValuesToHelpText [get, set]
 Gets or sets a value indicating whether to add the values of an enum after the description of the specification.
 
bool AutoHelp [get, set]
 Gets or sets a value indicating whether implicit option or verb 'help' should be supported.
 
bool AutoVersion [get, set]
 Gets or sets a value indicating whether implicit option or verb 'version' should be supported.
 
SentenceBuilder SentenceBuilder [get]
 Gets the SentenceBuilder instance specified in constructor.
 
Comparison< ComparableOptionOptionComparison = null [get, set]
 

Private Member Functions

IEnumerable< SpecificationGetSpecificationsFromType (Type type)
 
IEnumerable< SpecificationAdaptVerbsToSpecifications (IEnumerable< Type > types)
 
HelpText AddOptionsImpl (IEnumerable< Specification > specifications, string requiredWord, string optionGroupWord, int maximumLength)
 
OptionSpecification MakeHelpEntry ()
 
OptionSpecification MakeVersionEntry ()
 
HelpText AddPreOptionsLine (string value, int maximumLength)
 
HelpText AddOption (string requiredWord, string optionGroupWord, int maxLength, Specification specification, int widthOfHelpText)
 
string AddOptionName (int maxLength, OptionSpecification specification)
 
string AddValueName (int maxLength, ValueSpecification specification)
 
HelpText AddLine (StringBuilder builder, string value)
 
int GetMaxLength (IEnumerable< Specification > specifications)
 
int GetMaxOptionLength (OptionSpecification spec)
 
int GetMaxValueLength (ValueSpecification spec)
 
ComparableOption ToComparableOption (Specification spec, int index)
 

Static Private Member Functions

static Maybe< Tuple< UsageAttribute, IEnumerable< Example > > > GetUsageFromType (Type type)
 
static string FormatDefaultValue< T > (T value)
 

Private Attributes

readonly StringBuilder postOptionsHelp
 
readonly StringBuilder preOptionsHelp
 
string copyright
 
string heading
 
StringBuilder optionsHelp
 

Static Private Attributes

const int BuilderCapacity = 128
 
const int DefaultMaximumLength = 80
 
const int OptionToHelpTextSeparatorWidth = 4
 The number of spaces between an option and its associated help text.
 
const int OptionPrefixWidth = 2
 The width of the option prefix (either "--" or " ".
 
const int TotalOptionPadding = OptionToHelpTextSeparatorWidth + OptionPrefixWidth
 The total amount of extra space that needs to accounted for when indenting Option help text.
 

Detailed Description

Definition at line 32 of file HelpText.cs.

Constructor & Destructor Documentation

◆ HelpText() [1/6]

CommandLine.Text.HelpText.HelpText ( )

Initializes a new instance of the CommandLine.Text.HelpText class.

Definition at line 61 of file HelpText.cs.

62 : this(SentenceBuilder.Create(), string.Empty, string.Empty) { }
SentenceBuilder SentenceBuilder
Gets the SentenceBuilder instance specified in constructor.
Definition HelpText.cs:227
static SentenceBuilder Create()
Create instance of CommandLine.Text.SentenceBuilder,.

◆ HelpText() [2/6]

CommandLine.Text.HelpText.HelpText ( SentenceBuilder  sentenceBuilder)

Initializes a new instance of the CommandLine.Text.HelpText class specifying the sentence builder.

Parameters
sentenceBuilderA SentenceBuilder instance.

Definition at line 71 of file HelpText.cs.

72 : this(sentenceBuilder, string.Empty, string.Empty) { }

◆ HelpText() [3/6]

CommandLine.Text.HelpText.HelpText ( string  heading)

Initializes a new instance of the CommandLine.Text.HelpText class specifying heading string.

Parameters
headingAn heading string or an instance of CommandLine.Text.HeadingInfo.
Exceptions
System.ArgumentExceptionThrown when parameter heading is null or empty string.

Definition at line 80 of file HelpText.cs.

81 : this(SentenceBuilder.Create(), heading, string.Empty) { }

◆ HelpText() [4/6]

CommandLine.Text.HelpText.HelpText ( SentenceBuilder  sentenceBuilder,
string  heading 
)

Initializes a new instance of the CommandLine.Text.HelpText class specifying the sentence builder and heading string.

Parameters
sentenceBuilderA SentenceBuilder instance.
headingA string with heading or an instance of CommandLine.Text.HeadingInfo.

Definition at line 89 of file HelpText.cs.

90 : this(sentenceBuilder, heading, string.Empty) { }

◆ HelpText() [5/6]

CommandLine.Text.HelpText.HelpText ( string  heading,
string  copyright 
)

Initializes a new instance of the CommandLine.Text.HelpText class specifying heading and copyright strings.

Parameters
headingA string with heading or an instance of CommandLine.Text.HeadingInfo.
copyrightA string with copyright or an instance of CommandLine.Text.CopyrightInfo.
Exceptions
System.ArgumentNullExceptionThrown when one or more parameters are null or empty strings.

Definition at line 99 of file HelpText.cs.

◆ HelpText() [6/6]

CommandLine.Text.HelpText.HelpText ( SentenceBuilder  sentenceBuilder,
string  heading,
string  copyright 
)

Initializes a new instance of the CommandLine.Text.HelpText class specifying heading and copyright strings.

Parameters
sentenceBuilderA SentenceBuilder instance.
headingA string with heading or an instance of CommandLine.Text.HeadingInfo.
copyrightA string with copyright or an instance of CommandLine.Text.CopyrightInfo.
Exceptions
System.ArgumentNullExceptionThrown when one or more parameters are null or empty strings.

Definition at line 110 of file HelpText.cs.

111 {
112 if (sentenceBuilder == null)
113 {
114 throw new ArgumentNullException("sentenceBuilder");
115 }
116
117 if (heading == null)
118 {
119 throw new ArgumentNullException("heading");
120 }
121
122 if (copyright == null)
123 {
124 throw new ArgumentNullException("copyright");
125 }
126
127 preOptionsHelp = new StringBuilder(BuilderCapacity);
128 postOptionsHelp = new StringBuilder(BuilderCapacity);
129
130 try
131 {
132 MaximumDisplayWidth = Console.WindowWidth;
133
134 if (MaximumDisplayWidth < 1)
135 {
137 }
138 }
139 catch (IOException)
140 {
142 }
143
144 SentenceBuilder = sentenceBuilder;
145 this.heading = heading;
146 this.copyright = copyright;
147 AutoHelp = true;
148 AutoVersion = true;
149 }
readonly StringBuilder preOptionsHelp
Definition HelpText.cs:53
readonly StringBuilder postOptionsHelp
Definition HelpText.cs:52
const int DefaultMaximumLength
Definition HelpText.cs:35
bool AutoVersion
Gets or sets a value indicating whether implicit option or verb 'version' should be supported.
Definition HelpText.cs:222
int MaximumDisplayWidth
Gets or sets the maximum width of the display. This determines word wrap when displaying the text.
Definition HelpText.cs:191
bool AutoHelp
Gets or sets a value indicating whether implicit option or verb 'help' should be supported.
Definition HelpText.cs:217

Member Function Documentation

◆ AdaptVerbsToSpecifications()

IEnumerable< Specification > CommandLine.Text.HelpText.AdaptVerbsToSpecifications ( IEnumerable< Type >  types)
private

Definition at line 949 of file HelpText.cs.

950 {
951 IEnumerable<OptionSpecification> optionSpecs = from verbTuple in Verb.SelectFromTypes(types)
952 select
953 OptionSpecification.NewSwitch(string.Empty,
954 verbTuple.Item1.Name.Concat(verbTuple.Item1.Aliases)
955 .ToDelimitedString(", "),
956 false,
957 verbTuple.Item1.IsDefault
958 ? "(Default Verb) " + verbTuple.Item1.HelpText
959 : verbTuple.Item1.HelpText, //Default verb
960 string.Empty,
961 verbTuple.Item1.Hidden
962 );
963
964 if (AutoHelp)
965 {
966 optionSpecs = optionSpecs.Concat(new[] { MakeHelpEntry() });
967 }
968
969 if (AutoVersion)
970 {
971 optionSpecs = optionSpecs.Concat(new[] { MakeVersionEntry() });
972 }
973
974 return optionSpecs;
975 }
static OptionSpecification NewSwitch(string shortName, string longName, bool required, string helpText, string metaValue, bool hidden=false)
static IEnumerable< Tuple< Verb, Type > > SelectFromTypes(IEnumerable< Type > types)
Definition Verb.cs:47
OptionSpecification MakeVersionEntry()
Definition HelpText.cs:1029
OptionSpecification MakeHelpEntry()
Definition HelpText.cs:1019
HelpText()
Initializes a new instance of the CommandLine.Text.HelpText class.
Definition HelpText.cs:61

◆ AddLine() [1/2]

HelpText CommandLine.Text.HelpText.AddLine ( StringBuilder  builder,
string  value 
)
private

Definition at line 1161 of file HelpText.cs.

1162 {
1163 AddLine(builder, value, MaximumDisplayWidth);
1164
1165 return this;
1166 }
static void AddLine(StringBuilder builder, string value, int maximumLength)
Definition HelpText.cs:879

◆ AddLine() [2/2]

static void CommandLine.Text.HelpText.AddLine ( StringBuilder  builder,
string  value,
int  maximumLength 
)
staticpackage

Definition at line 879 of file HelpText.cs.

880 {
881 if (builder == null)
882 {
883 throw new ArgumentNullException(nameof(builder));
884 }
885
886 if (value == null)
887 {
888 throw new ArgumentNullException(nameof(value));
889 }
890
891 if (maximumLength < 1)
892 {
893 throw new ArgumentOutOfRangeException(nameof(value));
894 }
895
896 value = value.TrimEnd();
897
898 builder.AppendWhen(builder.Length > 0, Environment.NewLine);
899 builder.Append(TextWrapper.WrapAndIndentText(value, 0, maximumLength));
900 }

◆ AddOption()

HelpText CommandLine.Text.HelpText.AddOption ( string  requiredWord,
string  optionGroupWord,
int  maxLength,
Specification  specification,
int  widthOfHelpText 
)
private

Definition at line 1046 of file HelpText.cs.

1051 {
1052 OptionSpecification GetOptionGroupSpecification()
1053 {
1054 if (specification.Tag == SpecificationType.Option &&
1055 specification is OptionSpecification optionSpecification &&
1056 optionSpecification.Group.Length > 0
1057 )
1058
1059 {
1060 return optionSpecification;
1061 }
1062
1063 return null;
1064 }
1065
1066 if (specification.Hidden)
1067 {
1068 return this;
1069 }
1070
1071 optionsHelp.Append(" ");
1072
1073 StringBuilder name = new StringBuilder(maxLength)
1074 .BimapIf(specification.Tag == SpecificationType.Option,
1075 it => it.Append(AddOptionName(maxLength, (OptionSpecification)specification)),
1076 it => it.Append(AddValueName(maxLength, (ValueSpecification)specification))
1077 );
1078
1080 .Append(name.Length < maxLength
1081 ? name.ToString()
1082 .PadRight(maxLength)
1083 : name.ToString()
1084 )
1086
1087 string optionHelpText = specification.HelpText;
1088
1089 if (AddEnumValuesToHelpText && specification.EnumValues.Any())
1090 {
1091 optionHelpText += " Valid values: " + string.Join(", ", specification.EnumValues);
1092 }
1093
1094 specification.DefaultValue.Do(defaultValue =>
1095 optionHelpText =
1096 "(Default: {0}) ".FormatInvariant(FormatDefaultValue(defaultValue)) +
1097 optionHelpText
1098 );
1099
1100 OptionSpecification optionGroupSpecification = GetOptionGroupSpecification();
1101
1102 if (specification.Required && optionGroupSpecification == null)
1103 {
1104 optionHelpText = "{0} ".FormatInvariant(requiredWord) + optionHelpText;
1105 }
1106
1107 if (optionGroupSpecification != null)
1108 {
1109 optionHelpText = "({0}: {1}) ".FormatInvariant(optionGroupWord, optionGroupSpecification.Group) +
1110 optionHelpText;
1111 }
1112
1113 //note that we need to indent trim the start of the string because it's going to be
1114 //appended to an existing line that is as long as the indent-level
1115 string indented = TextWrapper
1116 .WrapAndIndentText(optionHelpText, maxLength + TotalOptionPadding, widthOfHelpText)
1117 .TrimStart();
1118
1120 .Append(indented)
1121 .Append(Environment.NewLine)
1122 .AppendWhen(AdditionalNewLineAfterOption, Environment.NewLine);
1123
1124 return this;
1125 }
IEnumerable< string > EnumValues
const int TotalOptionPadding
The total amount of extra space that needs to accounted for when indenting Option help text.
Definition HelpText.cs:50
bool AddEnumValuesToHelpText
Gets or sets a value indicating whether to add the values of an enum after the description of the spe...
Definition HelpText.cs:212
string AddValueName(int maxLength, ValueSpecification specification)
Definition HelpText.cs:1147
string AddOptionName(int maxLength, OptionSpecification specification)
Definition HelpText.cs:1127
const int OptionToHelpTextSeparatorWidth
The number of spaces between an option and its associated help text.
Definition HelpText.cs:40
bool AdditionalNewLineAfterOption
Gets or sets a value indicating whether to add an additional line after the description of the specif...
Definition HelpText.cs:202
override string ToString()
Returns the help screen as a System.String.
Definition HelpText.cs:830
StringBuilder optionsHelp
Definition HelpText.cs:56

◆ AddOptionName()

string CommandLine.Text.HelpText.AddOptionName ( int  maxLength,
OptionSpecification  specification 
)
private

Definition at line 1127 of file HelpText.cs.

1128 {
1129 return
1130 new StringBuilder(maxLength)
1131 .MapIf(specification.ShortName.Length > 0,
1132 it => it
1133 .AppendWhen(AddDashesToOption, '-')
1134 .AppendFormat("{0}", specification.ShortName)
1135 .AppendFormatWhen(specification.MetaValue.Length > 0, " {0}", specification.MetaValue)
1136 .AppendWhen(specification.LongName.Length > 0, ", ")
1137 )
1138 .MapIf(specification.LongName.Length > 0,
1139 it => it
1140 .AppendWhen(AddDashesToOption, "--")
1141 .AppendFormat("{0}", specification.LongName)
1142 .AppendFormatWhen(specification.MetaValue.Length > 0, "={0}", specification.MetaValue)
1143 )
1144 .ToString();
1145 }
bool AddDashesToOption
Gets or sets a value indicating whether the format of options should contain dashes....
Definition HelpText.cs:197

◆ AddOptions< T >() [1/2]

HelpText CommandLine.Text.HelpText.AddOptions< T > ( int  maximumLength,
ParserResult< T >  result 
)

Adds a text block with options usage string.

Parameters
maximumLengthThe maximum length of the help screen.
resultA parsing computation result.
Exceptions
System.ArgumentNullExceptionThrown when parameter result is null.

Definition at line 614 of file HelpText.cs.

615 {
616 if (result == null)
617 {
618 throw new ArgumentNullException("result");
619 }
620
621 return AddOptionsImpl(GetSpecificationsFromType(result.TypeInfo.Current),
624 maximumLength
625 );
626 }
HelpText AddOptionsImpl(IEnumerable< Specification > specifications, string requiredWord, string optionGroupWord, int maximumLength)
Definition HelpText.cs:977
IEnumerable< Specification > GetSpecificationsFromType(Type type)
Definition HelpText.cs:902
Func< string > RequiredWord
Gets a delegate that returns the word 'required'.
Func< string > OptionGroupWord
Gets a delegate that returns the word 'group'.

◆ AddOptions< T >() [2/2]

HelpText CommandLine.Text.HelpText.AddOptions< T > ( ParserResult< T >  result)

Adds a text block with options usage string.

Parameters
resultA parsing computation result.
Exceptions
System.ArgumentNullExceptionThrown when parameter result is null.

Definition at line 569 of file HelpText.cs.

570 {
571 if (result == null)
572 {
573 throw new ArgumentNullException("result");
574 }
575
576 return AddOptionsImpl(GetSpecificationsFromType(result.TypeInfo.Current),
580 );
581 }

◆ AddOptionsImpl()

HelpText CommandLine.Text.HelpText.AddOptionsImpl ( IEnumerable< Specification specifications,
string  requiredWord,
string  optionGroupWord,
int  maximumLength 
)
private

Definition at line 977 of file HelpText.cs.

981 {
982 int maxLength = GetMaxLength(specifications);
983
984 optionsHelp = new StringBuilder(BuilderCapacity);
985
986 int remainingSpace = maximumLength - (maxLength + TotalOptionPadding);
987
988 if (OptionComparison != null)
989 {
990 int i = -1;
991
992 List<ComparableOption> comparables = specifications.ToList()
993 .Select(s =>
994 {
995 i++;
996
997 return ToComparableOption(s, i);
998 }
999 )
1000 .ToList();
1001 comparables.Sort(OptionComparison);
1002
1003 foreach (ComparableOption comparable in comparables)
1004 {
1005 Specification spec = specifications.ElementAt(comparable.Index);
1006 AddOption(requiredWord, optionGroupWord, maxLength, spec, remainingSpace);
1007 }
1008 }
1009 else
1010 {
1011 specifications.ForEach(option =>
1012 AddOption(requiredWord, optionGroupWord, maxLength, option, remainingSpace)
1013 );
1014 }
1015
1016 return this;
1017 }
Comparison< ComparableOption > OptionComparison
Definition HelpText.cs:1320
HelpText AddOption(string requiredWord, string optionGroupWord, int maxLength, Specification specification, int widthOfHelpText)
Definition HelpText.cs:1046
int GetMaxLength(IEnumerable< Specification > specifications)
Definition HelpText.cs:1168
ComparableOption ToComparableOption(Specification spec, int index)
Definition HelpText.cs:1302

◆ AddPostOptionsLine()

HelpText CommandLine.Text.HelpText.AddPostOptionsLine ( string  value)

Adds a text line at the bottom, after options usage string.

Parameters
valueA System.String instance.
Returns
Updated CommandLine.Text.HelpText instance.
Exceptions
System.ArgumentNullExceptionThrown when parameter value is null or empty string.

Definition at line 505 of file HelpText.cs.

506 {
507 return AddLine(postOptionsHelp, value);
508 }

◆ AddPostOptionsLines()

HelpText CommandLine.Text.HelpText.AddPostOptionsLines ( IEnumerable< string >  lines)

Adds text lines at the bottom, after options usage string.

Parameters
linesA System.String sequence of line to add.
Returns
Updated CommandLine.Text.HelpText instance.

Definition at line 527 of file HelpText.cs.

528 {
529 lines.ForEach(line => AddPostOptionsLine(line));
530
531 return this;
532 }
HelpText AddPostOptionsLine(string value)
Adds a text line at the bottom, after options usage string.
Definition HelpText.cs:505

◆ AddPostOptionsText()

HelpText CommandLine.Text.HelpText.AddPostOptionsText ( string  text)

Adds a text block of lines at the bottom, after options usage string.

Parameters
textA System.String text block.
Returns
Updated CommandLine.Text.HelpText instance.

Definition at line 554 of file HelpText.cs.

555 {
556 string[] lines = text.Split(new[] { Environment.NewLine },
557 StringSplitOptions.None
558 );
559 lines.ForEach(line => AddPostOptionsLine(line));
560
561 return this;
562 }

◆ AddPreOptionsLine() [1/2]

HelpText CommandLine.Text.HelpText.AddPreOptionsLine ( string  value)

Adds a text line after copyright and before options usage strings.

Parameters
valueA System.String instance.
Returns
Updated CommandLine.Text.HelpText instance.
Exceptions
System.ArgumentNullExceptionThrown when parameter value is null or empty string.

Definition at line 494 of file HelpText.cs.

495 {
497 }
HelpText AddPreOptionsLine(string value)
Adds a text line after copyright and before options usage strings.
Definition HelpText.cs:494

◆ AddPreOptionsLine() [2/2]

HelpText CommandLine.Text.HelpText.AddPreOptionsLine ( string  value,
int  maximumLength 
)
private

Definition at line 1039 of file HelpText.cs.

1040 {
1041 AddLine(preOptionsHelp, value, maximumLength);
1042
1043 return this;
1044 }

◆ AddPreOptionsLines()

HelpText CommandLine.Text.HelpText.AddPreOptionsLines ( IEnumerable< string >  lines)

Adds text lines after copyright and before options usage strings.

Parameters
linesA System.String sequence of line to add.
Returns
Updated CommandLine.Text.HelpText instance.

Definition at line 515 of file HelpText.cs.

516 {
517 lines.ForEach(line => AddPreOptionsLine(line));
518
519 return this;
520 }

◆ AddPreOptionsText()

HelpText CommandLine.Text.HelpText.AddPreOptionsText ( string  text)

Adds a text block of lines after copyright and before options usage strings.

Parameters
textA System.String text block.
Returns
Updated CommandLine.Text.HelpText instance.

Definition at line 539 of file HelpText.cs.

540 {
541 string[] lines = text.Split(new[] { Environment.NewLine },
542 StringSplitOptions.None
543 );
544 lines.ForEach(line => AddPreOptionsLine(line));
545
546 return this;
547 }

◆ AddValueName()

string CommandLine.Text.HelpText.AddValueName ( int  maxLength,
ValueSpecification  specification 
)
private

Definition at line 1147 of file HelpText.cs.

1148 {
1149 return new StringBuilder(maxLength)
1150 .BimapIf(specification.MetaName.Length > 0,
1151 it => it.AppendFormat("{0} (pos. {1})", specification.MetaName, specification.Index),
1152 it => it.AppendFormat("value pos. {0}", specification.Index)
1153 )
1154 .AppendFormatWhen(specification.MetaValue.Length > 0,
1155 " {0}",
1156 specification.MetaValue
1157 )
1158 .ToString();
1159 }

◆ AddVerbs() [1/2]

HelpText CommandLine.Text.HelpText.AddVerbs ( int  maximumLength,
params Type[]  types 
)

Adds a text block with verbs usage string.

Parameters
maximumLengthThe maximum length of the help screen.
typesThe array of System.Type with verb commands.
Exceptions
System.ArgumentNullExceptionThrown when parameter types is null.
System.ArgumentOutOfRangeExceptionThrown if types array is empty.

Definition at line 635 of file HelpText.cs.

636 {
637 if (types == null)
638 {
639 throw new ArgumentNullException("types");
640 }
641
642 if (types.Length == 0)
643 {
644 throw new ArgumentOutOfRangeException("types");
645 }
646
650 maximumLength
651 );
652 }
IEnumerable< Specification > AdaptVerbsToSpecifications(IEnumerable< Type > types)
Definition HelpText.cs:949

◆ AddVerbs() [2/2]

HelpText CommandLine.Text.HelpText.AddVerbs ( params Type[]  types)

Adds a text block with verbs usage string.

Parameters
typesThe array of System.Type with verb commands.
Exceptions
System.ArgumentNullExceptionThrown when parameter types is null.
System.ArgumentOutOfRangeExceptionThrown if types array is empty.

Definition at line 589 of file HelpText.cs.

590 {
591 if (types == null)
592 {
593 throw new ArgumentNullException("types");
594 }
595
596 if (types.Length == 0)
597 {
598 throw new ArgumentOutOfRangeException("types");
599 }
600
605 );
606 }

◆ AutoBuild< T >() [1/3]

static HelpText CommandLine.Text.HelpText.AutoBuild< T > ( ParserResult< T >  parserResult,
Func< HelpText, HelpText onError,
Func< Example, Example onExample,
bool  verbsIndex = false,
int  maxDisplayWidth = DefaultMaximumLength 
)
static

Creates a new instance of the CommandLine.Text.HelpText class using common defaults.

Returns
An instance of CommandLine.Text.HelpText class.
Parameters
parserResultThe CommandLine.ParserResult<T> containing the instance that collected command line arguments parsed with CommandLine.Parser class.
onErrorA delegate used to customize the text block of reporting parsing errors text block.
onExampleA delegate used to customize CommandLine.Text.Example model used to render text block of usage examples.
verbsIndexIf true the output style is consistent with verb commands (no dashes), otherwise it outputs options.
maxDisplayWidthThe maximum width of the display.

The parameter verbsIndex is not ontly a metter of formatting, it controls whether to handle verbs or options.

Definition at line 253 of file HelpText.cs.

258 {
259 HelpText auto = new HelpText
260 {
261 Heading = HeadingInfo.Empty,
262 Copyright = CopyrightInfo.Empty,
264 AddDashesToOption = !verbsIndex,
265 MaximumDisplayWidth = maxDisplayWidth,
266 };
267
268 try
269 {
270 auto.Heading = HeadingInfo.Default;
271 auto.Copyright = CopyrightInfo.Default;
272 }
273 catch (Exception)
274 {
275 auto = onError(auto);
276 }
277
278 IEnumerable<Error> errors = Enumerable.Empty<Error>();
279
280 if (onError != null && parserResult.Tag == ParserResultType.NotParsed)
281 {
282 errors = ((NotParsed<T>)parserResult).Errors;
283
284 if (errors.IsHelp() ||
285 errors.OnlyMeaningfulOnes()
286 .Any())
287 {
288 auto = onError(auto);
289 }
290 }
291
292 ReflectionHelper.GetAttribute<AssemblyLicenseAttribute>()
293 .Do(license => license.AddToHelpText(auto, true));
294
295 Maybe<AssemblyUsageAttribute> usageAttr = ReflectionHelper.GetAttribute<AssemblyUsageAttribute>();
296
297 Maybe<IEnumerable<string>> usageLines = RenderUsageTextAsLines(parserResult, onExample)
298 .ToMaybe();
299
300 if (usageAttr.IsJust() || usageLines.IsJust())
301 {
302 string heading = auto.SentenceBuilder.UsageHeadingText();
303
304 if (heading.Length > 0)
305 {
307 {
308 heading = Environment.NewLine + heading;
309 }
310
311 auto.AddPreOptionsLine(heading);
312 }
313 }
314
315 usageAttr.Do(usage => usage.AddToHelpText(auto, true));
316
317 usageLines.Do(lines => auto.AddPreOptionsLines(lines));
318
319 if ((verbsIndex && parserResult.TypeInfo.Choices.Any()) ||
320 errors.Any(e => e.Tag == ErrorType.NoVerbSelectedError))
321 {
322 auto.AddDashesToOption = false;
323 auto.AddVerbs(parserResult.TypeInfo.Choices.ToArray());
324 }
325 else
326 {
327 auto.AddOptions(parserResult);
328 }
329
330 return auto;
331 }
The Maybe type models an optional value. A value of type Maybe a either contains a value of type a (r...
Definition Maybe.cs:33
HelpText AddPreOptionsLines(IEnumerable< string > lines)
Adds text lines after copyright and before options usage strings.
Definition HelpText.cs:515
string Copyright
Gets or sets the copyright string. You can directly assign a CommandLine.Text.CopyrightInfo instance.
Definition HelpText.cs:174
bool AddNewLineBetweenHelpSections
Gets or sets a value indicating whether to add newlines between help sections.
Definition HelpText.cs:207
string Heading
Gets or sets the heading string. You can directly assign a CommandLine.Text.HeadingInfo instance.
Definition HelpText.cs:156
ParserResultType
Discriminator enumeration of CommandLine.ParserResultType derivates.
ErrorType
Discriminator enumeration of CommandLine.Error derivates.
Definition Error.cs:13

◆ AutoBuild< T >() [2/3]

static HelpText CommandLine.Text.HelpText.AutoBuild< T > ( ParserResult< T >  parserResult,
Func< HelpText, HelpText onError,
int  maxDisplayWidth = DefaultMaximumLength 
)
static

Creates a custom instance of the CommandLine.Text.HelpText class, automatically handling verbs or options scenario.

Parameters
parserResultThe CommandLine.ParserResult<T> containing the instance that collected command line arguments parsed with CommandLine.Parser class.
onErrorA delegate used to customize the text block of reporting parsing errors text block.
maxDisplayWidthThe maximum width of the display.
Returns
An instance of CommandLine.Text.HelpText class.

This feature is meant to be invoked automatically by the parser, setting the HelpWriter property of CommandLine.ParserSettings.

Definition at line 371 of file HelpText.cs.

374 {
375 if (parserResult.Tag != ParserResultType.NotParsed)
376 {
377 throw new ArgumentException("Excepting NotParsed<T> type.", "parserResult");
378 }
379
380 IEnumerable<Error> errors = ((NotParsed<T>)parserResult).Errors;
381
382 if (errors.Any(e => e.Tag == ErrorType.VersionRequestedError))
383 {
384 return new HelpText($"{HeadingInfo.Default}{Environment.NewLine}")
385 {
386 MaximumDisplayWidth = maxDisplayWidth,
387 }.AddPreOptionsLine(Environment.NewLine);
388 }
389
390 if (!errors.Any(e => e.Tag == ErrorType.HelpVerbRequestedError))
391 {
392 return AutoBuild(parserResult,
393 current =>
394 {
395 onError?.Invoke(current);
396
397 return DefaultParsingErrorsHandler(parserResult, current);
398 },
399 e => e,
400 maxDisplayWidth: maxDisplayWidth
401 );
402 }
403
405 .Single();
406
407 NotParsed<object> pr = new NotParsed<object>(TypeInfo.Create(err.Type),
408 new Error[] { err }
409 );
410
411 return err.Matched
412 ? AutoBuild(pr,
413 current =>
414 {
415 onError?.Invoke(current);
416
417 return DefaultParsingErrorsHandler(pr, current);
418 },
419 e => e,
420 maxDisplayWidth: maxDisplayWidth
421 )
422 : AutoBuild(parserResult,
423 current =>
424 {
425 onError?.Invoke(current);
426
427 return DefaultParsingErrorsHandler(parserResult, current);
428 },
429 e => e,
430 true,
431 maxDisplayWidth
432 );
433 }
@ HelpVerbRequestedError
Value of CommandLine.HelpVerbRequestedError type.

◆ AutoBuild< T >() [3/3]

static HelpText CommandLine.Text.HelpText.AutoBuild< T > ( ParserResult< T >  parserResult,
int  maxDisplayWidth = DefaultMaximumLength 
)
static

Creates a default instance of the CommandLine.Text.HelpText class, automatically handling verbs or options scenario.

Parameters
parserResultThe CommandLine.ParserResult<T> containing the instance that collected command line arguments parsed with CommandLine.Parser class.
maxDisplayWidthThe maximum width of the display.
Returns
An instance of CommandLine.Text.HelpText class.

This feature is meant to be invoked automatically by the parser, setting the HelpWriter property of CommandLine.ParserSettings.

Definition at line 349 of file HelpText.cs.

350 {
351 return AutoBuild(parserResult, h => h, maxDisplayWidth);
352 }

◆ DefaultParsingErrorsHandler< T >()

static HelpText CommandLine.Text.HelpText.DefaultParsingErrorsHandler< T > ( ParserResult< T >  parserResult,
HelpText  current 
)
static

Supplies a default parsing error handler implementation.

Parameters
parserResultThe CommandLine.ParserResult<T> containing the instance that collected command line arguments parsed with CommandLine.Parser class.
currentThe CommandLine.Text.HelpText instance.

Definition at line 443 of file HelpText.cs.

444 {
445 if (parserResult == null)
446 {
447 throw new ArgumentNullException("parserResult");
448 }
449
450 if (current == null)
451 {
452 throw new ArgumentNullException("current");
453 }
454
455 if (((NotParsed<T>)parserResult).Errors.OnlyMeaningfulOnes()
456 .Empty())
457 {
458 return current;
459 }
460
461 IEnumerable<string> errors = RenderParsingErrorsTextAsLines(parserResult,
462 current.SentenceBuilder.FormatError,
463 current.SentenceBuilder
464 .FormatMutuallyExclusiveSetErrors,
465 2
466 ); // indent with two spaces
467
468 if (errors.Empty())
469 {
470 return current;
471 }
472
473 return current
474 .AddPreOptionsLine(string.Concat(Environment.NewLine, current.SentenceBuilder.ErrorsHeadingText()))
475 .AddPreOptionsLines(errors);
476 }

◆ FormatDefaultValue< T >()

static string CommandLine.Text.HelpText.FormatDefaultValue< T > ( value)
staticprivate

Definition at line 1266 of file HelpText.cs.

1267 {
1268 if (value is bool)
1269 {
1270 return value.ToStringLocal()
1271 .ToLowerInvariant();
1272 }
1273
1274 if (value is string)
1275 {
1276 return value.ToStringLocal();
1277 }
1278
1279 IEnumerable asEnumerable = value as IEnumerable;
1280
1281 if (asEnumerable == null)
1282 {
1283 return value.ToStringLocal();
1284 }
1285
1286 StringBuilder builder = new StringBuilder();
1287
1288 foreach (object item in asEnumerable)
1289 {
1290 builder
1291 .Append(item.ToStringLocal())
1292 .Append(" ");
1293 }
1294
1295 return builder.Length > 0
1296 ? builder.ToString(0, builder.Length - 1)
1297 : string.Empty;
1298 }

◆ GetMaxLength()

int CommandLine.Text.HelpText.GetMaxLength ( IEnumerable< Specification specifications)
private

Definition at line 1168 of file HelpText.cs.

1169 {
1170 return specifications.Aggregate(0,
1171 (length, spec) =>
1172 {
1173 if (spec.Hidden)
1174 {
1175 return length;
1176 }
1177
1178 int specLength = spec.Tag == SpecificationType.Option
1181
1182 return Math.Max(length, specLength);
1183 }
1184 );
1185 }
int GetMaxOptionLength(OptionSpecification spec)
Definition HelpText.cs:1188
int GetMaxValueLength(ValueSpecification spec)
Definition HelpText.cs:1234

◆ GetMaxOptionLength()

int CommandLine.Text.HelpText.GetMaxOptionLength ( OptionSpecification  spec)
private

Definition at line 1188 of file HelpText.cs.

1189 {
1190 int specLength = 0;
1191
1192 bool hasShort = spec.ShortName.Length > 0;
1193 bool hasLong = spec.LongName.Length > 0;
1194
1195 int metaLength = 0;
1196
1197 if (spec.MetaValue.Length > 0)
1198 {
1199 metaLength = spec.MetaValue.Length + 1;
1200 }
1201
1202 if (hasShort)
1203 {
1204 ++specLength;
1205
1207 {
1208 ++specLength;
1209 }
1210
1211 specLength += metaLength;
1212 }
1213
1214 if (hasLong)
1215 {
1216 specLength += spec.LongName.Length;
1217
1219 {
1220 specLength += OptionPrefixWidth;
1221 }
1222
1223 specLength += metaLength;
1224 }
1225
1226 if (hasShort && hasLong)
1227 {
1228 specLength += OptionPrefixWidth;
1229 }
1230
1231 return specLength;
1232 }
const int OptionPrefixWidth
The width of the option prefix (either "--" or " ".
Definition HelpText.cs:45

◆ GetMaxValueLength()

int CommandLine.Text.HelpText.GetMaxValueLength ( ValueSpecification  spec)
private

Definition at line 1234 of file HelpText.cs.

1235 {
1236 int specLength = 0;
1237
1238 bool hasMeta = spec.MetaName.Length > 0;
1239
1240 int metaLength = 0;
1241
1242 if (spec.MetaValue.Length > 0)
1243 {
1244 metaLength = spec.MetaValue.Length + 1;
1245 }
1246
1247 if (hasMeta)
1248 {
1249 specLength += spec.MetaName.Length +
1250 spec.Index.ToStringInvariant()
1251 .Length +
1252 8; //METANAME (pos. N)
1253 }
1254 else
1255 {
1256 specLength += spec.Index.ToStringInvariant()
1257 .Length +
1258 11; // "value pos. N"
1259 }
1260
1261 specLength += metaLength;
1262
1263 return specLength;
1264 }

◆ GetSpecificationsFromType()

IEnumerable< Specification > CommandLine.Text.HelpText.GetSpecificationsFromType ( Type  type)
private

Definition at line 902 of file HelpText.cs.

903 {
904 IEnumerable<Specification> specs = type.GetSpecifications(Specification.FromProperty);
905
906 IEnumerable<OptionSpecification> optionSpecs = specs
907 .OfType<OptionSpecification>();
908
909 if (AutoHelp)
910 {
911 optionSpecs = optionSpecs.Concat(new[] { MakeHelpEntry() });
912 }
913
914 if (AutoVersion)
915 {
916 optionSpecs = optionSpecs.Concat(new[] { MakeVersionEntry() });
917 }
918
919 IOrderedEnumerable<ValueSpecification> valueSpecs = specs
920 .OfType<ValueSpecification>()
921 .OrderBy(v => v.Index);
922
923 return Enumerable.Empty<Specification>()
924 .Concat(optionSpecs)
925 .Concat(valueSpecs);
926 }
static Specification FromProperty(PropertyInfo property)

◆ GetUsageFromType()

static Maybe< Tuple< UsageAttribute, IEnumerable< Example > > > CommandLine.Text.HelpText.GetUsageFromType ( Type  type)
staticprivate

Definition at line 928 of file HelpText.cs.

929 {
930 return type.GetUsageData()
931 .Map(tuple =>
932 {
933 PropertyInfo prop = tuple.Item1;
934 UsageAttribute attr = tuple.Item2;
935
936 IEnumerable<Example> examples = (IEnumerable<Example>)prop
937 .GetValue(null,
938 BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty,
939 null,
940 null,
941 null
942 );
943
944 return Tuple.Create(attr, examples);
945 }
946 );
947 }

◆ MakeHelpEntry()

OptionSpecification CommandLine.Text.HelpText.MakeHelpEntry ( )
private

Definition at line 1019 of file HelpText.cs.

1020 {
1021 return OptionSpecification.NewSwitch(string.Empty,
1022 "help",
1023 false,
1025 string.Empty
1026 );
1027 }
Func< bool, string > HelpCommandText
Get a delegate that returns the help text of help command. The delegates must accept a boolean that i...

◆ MakeVersionEntry()

OptionSpecification CommandLine.Text.HelpText.MakeVersionEntry ( )
private

Definition at line 1029 of file HelpText.cs.

1030 {
1031 return OptionSpecification.NewSwitch(string.Empty,
1032 "version",
1033 false,
1035 string.Empty
1036 );
1037 }
Func< bool, string > VersionCommandText
Get a delegate that returns the help text of vesion command. The delegates must accept a boolean that...

◆ operator string()

static implicit CommandLine.Text.HelpText.operator string ( HelpText  info)
static

Converts the help instance to a System.String.

Parameters
infoThis CommandLine.Text.HelpText instance.
Returns
The System.String that contains the help screen.

Definition at line 483 of file HelpText.cs.

484 {
485 return info.ToString();
486 }

◆ RenderParsingErrorsText< T >()

static string CommandLine.Text.HelpText.RenderParsingErrorsText< T > ( ParserResult< T >  parserResult,
Func< Error, string >  formatError,
Func< IEnumerable< MutuallyExclusiveSetError >, string >  formatMutuallyExclusiveSetErrors,
int  indent 
)
static

Builds a string that contains a parsing error message.

Parameters
parserResultThe CommandLine.ParserResult<T> containing the instance that collected command line arguments parsed with CommandLine.Parser class.
formatErrorThe error formatting delegate.
formatMutuallyExclusiveSetErrorsThe specialized CommandLine.MutuallyExclusiveSetError sequence formatting delegate.
indentNumber of spaces used to indent text.
Returns
The System.String that contains the parsing error message.

Definition at line 668 of file HelpText.cs.

673 {
674 return string.Join(Environment.NewLine,
675 RenderParsingErrorsTextAsLines(parserResult,
676 formatError,
677 formatMutuallyExclusiveSetErrors,
678 indent
679 )
680 );
681 }

◆ RenderParsingErrorsTextAsLines< T >()

static IEnumerable< string > CommandLine.Text.HelpText.RenderParsingErrorsTextAsLines< T > ( ParserResult< T >  parserResult,
Func< Error, string >  formatError,
Func< IEnumerable< MutuallyExclusiveSetError >, string >  formatMutuallyExclusiveSetErrors,
int  indent 
)
static

Builds a sequence of string that contains a parsing error message.

Parameters
parserResultThe CommandLine.ParserResult<T> containing the instance that collected command line arguments parsed with CommandLine.Parser class.
formatErrorThe error formatting delegate.
formatMutuallyExclusiveSetErrorsThe specialized CommandLine.MutuallyExclusiveSetError sequence formatting delegate.
indentNumber of spaces used to indent text.
Returns
A sequence of System.String that contains the parsing error message.

Definition at line 697 of file HelpText.cs.

703 {
704 if (parserResult == null)
705 {
706 throw new ArgumentNullException("parserResult");
707 }
708
709 IEnumerable<Error> meaningfulErrors =
710 ((NotParsed<T>)parserResult).Errors.OnlyMeaningfulOnes();
711
712 if (meaningfulErrors.Empty())
713 {
714 yield break;
715 }
716
717 foreach (Error error in meaningfulErrors
718 .Where(e => e.Tag != ErrorType.MutuallyExclusiveSetError))
719 {
720 StringBuilder line = new StringBuilder(indent.Spaces())
721 .Append(formatError(error));
722
723 yield return line.ToString();
724 }
725
726 string mutuallyErrs =
727 formatMutuallyExclusiveSetErrors(meaningfulErrors.OfType<MutuallyExclusiveSetError>());
728
729 if (mutuallyErrs.Length > 0)
730 {
731 string[] lines = mutuallyErrs
732 .Split(new[] { Environment.NewLine },
733 StringSplitOptions.None
734 );
735
736 foreach (string line in lines)
737 {
738 yield return line;
739 }
740 }
741 }

◆ RenderUsageText< T >() [1/2]

static string CommandLine.Text.HelpText.RenderUsageText< T > ( ParserResult< T >  parserResult)
static

Builds a string with usage text block created using CommandLine.Text.UsageAttribute data and metadata.

Template Parameters
TType of parsing computation result.
Parameters
parserResultA parsing computation result.
Returns
Resulting formatted text.

Definition at line 750 of file HelpText.cs.

751 {
752 return RenderUsageText(parserResult, example => example);
753 }

◆ RenderUsageText< T >() [2/2]

static string CommandLine.Text.HelpText.RenderUsageText< T > ( ParserResult< T >  parserResult,
Func< Example, Example mapperFunc 
)
static

Builds a string with usage text block created using CommandLine.Text.UsageAttribute data and metadata.

Template Parameters
TType of parsing computation result.
Parameters
parserResultA parsing computation result.
mapperFuncA mapping lambda normally used to translate text in other languages.
Returns
Resulting formatted text.

Definition at line 763 of file HelpText.cs.

764 {
765 return string.Join(Environment.NewLine, RenderUsageTextAsLines(parserResult, mapperFunc));
766 }

◆ RenderUsageTextAsLines< T >()

static IEnumerable< string > CommandLine.Text.HelpText.RenderUsageTextAsLines< T > ( ParserResult< T >  parserResult,
Func< Example, Example mapperFunc 
)
static

Builds a string sequence with usage text block created using CommandLine.Text.UsageAttribute data and metadata.

Template Parameters
TType of parsing computation result.
Parameters
parserResultA parsing computation result.
mapperFuncA mapping lambda normally used to translate text in other languages.
Returns
Resulting formatted text.

Definition at line 776 of file HelpText.cs.

778 {
779 if (parserResult == null)
780 {
781 throw new ArgumentNullException("parserResult");
782 }
783
784 Maybe<Tuple<UsageAttribute, IEnumerable<Example>>> usage = GetUsageFromType(parserResult.TypeInfo.Current);
785
786 if (usage.MatchNothing())
787 {
788 yield break;
789 }
790
791 Tuple<UsageAttribute, IEnumerable<Example>> usageTuple = usage.FromJustOrFail();
792 IEnumerable<Example> examples = usageTuple.Item2;
793 string appAlias = usageTuple.Item1.ApplicationAlias ?? ReflectionHelper.GetAssemblyName();
794
795 foreach (Example e in examples)
796 {
797 Example example = mapperFunc(e);
798
799 StringBuilder exampleText = new StringBuilder(example.HelpText)
800 .Append(':');
801
802 yield return exampleText.ToString();
803 IEnumerable<UnParserSettings> styles = example.GetFormatStylesOrDefault();
804
805 foreach (UnParserSettings s in styles)
806 {
807 StringBuilder commandLine = new StringBuilder(OptionPrefixWidth.Spaces())
808 .Append(appAlias)
809 .Append(' ')
810 .Append(Parser.Default.FormatCommandLine(example.Sample,
811 config =>
812 {
813 config.PreferShortName = s.PreferShortName;
814 config.GroupSwitches = s.GroupSwitches;
815 config.UseEqualToken = s.UseEqualToken;
816 config.SkipDefault = s.SkipDefault;
817 }
818 )
819 );
820
821 yield return commandLine.ToString();
822 }
823 }
824 }
bool MatchNothing()
Matches an empty value returning true.
Definition Maybe.cs:59
static Maybe< Tuple< UsageAttribute, IEnumerable< Example > > > GetUsageFromType(Type type)
Definition HelpText.cs:928

◆ ToComparableOption()

ComparableOption CommandLine.Text.HelpText.ToComparableOption ( Specification  spec,
int  index 
)
private

Definition at line 1302 of file HelpText.cs.

1303 {
1305 ValueSpecification value = spec as ValueSpecification;
1306 bool required = option?.Required ?? false;
1307
1308 return new ComparableOption
1309 {
1310 Required = required,
1311 IsOption = option != null,
1312 IsValue = value != null,
1313 LongName = option?.LongName ?? value?.MetaName,
1314 ShortName = option?.ShortName,
1315 Index = index,
1316 };
1317 }

◆ ToString()

override string CommandLine.Text.HelpText.ToString ( )

Returns the help screen as a System.String.

Returns
The System.String that contains the help screen.

Definition at line 830 of file HelpText.cs.

831 {
832 const int ExtraLength = 10;
833
834 int sbLength = heading.SafeLength() +
835 copyright.SafeLength() +
836 preOptionsHelp.SafeLength() +
837 optionsHelp.SafeLength() +
838 postOptionsHelp.SafeLength() +
839 ExtraLength;
840 StringBuilder result = new StringBuilder(sbLength);
841
842 result.Append(heading)
843 .AppendWhen(!string.IsNullOrEmpty(copyright),
844 Environment.NewLine,
846 )
847 .AppendWhen(preOptionsHelp.SafeLength() > 0,
848 NewLineIfNeededBefore(preOptionsHelp),
849 Environment.NewLine,
850 preOptionsHelp.ToString()
851 )
852 .AppendWhen(optionsHelp.SafeLength() > 0,
853 Environment.NewLine,
854 Environment.NewLine,
855 optionsHelp.SafeToString()
856 )
857 .AppendWhen(postOptionsHelp.SafeLength() > 0,
858 NewLineIfNeededBefore(postOptionsHelp),
859 Environment.NewLine,
860 postOptionsHelp.ToString()
861 );
862
863 string NewLineIfNeededBefore(StringBuilder sb)
864 {
866 result.Length > 0 &&
867 !result.SafeEndsWith(Environment.NewLine) &&
868 !sb.SafeStartsWith(Environment.NewLine))
869 {
870 return Environment.NewLine;
871 }
872
873 return null;
874 }
875
876 return result.ToString();
877 }

Member Data Documentation

◆ BuilderCapacity

const int CommandLine.Text.HelpText.BuilderCapacity = 128
staticprivate

Definition at line 34 of file HelpText.cs.

◆ copyright

string CommandLine.Text.HelpText.copyright
private

Definition at line 54 of file HelpText.cs.

◆ DefaultMaximumLength

const int CommandLine.Text.HelpText.DefaultMaximumLength = 80
staticprivate

Definition at line 35 of file HelpText.cs.

◆ heading

string CommandLine.Text.HelpText.heading
private

Definition at line 55 of file HelpText.cs.

◆ OptionPrefixWidth

const int CommandLine.Text.HelpText.OptionPrefixWidth = 2
staticprivate

The width of the option prefix (either "--" or " ".

Definition at line 45 of file HelpText.cs.

◆ optionsHelp

StringBuilder CommandLine.Text.HelpText.optionsHelp
private

Definition at line 56 of file HelpText.cs.

◆ OptionToHelpTextSeparatorWidth

const int CommandLine.Text.HelpText.OptionToHelpTextSeparatorWidth = 4
staticprivate

The number of spaces between an option and its associated help text.

Definition at line 40 of file HelpText.cs.

◆ postOptionsHelp

readonly StringBuilder CommandLine.Text.HelpText.postOptionsHelp
private

Definition at line 52 of file HelpText.cs.

◆ preOptionsHelp

readonly StringBuilder CommandLine.Text.HelpText.preOptionsHelp
private

Definition at line 53 of file HelpText.cs.

◆ RequiredThenAlphaComparison

Comparison<ComparableOption> CommandLine.Text.HelpText.RequiredThenAlphaComparison
static
Initial value:
= (attr1, attr2) =>
{
if (attr1.IsOption && attr2.IsOption)
{
if (attr1.Required && !attr2.Required)
{
return -1;
}
if (!attr1.Required && attr2.Required)
{
return 1;
}
return string.Compare(attr1.LongName, attr2.LongName, StringComparison.Ordinal);
}
if (attr1.IsOption && attr2.IsValue)
{
return -1;
}
return 1;
}

Definition at line 1322 of file HelpText.cs.

1323 {
1324 if (attr1.IsOption && attr2.IsOption)
1325 {
1326 if (attr1.Required && !attr2.Required)
1327 {
1328 return -1;
1329 }
1330
1331 if (!attr1.Required && attr2.Required)
1332 {
1333 return 1;
1334 }
1335
1336 return string.Compare(attr1.LongName, attr2.LongName, StringComparison.Ordinal);
1337 }
1338
1339 if (attr1.IsOption && attr2.IsValue)
1340 {
1341 return -1;
1342 }
1343
1344 return 1;
1345 };

◆ TotalOptionPadding

const int CommandLine.Text.HelpText.TotalOptionPadding = OptionToHelpTextSeparatorWidth + OptionPrefixWidth
staticprivate

The total amount of extra space that needs to accounted for when indenting Option help text.

Definition at line 50 of file HelpText.cs.

Property Documentation

◆ AddDashesToOption

bool CommandLine.Text.HelpText.AddDashesToOption
getset

Gets or sets a value indicating whether the format of options should contain dashes. It modifies behavior of AddOptions<T>(ParserResult<T>) method.

Definition at line 197 of file HelpText.cs.

197{ get; set; }

◆ AddEnumValuesToHelpText

bool CommandLine.Text.HelpText.AddEnumValuesToHelpText
getset

Gets or sets a value indicating whether to add the values of an enum after the description of the specification.

Definition at line 212 of file HelpText.cs.

212{ get; set; }

◆ AdditionalNewLineAfterOption

bool CommandLine.Text.HelpText.AdditionalNewLineAfterOption
getset

Gets or sets a value indicating whether to add an additional line after the description of the specification.

Definition at line 202 of file HelpText.cs.

202{ get; set; }

◆ AddNewLineBetweenHelpSections

bool CommandLine.Text.HelpText.AddNewLineBetweenHelpSections
getset

Gets or sets a value indicating whether to add newlines between help sections.

Definition at line 207 of file HelpText.cs.

207{ get; set; }

◆ AutoHelp

bool CommandLine.Text.HelpText.AutoHelp
getset

Gets or sets a value indicating whether implicit option or verb 'help' should be supported.

Definition at line 217 of file HelpText.cs.

217{ get; set; }

◆ AutoVersion

bool CommandLine.Text.HelpText.AutoVersion
getset

Gets or sets a value indicating whether implicit option or verb 'version' should be supported.

Definition at line 222 of file HelpText.cs.

222{ get; set; }

◆ Copyright

string CommandLine.Text.HelpText.Copyright
getset

Gets or sets the copyright string. You can directly assign a CommandLine.Text.CopyrightInfo instance.

Definition at line 173 of file HelpText.cs.

174 {
175 get => copyright;
176 set
177 {
178 if (value == null)
179 {
180 throw new ArgumentNullException("value");
181 }
182
183 copyright = value;
184 }
185 }

◆ Heading

string CommandLine.Text.HelpText.Heading
getset

Gets or sets the heading string. You can directly assign a CommandLine.Text.HeadingInfo instance.

Definition at line 155 of file HelpText.cs.

156 {
157 get => heading;
158 set
159 {
160 if (value == null)
161 {
162 throw new ArgumentNullException("value");
163 }
164
165 heading = value;
166 }
167 }

◆ MaximumDisplayWidth

int CommandLine.Text.HelpText.MaximumDisplayWidth
getset

Gets or sets the maximum width of the display. This determines word wrap when displaying the text.

The maximum width of the display.

Definition at line 191 of file HelpText.cs.

191{ get; set; }

◆ OptionComparison

Comparison<ComparableOption> CommandLine.Text.HelpText.OptionComparison = null
getset

Definition at line 1320 of file HelpText.cs.

1320{ get; set; } = null;

◆ SentenceBuilder

SentenceBuilder CommandLine.Text.HelpText.SentenceBuilder
get

Gets the SentenceBuilder instance specified in constructor.

Definition at line 227 of file HelpText.cs.

227{ get; }

The documentation for this class was generated from the following file: