Skip to content

Commit c2c0051

Browse files
committed
R: Argument ctor and HelpBuilder cleanup
1 parent 6da8117 commit c2c0051

File tree

5 files changed

+49
-79
lines changed

5 files changed

+49
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
the-root-command:
1+
command:
22
Test description
33

44
Usage:
5-
the-root-command [options] <the-root-arg-no-description-no-default> [<the-root-arg-no-description-default> <the-root-arg-no-default> [<the-root-arg> [<the-root-arg-enum-default>]]]
5+
command [options] <arg-no-description-no-default> [<arg-no-description-default> <arg-no-default> [<arg> [<arg-enum-default>]]]
66

77
Arguments:
8-
<the-root-arg-no-description-no-default>
9-
<the-root-arg-no-description-default> [default: the-root-arg-no-description-default-value]
10-
<the-root-arg-no-default> the-root-arg-no-default-description
11-
<the-root-arg> the-root-arg-description [default: the-root-arg-one-value]
12-
<Read|ReadWrite|Write> the-root-arg-enum-default-description [default: Read]
8+
<arg-no-description-no-default>
9+
<arg-no-description-default> [default: arg-no-description-default-value]
10+
<arg-no-default> arg-no-default-description
11+
<arg> arg-description [default: arg-one-value]
12+
<Read|ReadWrite|Write> arg-enum-default-description [default: Read]
1313

1414
Options:
15-
-trna, --the-root-option-no-arg (REQUIRED) the-root-option-no-arg-description
16-
-trondda, --the-root-option-no-description-default-arg <the-root-option-no-description-default-arg> [default: the-root-option--no-description-default-arg-value]
17-
-tronda, --the-root-option-no-default-arg <the-root-option-arg-no-default-arg> (REQUIRED) the-root-option-no-default-description
18-
-troda, --the-root-option-default-arg <the-root-option-arg> the-root-option-default-arg-description [default: the-root-option-arg-value]
19-
-troea, --the-root-option-enum-arg <Read|ReadWrite|Write> the-root-option-description [default: Read]
20-
-trorea, --the-root-option-required-enum-arg <Read|ReadWrite|Write> (REQUIRED) the-root-option-description [default: Read]
21-
-tromld, --the-root-option-multi-line-description the-root-option
22-
multi-line
23-
description
15+
-na, --option-no-arg (REQUIRED) option-no-arg-description
16+
-ondda, --option-no-description-default-arg <option-no-description-default-arg> [default: option-no-description-default-arg-value]
17+
-onda, --option-no-default-arg <option-arg-no-default-arg> (REQUIRED) option-no-default-description
18+
-oda, --option-default-arg <option-arg> option-default-arg-description [default: option-arg-value]
19+
-oea, --option-enum-arg <Read|ReadWrite|Write> option-description [default: Read]
20+
-orea, --option-required-enum-arg <Read|ReadWrite|Write> (REQUIRED) option-description [default: Read]
21+
-omld, --option-multi-line-description option with
22+
multi-line
23+
description
2424

