diff --git a/examples/greet/welcome_command.cr b/examples/greet/welcome_command.cr index accc7d8..30ff086 100644 --- a/examples/greet/welcome_command.cr +++ b/examples/greet/welcome_command.cr @@ -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" + # this will inherit the header and footer properties @inherit_borders = true # this will NOT inherit the parent flag options diff --git a/spec/formatter_spec.cr b/spec/formatter_spec.cr index 39a3c96..672f26b 100644 --- a/spec/formatter_spec.cr +++ b/spec/formatter_spec.cr @@ -19,6 +19,9 @@ 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 @@ -26,9 +29,14 @@ class WelcomeCommand < Cling::Command 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 @@ -92,6 +100,18 @@ describe Cling::Formatter do end.should eq "Usage:\n\tgreet [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 [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 [options]\n\n" + end + it "generates an arguments section" do String.build do |io| formatter.format_arguments(command_with_subcommand, io) diff --git a/src/cling/formatter.cr b/src/cling/formatter.cr index 3160c1a..6f46da4 100644 --- a/src/cling/formatter.cr +++ b/src/cling/formatter.cr @@ -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 << " " end @@ -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