Skip to content

Commit fed075e

Browse files
committed
fix 866: slight simplication of Argument<T> and Option<T> ctors, make them more congruent
1 parent 7146ca4 commit fed075e

File tree

6 files changed

+28
-30
lines changed

6 files changed

+28
-30
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va
423423
argumentResult.ErrorMessage = $"'{argumentResult.Tokens.Single().Value}' is not an integer";
424424

425425
return default;
426-
}),
427-
Description = ""
426+
})
428427
}
429428
};
430429

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,16 @@ public void Usage_section_shows_arguments_if_there_are_arguments_for_command_whe
187187
{
188188
Name = "arg2",
189189
Arity = new ArgumentArity(
190-
minArityForArg2,
190+
minArityForArg2,
191191
maxArityForArg2)
192-
};
192+
};
193193
var command = new Command("the-command", "command help")
194194
{
195195
arg1,
196196
arg2,
197-
new Option(new[] { "-v", "--verbosity" })
198-
{
199-
Description = "Sets the verbosity"
200-
}
197+
new Option(new[] { "-v", "--verbosity" }, "Sets the verbosity")
201198
};
199+
202200
var rootCommand = new RootCommand();
203201
rootCommand.AddCommand(command);
204202

@@ -479,8 +477,7 @@ public void Arguments_section_is_included_if_there_are_commands_with_arguments_c
479477
new Argument
480478
{
481479
Name = "arg command name",
482-
Description = "test",
483-
Arity = ArgumentArity.ExactlyOne
480+
Description = "test"
484481
}
485482
};
486483

src/System.CommandLine.Tests/ParseDiagramTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ public void Parse_diagram_identifies_options_where_default_values_have_been_appl
7878
{
7979
new Option(new[] { "-h", "--height" })
8080
{
81-
Argument = new Argument<int>(getDefaultValue: () => 10), Description = ""
81+
Argument = new Argument<int>(getDefaultValue: () => 10)
8282
},
8383
new Option(new[] { "-w", "--width" })
8484
{
85-
Argument = new Argument<int>(getDefaultValue: () => 15), Description = ""
85+
Argument = new Argument<int>(getDefaultValue: () => 15)
8686
},
8787
new Option(new[] { "-c", "--color" })
8888
{
89-
Argument = new Argument<ConsoleColor>(() => ConsoleColor.Cyan), Description = ""
89+
Argument = new Argument<ConsoleColor>(() => ConsoleColor.Cyan)
9090
}
9191
};
9292

src/System.CommandLine.Tests/ParseDirectiveTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public async Task Parse_directive_writes_parse_diagram()
2929
rootCommand.AddCommand(subcommand);
3030
var option = new Option(new[] { "-c", "--count" })
3131
{
32-
Argument = new Argument<int>(), Description = ""
32+
Argument = new Argument<int>()
3333
};
3434
subcommand.AddOption(option);
3535

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@ namespace System.CommandLine
77
{
88
public class Argument<T> : Argument
99
{
10-
public Argument(string name) : this()
10+
public Argument() : base(null)
1111
{
12-
Name = name;
12+
ArgumentType = typeof(T);
1313
}
1414

15-
public Argument() : base(null)
15+
public Argument(
16+
string name,
17+
string description = null) : base(name)
1618
{
1719
ArgumentType = typeof(T);
20+
Description = description;
1821
}
1922

20-
public Argument(string name, Func<T> getDefaultValue) : this(name)
23+
public Argument(
24+
string name,
25+
Func<T> getDefaultValue,
26+
string description = null) : this(name)
2127
{
2228
if (getDefaultValue == null)
2329
{
2430
throw new ArgumentNullException(nameof(getDefaultValue));
2531
}
2632

2733
SetDefaultValueFactory(() => getDefaultValue());
34+
35+
Description = description;
2836
}
2937

3038
public Argument(Func<T> getDefaultValue) : this()

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,16 @@ namespace System.CommandLine
77
{
88
public class Option<T> : Option
99
{
10-
public Option(string alias) : base(alias)
11-
{
12-
Argument = new Argument<T>();
13-
}
14-
15-
public Option(string[] aliases) : base(aliases)
16-
{
17-
Argument = new Argument<T>();
18-
}
19-
20-
public Option(string alias, string description) : base(alias, description)
10+
public Option(
11+
string alias,
12+
string description = null) : base(alias, description)
2113
{
2214
Argument = new Argument<T>();
2315
}
2416

25-
public Option(string[] aliases, string description) : base(aliases, description)
17+
public Option(
18+
string[] aliases,
19+
string description = null) : base(aliases, description)
2620
{
2721
Argument = new Argument<T>();
2822
}

0 commit comments

Comments
 (0)