Skip to content

Commit 5afbdda

Browse files
committed
updates help text to incorporate verb aliases
1 parent fe15c5d commit 5afbdda

File tree

4 files changed

+102
-10
lines changed

4 files changed

+102
-10
lines changed

src/CommandLine/Core/Verb.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace CommandLine.Core
99
{
1010
sealed class Verb
1111
{
12-
public Verb(string name, string helpText, bool hidden, bool isDefault, IEnumerable<string> aliases)
12+
public Verb(string name, string helpText, bool hidden, bool isDefault, string[] aliases)
1313
{
14-
if ( string.IsNullOrWhiteSpace(name))
14+
if (string.IsNullOrWhiteSpace(name))
1515
throw new ArgumentNullException(nameof(name));
1616
Name = name;
1717

@@ -29,7 +29,7 @@ public Verb(string name, string helpText, bool hidden, bool isDefault, IEnumerab
2929

3030
public bool IsDefault { get; private set; }
3131

32-
public IEnumerable<string> Aliases { get; private set; }
32+
public string[] Aliases { get; private set; }
3333

3434
public static Verb FromAttribute(VerbAttribute attribute)
3535
{

src/CommandLine/Text/HelpText.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,10 +851,10 @@ private IEnumerable<Specification> AdaptVerbsToSpecifications(IEnumerable<Type>
851851
var optionSpecs = from verbTuple in Verb.SelectFromTypes(types)
852852
select
853853
OptionSpecification.NewSwitch(
854-
string.Empty,
855854
verbTuple.Item1.Name,
855+
verbTuple.Item1.Aliases.ToDelimitedString(", "),
856856
false,
857-
verbTuple.Item1.IsDefault? "(Default Verb) "+verbTuple.Item1.HelpText: verbTuple.Item1.HelpText, //Default verb
857+
verbTuple.Item1.IsDefault ? "(Default Verb) " + verbTuple.Item1.HelpText : verbTuple.Item1.HelpText, //Default verb
858858
string.Empty,
859859
verbTuple.Item1.Hidden);
860860
if (autoHelp)

src/CommandLine/VerbAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace CommandLine
1212
//public sealed class VerbAttribute : Attribute
1313
public class VerbAttribute : Attribute
1414
{
15-
private Infrastructure.LocalizableAttributeProperty helpText;
15+
private readonly Infrastructure.LocalizableAttributeProperty helpText;
1616
private Type resourceType;
1717

1818
/// <summary>
@@ -72,6 +72,6 @@ public Type ResourceType
7272
/// <summary>
7373
/// Gets or sets the aliases
7474
/// </summary>
75-
public IEnumerable<string> Aliases { get; private set; }
75+
public string[] Aliases { get; private set; }
7676
}
7777
}

tests/CommandLine.Tests/Unit/Issue6Tests.cs

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Linq;
35
using CommandLine.Tests.Fakes;
46
using CommandLine.Text;
57
using FluentAssertions;
8+
using Microsoft.FSharp.Core;
69
using Xunit;
710
using Xunit.Abstractions;
811

@@ -22,26 +25,115 @@ public void Parse_option_with_aliased_verbs(string args, Type expectedArgType)
2225
{
2326
var arguments = args.Split(' ');
2427
object options = null;
28+
IEnumerable<Error> errors = null;
2529
var result = Parser.Default.ParseArguments<AliasedVerbOption1, AliasedVerbOption2>(arguments)
26-
.WithParsed((o) => options = o)
30+
.WithParsed(o => options = o)
31+
.WithNotParsed(o => errors = o)
2732
;
33+
if (errors != null && errors.Any())
34+
{
35+
foreach (Error e in errors)
36+
{
37+
System.Console.WriteLine(e.ToString());
38+
}
39+
}
2840

2941
Assert.NotNull(options);
3042
Assert.Equal(expectedArgType, options.GetType());
3143
}
3244

33-
[Verb("move", aliases:new string[] { "mv" })]
45+
[Theory]
46+
[InlineData("--help", true, new string[]
47+
{
48+
"copy, cp, cpy (Default Verb) Copy some stuff",
49+
"move, mv",
50+
"delete Delete stuff",
51+
"help Display more information on a specific command.",
52+
"version Display version information.",
53+
})]
54+
[InlineData("help", true, new string[]
55+
{
56+
"copy, cp, cpy (Default Verb) Copy some stuff",
57+
"move, mv",
58+
"delete Delete stuff",
59+
"help Display more information on a specific command.",
60+
"version Display version information.",
61+
})]
62+
[InlineData("move --help", false, new string[]
63+
{
64+
"-a, --alpha Required.",
65+
"--help Display this help screen.",
66+
"--version Display version information.",
67+
})]
68+
[InlineData("mv --help", false, new string[]
69+
{
70+
"-a, --alpha Required.",
71+
"--help Display this help screen.",
72+
"--version Display version information.",
73+
})]
74+
[InlineData("delete --help", false, new string[]
75+
{
76+
"-b, --beta Required.",
77+
"--help Display this help screen.",
78+
"--version Display version information.",
79+
})]
80+
public void Parse_help_option_for_aliased_verbs(string args, bool verbsIndex, string[] expected)
81+
{
82+
var arguments = args.Split(' ');
83+
object options = null;
84+
IEnumerable<Error> errors = null;
85+
// the order of the arguments here drives the order of the commands shown
86+
// in the help message
87+
var result = Parser.Default.ParseArguments<
88+
AliasedVerbOption2,
89+
AliasedVerbOption1,
90+
VerbNoAlias
91+
>(arguments)
92+
.WithParsed(o => options = o)
93+
.WithNotParsed(o => errors = o)
94+
;
95+
96+
var message = HelpText.AutoBuild(result,
97+
error => error,
98+
ex => ex,
99+
verbsIndex: verbsIndex
100+
);
101+
102+
string helpMessage = message.ToString();
103+
var helps = helpMessage.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Skip(2).ToList<string>();
104+
105+
expected.Length.Should().Be(helps.Count);
106+
int i = 0;
107+
foreach (var expect in expected)
108+
{
109+
helps[i].Trim().Should().Be(expect);
110+
i++;
111+
}
112+
}
113+
114+
[Verb("move", aliases: new string[] { "mv" })]
34115
public class AliasedVerbOption1
35116
{
36117
[Option('a', "alpha", Required = true)]
37118
public string Option { get; set; }
38119
}
39120

40-
[Verb("copy", aliases: new string[] { "cp" })]
121+
[Verb("copy",
122+
isDefault: true,
123+
aliases: new string[] { "cp", "cpy" },
124+
HelpText = "Copy some stuff"
125+
)]
41126
public class AliasedVerbOption2
42127
{
43128
[Option('a', "alpha", Required = true)]
44129
public string Option { get; set; }
45130
}
131+
132+
[Verb("delete",HelpText = "Delete stuff")]
133+
public class VerbNoAlias
134+
{
135+
[Option('b', "beta", Required = true)]
136+
public string Option { get; set; }
137+
}
46138
}
47139
}

0 commit comments

Comments
 (0)