Skip to content

Commit 5c87d7f

Browse files
kzupatriksvensson
authored andcommitted
Allow using -? as a shorthand for -h
Given that it's quite a common switch and extremely unlikely to be already in use for something else, we can just consider it to be the same as having entered `-h` as an arg. This adds the `?` as a valid option character name. Fixes #1547
1 parent 0e2ed51 commit 5c87d7f

File tree

6 files changed

+72
-3
lines changed

6 files changed

+72
-3
lines changed

src/Spectre.Console.Cli/Internal/Configuration/TemplateParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static OptionResult ParseOptionTemplate(string template)
8686

8787
foreach (var character in token.Value)
8888
{
89-
if (!char.IsLetterOrDigit(character) && character != '-' && character != '_')
89+
if (!char.IsLetterOrDigit(character) && character != '-' && character != '_' && character != '?')
9090
{
9191
throw CommandTemplateException.InvalidCharacterInOptionName(template, token, character);
9292
}

src/Spectre.Console.Cli/Internal/Parsing/CommandTreeParser.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public CommandTreeParser(CommandModel configuration, CaseSensitivity caseSensiti
2121
{
2222
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
2323
_parsingMode = parsingMode ?? _configuration.ParsingMode;
24-
_help = new CommandOptionAttribute("-h|--help");
24+
_help = new CommandOptionAttribute("-?|-h|--help");
2525
_convertFlagsToRemainingArguments = convertFlagsToRemainingArguments ?? false;
2626

2727
CaseSensitivity = caseSensitivity;

src/Spectre.Console.Cli/Internal/Parsing/CommandTreeTokenizer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private static IEnumerable<CommandTreeToken> ScanShortOptions(CommandTreeTokeniz
176176
break;
177177
}
178178

179-
if (char.IsLetter(current))
179+
if (char.IsLetter(current) || current is '?')
180180
{
181181
context.AddRemaining(current);
182182
reader.Read(); // Consume
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
USAGE:
2+
myapp [OPTIONS] <COMMAND>
3+
4+
OPTIONS:
5+
-h, --help Prints help information
6+
7+
COMMANDS:
8+
dog <AGE> The dog command
9+
horse The horse command
10+
giraffe <LENGTH> The giraffe command
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
DESCRIPTION:
2+
The horse command.
3+
4+
USAGE:
5+
myapp horse [LEGS] [OPTIONS]
6+
7+
ARGUMENTS:
8+
[LEGS] The number of legs
9+
10+
OPTIONS:
11+
DEFAULT
12+
-h, --help Prints help information
13+
-a, --alive Indicates whether or not the animal is alive
14+
-n, --name <VALUE>
15+
-d, --day <MON|TUE>
16+
--file food.txt
17+
--directory

test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Help.cs

+42
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ public Task Should_Output_Root_Correctly()
2828
return Verifier.Verify(result.Output);
2929
}
3030

31+
[Fact]
32+
[Expectation("Root", "QuestionMark")]
33+
public Task Should_Output_Root_Correctly_QuestionMark()
34+
{
35+
// Given
36+
var fixture = new CommandAppTester();
37+
fixture.Configure(configurator =>
38+
{
39+
configurator.SetApplicationName("myapp");
40+
configurator.AddCommand<DogCommand>("dog");
41+
configurator.AddCommand<HorseCommand>("horse");
42+
configurator.AddCommand<GiraffeCommand>("giraffe");
43+
});
44+
45+
// When
46+
var result = fixture.Run("-?");
47+
48+
// Then
49+
return Verifier.Verify(result.Output);
50+
}
51+
3152
[Fact]
3253
[Expectation("Root_Command")]
3354
public Task Should_Output_Root_Command_Correctly()
@@ -49,6 +70,27 @@ public Task Should_Output_Root_Command_Correctly()
4970
return Verifier.Verify(result.Output);
5071
}
5172

73+
[Fact]
74+
[Expectation("Root_Command", "QuestionMark")]
75+
public Task Should_Output_Root_Command_Correctly_QuestionMark()
76+
{
77+
// Given
78+
var fixture = new CommandAppTester();
79+
fixture.Configure(configurator =>
80+
{
81+
configurator.SetApplicationName("myapp");
82+
configurator.AddCommand<DogCommand>("dog");
83+
configurator.AddCommand<HorseCommand>("horse");
84+
configurator.AddCommand<GiraffeCommand>("giraffe");
85+
});
86+
87+
// When
88+
var result = fixture.Run("horse", "-?");
89+
90+
// Then
91+
return Verifier.Verify(result.Output);
92+
}
93+
5294
[Fact]
5395
[Expectation("Hidden_Commands")]
5496
public Task Should_Skip_Hidden_Commands()

0 commit comments

Comments
 (0)