@@ -70,6 +70,18 @@ public virtual void Write(ICommand command)
70
70
AddAdditionalArguments ( command ) ;
71
71
}
72
72
73
+ public virtual void Write ( IOption option )
74
+ {
75
+ if ( option is null )
76
+ {
77
+ throw new ArgumentNullException ( nameof ( option ) ) ;
78
+ }
79
+
80
+ var item = GetOptionHelpItems ( option ) . ToList ( ) [ 0 ] ;
81
+
82
+ Console . Out . Write ( $ "{ item . Invocation } { item . Description } ") ;
83
+ }
84
+
73
85
protected int CurrentIndentation => _indentationLevel * IndentationSize ;
74
86
75
87
/// <summary>
@@ -121,7 +133,6 @@ private void AppendBlankLine()
121
133
Console . Out . WriteLine ( ) ;
122
134
}
123
135
124
-
125
136
/// <summary>
126
137
/// Writes whitespace to the console based on the provided offset,
127
138
/// defaulting to the <see cref="CurrentIndentation"/>
@@ -203,7 +214,9 @@ private void AppendDescription(string description)
203
214
/// Maximum number of characters accross all <see cref="HelpItem">help items</see>
204
215
/// occupied by the invocation text
205
216
/// </param>
206
- protected void AppendHelpItem ( HelpItem helpItem , int maxInvocationWidth )
217
+ protected void AppendHelpItem (
218
+ HelpItem helpItem ,
219
+ int maxInvocationWidth )
207
220
{
208
221
if ( helpItem is null )
209
222
{
@@ -346,11 +359,6 @@ protected virtual string DefaultValueHint(IArgument argument, bool isSingleArgum
346
359
_ => ""
347
360
} ;
348
361
349
- /// <summary>
350
- /// Formats the help rows for a given option
351
- /// </summary>
352
- /// <param name="symbol"></param>
353
- /// <returns>A new <see cref="HelpItem"/></returns>
354
362
private IEnumerable < HelpItem > GetOptionHelpItems ( ISymbol symbol )
355
363
{
356
364
var rawAliases = symbol
@@ -416,7 +424,7 @@ protected virtual void AddSynopsis(ICommand command)
416
424
}
417
425
418
426
var title = $ "{ command . Name } :";
419
- HelpSection . Write ( this , title , command . Description ) ;
427
+ HelpSection . WriteHeading ( this , title , command . Description ) ;
420
428
}
421
429
422
430
/// <summary>
@@ -477,7 +485,7 @@ protected virtual void AddUsage(ICommand command)
477
485
usage . Add ( Usage . AdditionalArguments ) ;
478
486
}
479
487
480
- HelpSection . Write ( this , Usage . Title , string . Join ( " " , usage . Where ( u => ! string . IsNullOrWhiteSpace ( u ) ) ) ) ;
488
+ HelpSection . WriteHeading ( this , Usage . Title , string . Join ( " " , usage . Where ( u => ! string . IsNullOrWhiteSpace ( u ) ) ) ) ;
481
489
}
482
490
483
491
private string FormatArgumentUsage ( IReadOnlyCollection < IArgument > arguments )
@@ -553,7 +561,10 @@ protected virtual void AddArguments(ICommand command)
553
561
commands . Add ( command ) ;
554
562
}
555
563
556
- HelpSection . Write ( this , Arguments . Title , commands , GetArgumentHelpItems ) ;
564
+ HelpSection . WriteItems (
565
+ this ,
566
+ Arguments . Title ,
567
+ commands . SelectMany ( GetArgumentHelpItems ) . Distinct ( ) . ToArray ( ) ) ;
557
568
}
558
569
559
570
/// <summary>
@@ -564,12 +575,15 @@ protected virtual void AddArguments(ICommand command)
564
575
protected virtual void AddOptions ( ICommand command )
565
576
{
566
577
var options = command
567
- . Children
568
- . OfType < IOption > ( )
569
- . Where ( ShouldShowHelp )
570
- . ToArray ( ) ;
571
-
572
- HelpSection . Write ( this , Options . Title , options , GetOptionHelpItems ) ;
578
+ . Children
579
+ . OfType < IOption > ( )
580
+ . Where ( ShouldShowHelp )
581
+ . ToArray ( ) ;
582
+
583
+ HelpSection . WriteItems (
584
+ this ,
585
+ Options . Title ,
586
+ options . SelectMany ( GetOptionHelpItems ) . Distinct ( ) . ToArray ( ) ) ;
573
587
}
574
588
575
589
/// <summary>
@@ -580,12 +594,14 @@ protected virtual void AddOptions(ICommand command)
580
594
protected virtual void AddSubcommands ( ICommand command )
581
595
{
582
596
var subcommands = command
583
- . Children
584
- . OfType < ICommand > ( )
585
- . Where ( ShouldShowHelp )
586
- . ToArray ( ) ;
587
-
588
- HelpSection . Write ( this , Commands . Title , subcommands , GetOptionHelpItems ) ;
597
+ . Children
598
+ . OfType < ICommand > ( )
599
+ . Where ( ShouldShowHelp )
600
+ . ToArray ( ) ;
601
+
602
+ HelpSection . WriteItems ( this ,
603
+ Commands . Title ,
604
+ subcommands . SelectMany ( GetOptionHelpItems ) . ToArray ( ) ) ;
589
605
}
590
606
591
607
protected virtual void AddAdditionalArguments ( ICommand command )
@@ -595,7 +611,7 @@ protected virtual void AddAdditionalArguments(ICommand command)
595
611
return ;
596
612
}
597
613
598
- HelpSection . Write ( this , AdditionalArguments . Title , AdditionalArguments . Description ) ;
614
+ HelpSection . WriteHeading ( this , AdditionalArguments . Title , AdditionalArguments . Description ) ;
599
615
}
600
616
601
617
private bool ShouldDisplayArgumentHelp ( ICommand ? command )
@@ -650,12 +666,12 @@ protected bool Equals(HelpItem other) =>
650
666
651
667
private static class HelpSection
652
668
{
653
- public static void Write (
669
+ public static void WriteHeading (
654
670
HelpBuilder builder ,
655
671
string title ,
656
672
string ? description = null )
657
673
{
658
- if ( ! ShouldWrite ( description , null ) )
674
+ if ( ! ShouldWrite ( description , Array . Empty < ISymbol > ( ) ) )
659
675
{
660
676
return ;
661
677
}
@@ -667,34 +683,35 @@ public static void Write(
667
683
builder . AppendBlankLine ( ) ;
668
684
}
669
685
670
- public static void Write (
686
+ public static void WriteItems (
671
687
HelpBuilder builder ,
672
688
string title ,
673
- IReadOnlyCollection < ISymbol > ? usageItems = null ,
674
- Func < ISymbol , IEnumerable < HelpItem > > ? formatter = null ,
689
+ IReadOnlyCollection < HelpItem > usageItems ,
675
690
string ? description = null )
676
691
{
677
- if ( ! ShouldWrite ( description , usageItems ) )
692
+ if ( usageItems . Count == 0 )
678
693
{
679
694
return ;
680
695
}
681
696
682
697
AppendHeading ( builder , title ) ;
683
698
builder . Indent ( ) ;
699
+
684
700
AddDescription ( builder , description ) ;
685
- AddInvocation ( builder , usageItems , formatter ) ;
701
+ AddInvocation ( builder , usageItems ) ;
702
+
686
703
builder . Outdent ( ) ;
687
704
builder . AppendBlankLine ( ) ;
688
705
}
689
706
690
- private static bool ShouldWrite ( string ? description , IReadOnlyCollection < ISymbol > ? usageItems )
707
+ private static bool ShouldWrite ( string ? description , IReadOnlyCollection < ISymbol > usageItems )
691
708
{
692
709
if ( ! string . IsNullOrWhiteSpace ( description ) )
693
710
{
694
711
return true ;
695
712
}
696
713
697
- return usageItems ? . Any ( ) == true ;
714
+ return usageItems . Count > 0 ;
698
715
}
699
716
700
717
private static void AppendHeading ( HelpBuilder builder , string ? title = null )
@@ -719,14 +736,8 @@ private static void AddDescription(HelpBuilder builder, string? description = nu
719
736
720
737
private static void AddInvocation (
721
738
HelpBuilder builder ,
722
- IReadOnlyCollection < ISymbol > ? symbols ,
723
- Func < ISymbol , IEnumerable < HelpItem > > ? formatter )
739
+ IReadOnlyCollection < HelpItem > helpItems )
724
740
{
725
- var helpItems = symbols
726
- . SelectMany ( formatter )
727
- . Distinct ( )
728
- . ToList ( ) ;
729
-
730
741
var maxWidth = helpItems
731
742
. Select ( line => line . Invocation . Length )
732
743
. OrderByDescending ( textLength => textLength )
0 commit comments