Add ability to pass example args using params syntax (#1166)

This commit is contained in:
Andrii Rublov 2023-05-12 04:08:42 -07:00 committed by GitHub
parent dac2097321
commit 404b052a5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 20 deletions

View File

@ -37,8 +37,8 @@ app.Configure(config =>
config.AddCommand<HelloCommand>("hello") config.AddCommand<HelloCommand>("hello")
.WithAlias("hola") .WithAlias("hola")
.WithDescription("Say hello") .WithDescription("Say hello")
.WithExample(new []{"hello", "Phil"}) .WithExample("hello", "Phil")
.WithExample(new []{"hello", "Phil", "--count", "4"}); .WithExample("hello", "Phil", "--count", "4");
}); });
``` ```

View File

@ -10,7 +10,7 @@ public interface ICommandConfigurator
/// </summary> /// </summary>
/// <param name="args">The example arguments.</param> /// <param name="args">The example arguments.</param>
/// <returns>The same <see cref="ICommandConfigurator"/> instance so that multiple calls can be chained.</returns> /// <returns>The same <see cref="ICommandConfigurator"/> instance so that multiple calls can be chained.</returns>
ICommandConfigurator WithExample(string[] args); ICommandConfigurator WithExample(params string[] args);
/// <summary> /// <summary>
/// Adds an alias (an alternative name) to the command being configured. /// Adds an alias (an alternative name) to the command being configured.

View File

@ -14,7 +14,7 @@ public interface IConfigurator
/// Adds an example of how to use the application. /// Adds an example of how to use the application.
/// </summary> /// </summary>
/// <param name="args">The example arguments.</param> /// <param name="args">The example arguments.</param>
void AddExample(string[] args); void AddExample(params string[] args);
/// <summary> /// <summary>
/// Adds a command. /// Adds a command.

View File

@ -9,7 +9,7 @@ internal sealed class CommandConfigurator : ICommandConfigurator
Command = command; Command = command;
} }
public ICommandConfigurator WithExample(string[] args) public ICommandConfigurator WithExample(params string[] args)
{ {
Command.Examples.Add(args); Command.Examples.Add(args);
return this; return this;

View File

@ -20,7 +20,7 @@ internal sealed class Configurator : IUnsafeConfigurator, IConfigurator, IConfig
Examples = new List<string[]>(); Examples = new List<string[]>();
} }
public void AddExample(string[] args) public void AddExample(params string[] args)
{ {
Examples.Add(args); Examples.Add(args);
} }

View File

@ -1,5 +1,3 @@
using Spectre.Console.Cli;
namespace Spectre.Console.Tests.Unit.Cli; namespace Spectre.Console.Tests.Unit.Cli;
public sealed partial class CommandAppTests public sealed partial class CommandAppTests
@ -41,7 +39,7 @@ public sealed partial class CommandAppTests
configurator.AddCommand<DogCommand>("dog"); configurator.AddCommand<DogCommand>("dog");
configurator.AddCommand<HorseCommand>("horse"); configurator.AddCommand<HorseCommand>("horse");
configurator.AddCommand<GiraffeCommand>("giraffe") configurator.AddCommand<GiraffeCommand>("giraffe")
.WithExample(new[] { "giraffe", "123" }) .WithExample("giraffe", "123")
.IsHidden(); .IsHidden();
}); });
@ -64,7 +62,7 @@ public sealed partial class CommandAppTests
configurator.AddCommand<DogCommand>("dog"); configurator.AddCommand<DogCommand>("dog");
configurator.AddCommand<HorseCommand>("horse"); configurator.AddCommand<HorseCommand>("horse");
configurator.AddCommand<GiraffeCommand>("giraffe") configurator.AddCommand<GiraffeCommand>("giraffe")
.WithExample(new[] { "giraffe", "123" }) .WithExample("giraffe", "123")
.IsHidden(); .IsHidden();
configurator.TrimTrailingPeriods(false); configurator.TrimTrailingPeriods(false);
}); });
@ -232,8 +230,8 @@ public sealed partial class CommandAppTests
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
configurator.AddExample(new[] { "dog", "--name", "Rufus", "--age", "12", "--good-boy" }); configurator.AddExample("dog", "--name", "Rufus", "--age", "12", "--good-boy");
configurator.AddExample(new[] { "horse", "--name", "Brutus" }); configurator.AddExample("horse", "--name", "Brutus");
configurator.AddCommand<DogCommand>("dog"); configurator.AddCommand<DogCommand>("dog");
configurator.AddCommand<HorseCommand>("horse"); configurator.AddCommand<HorseCommand>("horse");
}); });
@ -255,9 +253,9 @@ public sealed partial class CommandAppTests
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
configurator.AddCommand<DogCommand>("dog") configurator.AddCommand<DogCommand>("dog")
.WithExample(new[] { "dog", "--name", "Rufus", "--age", "12", "--good-boy" }); .WithExample("dog", "--name", "Rufus", "--age", "12", "--good-boy");
configurator.AddCommand<HorseCommand>("horse") configurator.AddCommand<HorseCommand>("horse")
.WithExample(new[] { "horse", "--name", "Brutus" }); .WithExample("horse", "--name", "Brutus");
}); });
// When // When
@ -280,9 +278,9 @@ public sealed partial class CommandAppTests
{ {
animal.SetDescription("The animal command."); animal.SetDescription("The animal command.");
animal.AddCommand<DogCommand>("dog") animal.AddCommand<DogCommand>("dog")
.WithExample(new[] { "animal", "dog", "--name", "Rufus", "--age", "12", "--good-boy" }); .WithExample("animal", "dog", "--name", "Rufus", "--age", "12", "--good-boy");
animal.AddCommand<HorseCommand>("horse") animal.AddCommand<HorseCommand>("horse")
.WithExample(new[] { "animal", "horse", "--name", "Brutus" }); .WithExample("animal", "horse", "--name", "Brutus");
}); });
}); });
@ -308,9 +306,9 @@ public sealed partial class CommandAppTests
animal.AddExample(new[] { "animal", "--help" }); animal.AddExample(new[] { "animal", "--help" });
animal.AddCommand<DogCommand>("dog") animal.AddCommand<DogCommand>("dog")
.WithExample(new[] { "animal", "dog", "--name", "Rufus", "--age", "12", "--good-boy" }); .WithExample("animal", "dog", "--name", "Rufus", "--age", "12", "--good-boy");
animal.AddCommand<HorseCommand>("horse") animal.AddCommand<HorseCommand>("horse")
.WithExample(new[] { "animal", "horse", "--name", "Brutus" }); .WithExample("animal", "horse", "--name", "Brutus");
}); });
}); });
@ -331,7 +329,7 @@ public sealed partial class CommandAppTests
fixture.Configure(configurator => fixture.Configure(configurator =>
{ {
configurator.SetApplicationName("myapp"); configurator.SetApplicationName("myapp");
configurator.AddExample(new[] { "12", "-c", "3" }); configurator.AddExample("12", "-c", "3");
}); });
// When // When