Skip to content

Commit

Permalink
Add ability to pass example args using params syntax (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
seclerp authored May 12, 2023
1 parent dac2097 commit 404b052
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions docs/input/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ app.Configure(config =>
config.AddCommand<HelloCommand>("hello")
.WithAlias("hola")
.WithDescription("Say hello")
.WithExample(new []{"hello", "Phil"})
.WithExample(new []{"hello", "Phil", "--count", "4"});
.WithExample("hello", "Phil")
.WithExample("hello", "Phil", "--count", "4");
});
```

Expand Down
2 changes: 1 addition & 1 deletion src/Spectre.Console.Cli/ICommandConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface ICommandConfigurator
/// </summary>
/// <param name="args">The example arguments.</param>
/// <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>
/// Adds an alias (an alternative name) to the command being configured.
Expand Down
4 changes: 2 additions & 2 deletions src/Spectre.Console.Cli/IConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public interface IConfigurator
/// Adds an example of how to use the application.
/// </summary>
/// <param name="args">The example arguments.</param>
void AddExample(string[] args);

void AddExample(params string[] args);

/// <summary>
/// Adds a command.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public CommandConfigurator(ConfiguredCommand command)
Command = command;
}

public ICommandConfigurator WithExample(string[] args)
public ICommandConfigurator WithExample(params string[] args)
{
Command.Examples.Add(args);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Configurator(ITypeRegistrar registrar)
Examples = new List<string[]>();
}

public void AddExample(string[] args)
public void AddExample(params string[] args)
{
Examples.Add(args);
}
Expand Down
24 changes: 11 additions & 13 deletions test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Help.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Spectre.Console.Cli;

namespace Spectre.Console.Tests.Unit.Cli;

public sealed partial class CommandAppTests
Expand Down Expand Up @@ -41,7 +39,7 @@ public Task Should_Skip_Hidden_Commands()
configurator.AddCommand<DogCommand>("dog");
configurator.AddCommand<HorseCommand>("horse");
configurator.AddCommand<GiraffeCommand>("giraffe")
.WithExample(new[] { "giraffe", "123" })
.WithExample("giraffe", "123")
.IsHidden();
});

Expand All @@ -64,7 +62,7 @@ public Task Should_Not_Trim_Description_Trailing_Period()
configurator.AddCommand<DogCommand>("dog");
configurator.AddCommand<HorseCommand>("horse");
configurator.AddCommand<GiraffeCommand>("giraffe")
.WithExample(new[] { "giraffe", "123" })
.WithExample("giraffe", "123")
.IsHidden();
configurator.TrimTrailingPeriods(false);
});
Expand Down Expand Up @@ -232,8 +230,8 @@ public Task Should_Output_Root_Examples_Defined_On_Root()
fixture.Configure(configurator =>
{
configurator.SetApplicationName("myapp");
configurator.AddExample(new[] { "dog", "--name", "Rufus", "--age", "12", "--good-boy" });
configurator.AddExample(new[] { "horse", "--name", "Brutus" });
configurator.AddExample("dog", "--name", "Rufus", "--age", "12", "--good-boy");
configurator.AddExample("horse", "--name", "Brutus");
configurator.AddCommand<DogCommand>("dog");
configurator.AddCommand<HorseCommand>("horse");
});
Expand All @@ -255,9 +253,9 @@ public Task Should_Output_Root_Examples_Defined_On_Direct_Children_If_Root_Have_
{
configurator.SetApplicationName("myapp");
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")
.WithExample(new[] { "horse", "--name", "Brutus" });
.WithExample("horse", "--name", "Brutus");
});

// When
Expand All @@ -280,9 +278,9 @@ public Task Should_Output_Root_Examples_Defined_On_Leaves_If_No_Other_Examples_A
{
animal.SetDescription("The animal command.");
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")
.WithExample(new[] { "animal", "horse", "--name", "Brutus" });
.WithExample("animal", "horse", "--name", "Brutus");
});
});

Expand All @@ -308,9 +306,9 @@ public Task Should_Only_Output_Command_Examples_Defined_On_Command()
animal.AddExample(new[] { "animal", "--help" });

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")
.WithExample(new[] { "animal", "horse", "--name", "Brutus" });
.WithExample("animal", "horse", "--name", "Brutus");
});
});

Expand All @@ -331,7 +329,7 @@ public Task Should_Output_Root_Examples_If_Default_Command_Is_Specified()
fixture.Configure(configurator =>
{
configurator.SetApplicationName("myapp");
configurator.AddExample(new[] { "12", "-c", "3" });
configurator.AddExample("12", "-c", "3");
});

// When
Expand Down

0 comments on commit 404b052

Please sign in to comment.