Skip to content

Commit f66a537

Browse files
authored
Merge pull request #17 from BigBoyBarney/usage-command-placeholder
feat: add <command> placeholder to usage
2 parents dcb26bc + 7d6dc20 commit f66a537

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

spec/formatter_spec.cr

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ class WelcomeCommand < Cling::Command
2525
end
2626
end
2727

28-
command = GreetCommand.new
29-
command.add_command WelcomeCommand.new
28+
command_with_subcommand = GreetCommand.new
29+
command_with_subcommand.add_command WelcomeCommand.new
30+
31+
command_without_subcommand = GreetCommand.new
3032
formatter = Cling::Formatter.new
3133

3234
describe Cling::Formatter do
3335
it "generates a help template" do
34-
formatter.generate(command).chomp.should eq <<-HELP
36+
formatter.generate(command_with_subcommand).chomp.should eq <<-HELP
3537
Greets a person
3638
3739
Usage:
38-
\tgreet <arguments> [options]
40+
\tgreet <command> <arguments> [options]
3941
4042
Commands:
4143
\twelcome sends a friendly welcome message
@@ -53,11 +55,11 @@ describe Cling::Formatter do
5355
it "generates with a custom delimiter" do
5456
formatter.options.option_delim = '+'
5557

56-
formatter.generate(command).chomp.should eq <<-HELP
58+
formatter.generate(command_with_subcommand).chomp.should eq <<-HELP
5759
Greets a person
5860
5961
Usage:
60-
\tgreet <arguments> [options]
62+
\tgreet <command> <arguments> [options]
6163
6264
Commands:
6365
\twelcome sends a friendly welcome message
@@ -74,27 +76,33 @@ describe Cling::Formatter do
7476

7577
it "generates a description section" do
7678
String.build do |io|
77-
formatter.format_description(command, io)
79+
formatter.format_description(command_with_subcommand, io)
7880
end.should eq "Greets a person\n\n"
7981
end
8082

81-
it "generates a usage section" do
83+
it "generates a usage section with subcommand" do
84+
String.build do |io|
85+
formatter.format_usage(command_with_subcommand, io)
86+
end.should eq "Usage:\n\tgreet <command> <arguments> [options]\n\n"
87+
end
88+
89+
it "generates a usage section without subcommand" do
8290
String.build do |io|
83-
formatter.format_usage(command, io)
91+
formatter.format_usage(command_without_subcommand, io)
8492
end.should eq "Usage:\n\tgreet <arguments> [options]\n\n"
8593
end
8694

8795
it "generates an arguments section" do
8896
String.build do |io|
89-
formatter.format_arguments(command, io)
97+
formatter.format_arguments(command_with_subcommand, io)
9098
end.should eq "Arguments:\n\tname the name of the person (required)\n\n"
9199
end
92100

93101
it "generates an options section" do
94102
formatter.options.option_delim = '-'
95103

96104
String.build do |io|
97-
formatter.format_options(command, io)
105+
formatter.format_options(command_with_subcommand, io)
98106
end.chomp.should eq <<-HELP
99107
Options:
100108
\t-h, --help sends help information

spec/helper_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe Cling::MainCommand do
4949
Runs some Crystal commands
5050
5151
Usage:
52-
\tmain [options]
52+
\tmain <command> [options]
5353
5454
Commands:
5555
\tcontext

src/cling/formatter.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ module Cling
6363

6464
if command.usage.empty?
6565
io << "\n\t" << command.name
66+
unless command.children.empty?
67+
io << " <command>"
68+
end
69+
6670
unless command.arguments.empty?
6771
if command.arguments.values.any? &.required?
6872
io << " <arguments>"

0 commit comments

Comments
 (0)