Skip to content

Commit

Permalink
updates help text to incorporate verb aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjaylward committed May 27, 2020
1 parent 5b4a509 commit 9ee6962
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/CommandLine/Core/Verb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace CommandLine.Core
{
sealed class Verb
{
public Verb(string name, string helpText, bool hidden, bool isDefault, IEnumerable<string> aliases)
public Verb(string name, string helpText, bool hidden, bool isDefault, string[] aliases)
{
if ( string.IsNullOrWhiteSpace(name))
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
Name = name;

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

public bool IsDefault { get; private set; }

public IEnumerable<string> Aliases { get; private set; }
public string[] Aliases { get; private set; }

public static Verb FromAttribute(VerbAttribute attribute)
{
Expand Down
4 changes: 2 additions & 2 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -851,10 +851,10 @@ private IEnumerable<Specification> AdaptVerbsToSpecifications(IEnumerable<Type>
var optionSpecs = from verbTuple in Verb.SelectFromTypes(types)
select
OptionSpecification.NewSwitch(
string.Empty,
verbTuple.Item1.Name,
verbTuple.Item1.Aliases.ToDelimitedString(", "),
false,
verbTuple.Item1.IsDefault? "(Default Verb) "+verbTuple.Item1.HelpText: verbTuple.Item1.HelpText, //Default verb
verbTuple.Item1.IsDefault ? "(Default Verb) " + verbTuple.Item1.HelpText : verbTuple.Item1.HelpText, //Default verb
string.Empty,
verbTuple.Item1.Hidden);
if (autoHelp)
Expand Down
4 changes: 2 additions & 2 deletions src/CommandLine/VerbAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace CommandLine
//public sealed class VerbAttribute : Attribute
public class VerbAttribute : Attribute
{
private Infrastructure.LocalizableAttributeProperty helpText;
private readonly Infrastructure.LocalizableAttributeProperty helpText;
private Type resourceType;

/// <summary>
Expand Down Expand Up @@ -72,6 +72,6 @@ public Type ResourceType
/// <summary>
/// Gets or sets the aliases
/// </summary>
public IEnumerable<string> Aliases { get; private set; }
public string[] Aliases { get; private set; }
}
}
98 changes: 95 additions & 3 deletions tests/CommandLine.Tests/Unit/Issue6Tests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using CommandLine.Tests.Fakes;
using CommandLine.Text;
using FluentAssertions;
using Microsoft.FSharp.Core;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -22,26 +25,115 @@ public void Parse_option_with_aliased_verbs(string args, Type expectedArgType)
{
var arguments = args.Split(' ');
object options = null;
IEnumerable<Error> errors = null;
var result = Parser.Default.ParseArguments<AliasedVerbOption1, AliasedVerbOption2>(arguments)
.WithParsed((o) => options = o)
.WithParsed(o => options = o)
.WithNotParsed(o => errors = o)
;
if (errors != null && errors.Any())
{
foreach (Error e in errors)
{
System.Console.WriteLine(e.ToString());
}
}

Assert.NotNull(options);
Assert.Equal(expectedArgType, options.GetType());
}

[Verb("move", aliases:new string[] { "mv" })]
[Theory]
[InlineData("--help", true, new string[]
{
"copy, cp, cpy (Default Verb) Copy some stuff",
"move, mv",
"delete Delete stuff",
"help Display more information on a specific command.",
"version Display version information.",
})]
[InlineData("help", true, new string[]
{
"copy, cp, cpy (Default Verb) Copy some stuff",
"move, mv",
"delete Delete stuff",
"help Display more information on a specific command.",
"version Display version information.",
})]
[InlineData("move --help", false, new string[]
{
"-a, --alpha Required.",
"--help Display this help screen.",
"--version Display version information.",
})]
[InlineData("mv --help", false, new string[]
{
"-a, --alpha Required.",
"--help Display this help screen.",
"--version Display version information.",
})]
[InlineData("delete --help", false, new string[]
{
"-b, --beta Required.",
"--help Display this help screen.",
"--version Display version information.",
})]
public void Parse_help_option_for_aliased_verbs(string args, bool verbsIndex, string[] expected)
{
var arguments = args.Split(' ');
object options = null;
IEnumerable<Error> errors = null;
// the order of the arguments here drives the order of the commands shown
// in the help message
var result = Parser.Default.ParseArguments<
AliasedVerbOption2,
AliasedVerbOption1,
VerbNoAlias
>(arguments)
.WithParsed(o => options = o)
.WithNotParsed(o => errors = o)
;

var message = HelpText.AutoBuild(result,
error => error,
ex => ex,
verbsIndex: verbsIndex
);

string helpMessage = message.ToString();
var helps = helpMessage.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Skip(2).ToList<string>();

expected.Length.Should().Be(helps.Count);
int i = 0;
foreach (var expect in expected)
{
helps[i].Trim().Should().Be(expect);
i++;
}
}

[Verb("move", aliases: new string[] { "mv" })]
public class AliasedVerbOption1
{
[Option('a', "alpha", Required = true)]
public string Option { get; set; }
}

[Verb("copy", aliases: new string[] { "cp" })]
[Verb("copy",
isDefault: true,
aliases: new string[] { "cp", "cpy" },
HelpText = "Copy some stuff"
)]
public class AliasedVerbOption2
{
[Option('a', "alpha", Required = true)]
public string Option { get; set; }
}

[Verb("delete",HelpText = "Delete stuff")]
public class VerbNoAlias
{
[Option('b', "beta", Required = true)]
public string Option { get; set; }
}
}
}

0 comments on commit 9ee6962

Please sign in to comment.