src/System.CommandLine.Tests/Help/HelpBuilderTests.Approval.cs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,57 @@ public void Help_describes_default_values_for_complex_root_command_scenario()
1414
{
1515
var command = new RootCommand(description: "Test description")
1616
{
17-
new Argument<string>("the-root-arg-no-description-no-default"),
18-
new Argument<string>("the-root-arg-no-description-default",
19-
argResult => "the-root-arg-no-description-default-value",
17+
new Argument<string>("arg-no-description-no-default"),
18+
new Argument<string>("arg-no-description-default",
19+
argResult => "arg-no-description-default-value",
2020
isDefault: true),
21-
new Argument<string>("the-root-arg-no-default")
21+
new Argument<string>("arg-no-default")
2222
{
23-
Description = "the-root-arg-no-default-description",
23+
Description = "arg-no-default-description",
2424
},
25-
new Argument<string>("the-root-arg", () => "the-root-arg-one-value")
25+
new Argument<string>("arg", () => "arg-one-value")
2626
{
27-
Description = "the-root-arg-description"
27+
Description = "arg-description"
2828
},
29-
new Argument<FileAccess>("the-root-arg-enum-default", () => FileAccess.Read)
29+
new Argument<FileAccess>("arg-enum-default", () => FileAccess.Read)
3030
{
31-
Description = "the-root-arg-enum-default-description",
32-
ArgumentType = typeof(FileAccess)
31+
Description = "arg-enum-default-description",
3332
},
34-
new Option(aliases: new string[] {"--the-root-option-no-arg", "-trna"}) {
35-
Description = "the-root-option-no-arg-description",
33+
new Option(aliases: new string[] {"--option-no-arg", "-na"}) {
34+
Description = "option-no-arg-description",
3635
IsRequired = true
3736
},
3837
new Option<string>(
39-
aliases: new string[] {"--the-root-option-no-description-default-arg", "-trondda"},
40-
parseArgument: _ => "the-root-option--no-description-default-arg-value",
38+
aliases: new string[] {"--option-no-description-default-arg", "-ondda"},
39+
parseArgument: _ => "option-no-description-default-arg-value",
4140
isDefault: true
4241
),
43-
new Option<string>(aliases: new string[] {"--the-root-option-no-default-arg", "-tronda"}) {
44-
Description = "the-root-option-no-default-description",
45-
ArgumentHelpName = "the-root-option-arg-no-default-arg",
42+
new Option<string>(aliases: new string[] {"--option-no-default-arg", "-onda"}) {
43+
Description = "option-no-default-description",
44+
ArgumentHelpName = "option-arg-no-default-arg",
4645
IsRequired = true
4746
},
48-
new Option<string>(aliases: new string[] {"--the-root-option-default-arg", "-troda"}, () => "the-root-option-arg-value")
47+
new Option<string>(aliases: new string[] {"--option-default-arg", "-oda"}, () => "option-arg-value")
4948
{
50-
Description = "the-root-option-default-arg-description",
51-
ArgumentHelpName = "the-root-option-arg",
49+
Description = "option-default-arg-description",
50+
ArgumentHelpName = "option-arg",
5251
},
53-
new Option<FileAccess>(aliases: new string[] {"--the-root-option-enum-arg", "-troea"}, () => FileAccess.Read)
52+
new Option<FileAccess>(aliases: new string[] {"--option-enum-arg", "-oea"}, () => FileAccess.Read)
5453
{
55-
Description = "the-root-option-description",
56-
ArgumentHelpName = "the-root-option-arg",
54+
Description = "option-description",
55+
ArgumentHelpName = "option-arg",
5756
},
58-
new Option<FileAccess>(aliases: new string[] {"--the-root-option-required-enum-arg", "-trorea"}, () => FileAccess.Read)
57+
new Option<FileAccess>(aliases: new string[] {"--option-required-enum-arg", "-orea"}, () => FileAccess.Read)
5958
{
60-
Description = "the-root-option-description",
61-
ArgumentHelpName = "the-root-option-arg",
59+
Description = "option-description",
60+
ArgumentHelpName = "option-arg",
6261
IsRequired = true
6362
},
64-
new Option(aliases: new string[] {"--the-root-option-multi-line-description", "-tromld"}) {
65-
Description = "the-root-option\r\nmulti-line\ndescription"
63+
new Option(aliases: new string[] {"--option-multi-line-description", "-omld"}) {
64+
Description = "option with\r\nmulti-line\ndescription"
6665
}
6766
};
68-
command.Name = "the-root-command";
67+
command.Name = "command";
6968

7069
GetHelpBuilder(LargeMaxWidth).Write(command);
7170
Approvals.Verify(_console.Out.ToString());

src/System.CommandLine/Argument.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ public Argument()
3434
/// <param name="name">The name of the argument.</param>
3535
public Argument(string name)
3636
{
37-
if (!string.IsNullOrWhiteSpace(name))
38-
{
39-
Name = name!;
40-
}
37+
Name = name;
4138
}
4239

4340
internal HashSet<string>? AllowedValues { get; private set; }

src/System.CommandLine/Argument{T}.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,10 @@ public Argument(Func<T> getDefaultValue) : this()
7575
/// <param name="isDefault"><c>true</c> to use the <paramref name="parse"/> result as default value.</param>
7676
/// <exception cref="ArgumentNullException">Thrown when <paramref name="parse"/> is null.</exception>
7777
public Argument(
78-
string? name,
78+
string name,
7979
ParseArgument<T> parse,
80-
bool isDefault = false) : this()
80+
bool isDefault = false) : this(name)
8181
{
82-
if (!string.IsNullOrWhiteSpace(name))
83-
{
84-
Name = name!;
85-
}
86-
8782
if (parse is null)
8883
{
8984
throw new ArgumentNullException(nameof(parse));

src/System.CommandLine/Help/HelpBuilder.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.CommandLine.IO;
66
using System.CommandLine.Parsing;
77
using System.Linq;
8-
using System.Text.RegularExpressions;
98
using static System.CommandLine.Help.DefaultHelpText;
109

1110
namespace System.CommandLine.Help
@@ -195,11 +194,6 @@ private void AppendHeading(string? heading)
195194
/// <exception cref="ArgumentNullException"></exception>
196195
private void AppendDescription(string description)
197196
{
198-
if (description is null)
199-
{
200-
throw new ArgumentNullException(nameof(description));
201-
}
202-
203197
var availableWidth = GetAvailableWidth();
204198
var descriptionLines = SplitText(description, availableWidth);
205199

@@ -389,7 +383,6 @@ protected virtual IReadOnlyList<string> SplitText(string text, int width)
389383
throw new ArgumentOutOfRangeException(nameof(width), $"{nameof(width)} must be non-negative.");
390384
}
391385

392-
393386
if (width == 0)
394387
{
395388
return Array.Empty<string>();
@@ -818,15 +811,8 @@ protected virtual void AddAdditionalArguments(ICommand command)
818811
HelpSection.WriteHeading(this, AdditionalArguments.Title, AdditionalArguments.Description);
819812
}
820813

821-
private bool ShouldDisplayArgumentHelp(ICommand? command)
822-
{
823-
if (command is null)
824-
{
825-
return false;
826-
}
827-
828-
return command.Arguments.Any(ShouldShowHelp);
829-
}
814+
private bool ShouldDisplayArgumentHelp(ICommand command) =>
815+
command.Arguments.Any(ShouldShowHelp);
830816

831817
private int GetConsoleWindowWidth(IConsole console)
832818
{
@@ -840,11 +826,6 @@ private int GetConsoleWindowWidth(IConsole console)
840826
}
841827
}
842828

843-
private static string ShortenWhitespace(string input)
844-
{
845-
return Regex.Replace(input, @"\s+", " ").TrimEnd();
846-
}
847-
848829
private string JoinNonEmpty(string separator, params string?[] values)
849830
{
850831
return string.Join(separator, values.Where(str => !string.IsNullOrEmpty(str)));
@@ -874,8 +855,6 @@ protected bool Equals(HelpItem other) =>
874855
public override bool Equals(object obj) => Equals((HelpItem)obj);
875856

876857
public override int GetHashCode() => (Invocation, Description).GetHashCode();
877-
878-
public bool HasDefaultValueHint => !string.IsNullOrWhiteSpace(DefaultValueHint);
879858
}
880859

881860
private static class HelpSection

0 commit comments

Comments
 (0)