Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/greet/welcome_command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class WelcomeCommand < Cling::Command
@summary = @description = "sends a friendly welcome message"

add_argument "name", description: "the name of the person to greet", required: true
add_option 'h', "help", description: "sends help information"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added the help option to the example, as it was referenced, but not actually included, leading to an error if the user ran it.


# this will inherit the header and footer properties
@inherit_borders = true
# this will NOT inherit the parent flag options
Expand Down
24 changes: 22 additions & 2 deletions spec/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@ class WelcomeCommand < Cling::Command
def setup : Nil
@name = "welcome"
@summary = @description = "sends a friendly welcome message"

add_argument "name", description: "the name of the person to greet", required: true
add_option 'h', "help", description: "sends help information"
end

def run(arguments : Cling::Arguments, options : Cling::Options) : Nil
end
end

command_with_subcommand = GreetCommand.new
command_with_subcommand.add_command WelcomeCommand.new

command_without_subcommand = GreetCommand.new

subcommand = WelcomeCommand.new
command_with_subcommand.add_command subcommand

sub_subcommand = WelcomeCommand.new
subcommand.add_command sub_subcommand

formatter = Cling::Formatter.new

describe Cling::Formatter do
Expand Down Expand Up @@ -92,6 +100,18 @@ describe Cling::Formatter do
end.should eq "Usage:\n\tgreet <arguments> [options]\n\n"
end

it "generates a usage section for subcommand" do
String.build do |io|
formatter.format_usage(subcommand, io)
end.should eq "Usage:\n\tgreet welcome <command> <arguments> [options]\n\n"
end

it "generates a usage section for nested subcommand" do
String.build do |io|
formatter.format_usage(sub_subcommand, io)
end.should eq "Usage:\n\tgreet welcome welcome <arguments> [options]\n\n"
end

it "generates an arguments section" do
String.build do |io|
formatter.format_arguments(command_with_subcommand, io)
Expand Down
15 changes: 14 additions & 1 deletion src/cling/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ module Cling
io << "Usage:"

if command.usage.empty?
io << "\n\t" << command.name
io << "\n\t"

print_command_names(command, io)

unless command.children.empty?
io << " <command>"
end
Expand Down Expand Up @@ -164,5 +167,15 @@ module Cling
return unless footer = command.footer
io << footer << "\n\n"
end

# Recursively prints all command names, including parent commands.
private def print_command_names(command : Command, io : IO) : Nil
if parent = command.parent
print_command_names(parent, io)
end

io << " " if parent
io << command.name
end
end
end