@@ -109,6 +109,7 @@ ComparableOption ToComparableOption(Specification spec, int index)
109109 private bool addEnumValuesToHelpText ;
110110 private bool autoHelp ;
111111 private bool autoVersion ;
112+ private bool addNewLineBetweenHelpSections ;
112113
113114 /// <summary>
114115 /// Initializes a new instance of the <see cref="CommandLine.Text.HelpText"/> class.
@@ -258,6 +259,15 @@ public bool AdditionalNewLineAfterOption
258259 set { additionalNewLineAfterOption = value ; }
259260 }
260261
262+ /// <summary>
263+ /// Gets or sets a value indicating whether to add newlines between help sections.
264+ /// </summary>
265+ public bool AddNewLineBetweenHelpSections
266+ {
267+ get { return addNewLineBetweenHelpSections ; }
268+ set { addNewLineBetweenHelpSections = value ; }
269+ }
270+
261271 /// <summary>
262272 /// Gets or sets a value indicating whether to add the values of an enum after the description of the specification.
263273 /// </summary>
@@ -352,7 +362,11 @@ public static HelpText AutoBuild<T>(
352362 {
353363 var heading = auto . SentenceBuilder . UsageHeadingText ( ) ;
354364 if ( heading . Length > 0 )
365+ {
366+ if ( auto . AddNewLineBetweenHelpSections )
367+ heading = Environment . NewLine + heading ;
355368 auto . AddPreOptionsLine ( heading ) ;
369+ }
356370 }
357371
358372 usageAttr . Do (
@@ -707,19 +721,40 @@ public static IEnumerable<string> RenderUsageTextAsLines<T>(ParserResult<T> pars
707721 public override string ToString ( )
708722 {
709723 const int ExtraLength = 10 ;
710- return
711- new StringBuilder (
712- heading . SafeLength ( ) + copyright . SafeLength ( ) + preOptionsHelp . SafeLength ( ) +
713- optionsHelp . SafeLength ( ) + ExtraLength ) . Append ( heading )
714- . AppendWhen ( ! string . IsNullOrEmpty ( copyright ) , Environment . NewLine , copyright )
715- . AppendWhen ( preOptionsHelp . Length > 0 , Environment . NewLine , preOptionsHelp . ToString ( ) )
716- . AppendWhen (
717- optionsHelp != null && optionsHelp . Length > 0 ,
724+
725+ var sbLength = heading . SafeLength ( ) + copyright . SafeLength ( ) + preOptionsHelp . SafeLength ( )
726+ + optionsHelp . SafeLength ( ) + postOptionsHelp . SafeLength ( ) + ExtraLength ;
727+ var result = new StringBuilder ( sbLength ) ;
728+
729+ result . Append ( heading )
730+ . AppendWhen ( ! string . IsNullOrEmpty ( copyright ) ,
731+ Environment . NewLine ,
732+ copyright )
733+ . AppendWhen ( preOptionsHelp . SafeLength ( ) > 0 ,
734+ NewLineIfNeededBefore ( preOptionsHelp ) ,
735+ Environment . NewLine ,
736+ preOptionsHelp . ToString ( ) )
737+ . AppendWhen ( optionsHelp . SafeLength ( ) > 0 ,
718738 Environment . NewLine ,
719739 Environment . NewLine ,
720740 optionsHelp . SafeToString ( ) )
721- . AppendWhen ( postOptionsHelp . Length > 0 , Environment . NewLine , postOptionsHelp . ToString ( ) )
722- . ToString ( ) ;
741+ . AppendWhen ( postOptionsHelp . SafeLength ( ) > 0 ,
742+ NewLineIfNeededBefore ( postOptionsHelp ) ,
743+ Environment . NewLine ,
744+ postOptionsHelp . ToString ( ) ) ;
745+
746+ string NewLineIfNeededBefore ( StringBuilder sb )
747+ {
748+ if ( AddNewLineBetweenHelpSections
749+ && result . Length > 0
750+ && ! result . SafeEndsWith ( Environment . NewLine )
751+ && ! sb . SafeStartsWith ( Environment . NewLine ) )
752+ return Environment . NewLine ;
753+ else
754+ return null ;
755+ }
756+
757+ return result . ToString ( ) ;
723758 }
724759
725760 internal static void AddLine ( StringBuilder builder , string value , int maximumLength )
0 commit comments