Skip to content

Commit 8b8567f

Browse files
Properly display command usage when it has sub-commands and doesn't have arguments
1 parent e15b0ad commit 8b8567f

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

lib/dry/cli/usage.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ def self.call(result)
3131
def self.commands_and_arguments(result)
3232
max_length = 0
3333
ret = commands(result).each_with_object({}) do |(name, node), memo|
34-
args = if node.command && node.leaf? && node.children?
35-
ROOT_COMMAND_WITH_SUBCOMMANDS_BANNER
36-
elsif node.leaf?
37-
arguments(node.command)
38-
else
39-
SUBCOMMAND_BANNER
40-
end
41-
42-
partial = " #{command_name(result, name)}#{args}"
34+
args = arguments(node.command)
35+
args_banner = if node.command && node.leaf? && node.children? && args
36+
ROOT_COMMAND_WITH_SUBCOMMANDS_BANNER
37+
elsif node.leaf? && args
38+
args
39+
elsif node.children?
40+
SUBCOMMAND_BANNER
41+
end
42+
43+
partial = " #{command_name(result, name)}#{args_banner}"
4344
max_length = partial.bytesize if max_length < partial.bytesize
4445
memo[partial] = node
4546
end

spec/support/fixtures/shared_commands.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,18 @@ def call(**params)
438438
end
439439
end
440440

441+
class Namespace < Dry::CLI::Command
442+
desc "This is a namespace"
443+
444+
class SubCommand < Dry::CLI::Command
445+
desc "I'm a concrete command"
446+
447+
def call(**params)
448+
puts "I'm a concrete command"
449+
end
450+
end
451+
end
452+
441453
class InitializedCommand < Dry::CLI::Command
442454
attr_reader :prop
443455

spec/support/fixtures/with_block.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
cli.register "root-command", Commands::RootCommand do |prefix|
2323
prefix.register "sub-command", Commands::RootCommands::SubCommand
2424
end
25+
cli.register "namespace", Commands::Namespace do |prefix|
26+
prefix.register "sub-command", Commands::Namespace::SubCommand
27+
end
2528

2629
cli.register "options-with-aliases", Commands::OptionsWithAliases
2730
cli.register "variadic default", Commands::VariadicArguments

spec/support/fixtures/with_registry.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ module Commands
5858
register "with-initializer", ::Commands::InitializedCommand.new(prop: "prop_val")
5959
register "root-command", ::Commands::RootCommand
6060
register "root-command sub-command", ::Commands::RootCommands::SubCommand
61+
register "namespace", ::Commands::Namespace
62+
register "namespace sub-command", ::Commands::Namespace::SubCommand
6163

6264
register "options-with-aliases", ::Commands::OptionsWithAliases
6365
register "variadic default", ::Commands::VariadicArguments

spec/support/fixtures/with_zero_arity_block.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
register "root-command" do
2424
register "sub-command", Commands::RootCommands::SubCommand
2525
end
26+
register "namespace", Commands::Namespace
27+
register "namespace" do
28+
register "sub-command", Commands::Namespace::SubCommand
29+
end
2630

2731
register "options-with-aliases", Commands::OptionsWithAliases
2832
register "variadic default", Commands::VariadicArguments

spec/support/shared_examples/rendering.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#{cmd} greeting [RESPONSE]
2020
#{cmd} hello # Print a greeting
2121
#{cmd} inherited [SUBCOMMAND]
22+
#{cmd} namespace [SUBCOMMAND] # This is a namespace
2223
#{cmd} new PROJECT # Generate a new Foo project
2324
#{cmd} options-with-aliases # Accepts options with aliases
2425
#{cmd} root-command [ARGUMENT|SUBCOMMAND] # Root command with arguments and subcommands
@@ -80,6 +81,7 @@
8081
#{cmd} greeting [RESPONSE]
8182
#{cmd} hello # Print a greeting
8283
#{cmd} inherited [SUBCOMMAND]
84+
#{cmd} namespace [SUBCOMMAND] # This is a namespace
8385
#{cmd} new PROJECT # Generate a new Foo project
8486
#{cmd} options-with-aliases # Accepts options with aliases
8587
#{cmd} root-command [ARGUMENT|SUBCOMMAND] # Root command with arguments and subcommands
@@ -109,6 +111,7 @@
109111
#{cmd} greeting [RESPONSE]
110112
#{cmd} hello # Print a greeting
111113
#{cmd} inherited [SUBCOMMAND]
114+
#{cmd} namespace [SUBCOMMAND] # This is a namespace
112115
#{cmd} new PROJECT # Generate a new Foo project
113116
#{cmd} options-with-aliases # Accepts options with aliases
114117
#{cmd} root-command [ARGUMENT|SUBCOMMAND] # Root command with arguments and subcommands

0 commit comments

Comments
 (0)