Skip to content

Commit 651d866

Browse files
committed
refactor commands_help_data
1 parent aeef258 commit 651d866

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

lib/bashly/concerns/command_scopes.rb

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,21 @@ def required_flags
7070
flags.select &:required
7171
end
7272

73+
def public_commands
74+
commands.reject &:private
75+
end
76+
7377
# Returns a data structure for displaying subcommands help
7478
def commands_help_data
75-
group_string = strings[:commands]
7679
result = {}
7780

78-
commands.reject(&:private).each do |command|
79-
summary = if command.default
80-
strings[:default_command_summary] % { summary: command.summary }
81-
else
82-
command.summary
83-
end
84-
85-
group_string = strings[:group] % { group: command.group } if command.group
86-
87-
result[group_string] ||= {}
88-
result[group_string][command.name] = summary
89-
90-
if command.deep_help
91-
command.commands.reject(&:private).each do |subcommand|
92-
sub_summary = if subcommand.default
93-
strings[:default_command_summary] % { summary: subcommand.summary }
94-
else
95-
subcommand.summary
96-
end
97-
98-
result[group_string]["#{command.name} #{subcommand.name}"] = sub_summary
99-
end
81+
public_commands.each do |command|
82+
result[command.group_string] ||= {}
83+
result[command.group_string][command.name] = command.summary_string
84+
next unless command.deep_help
85+
86+
command.public_commands.each do |subcommand|
87+
result[command.group_string]["#{command.name} #{subcommand.name}"] = subcommand.summary_string
10088
end
10189
end
10290

lib/bashly/script/command.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ def full_name
100100
parents.any? ? (parents + [name]).join(' ') : name
101101
end
102102

103+
# Returns the string for the group caption
104+
def group_string
105+
if group
106+
strings[:group] % { group: group }
107+
else
108+
strings[:commands]
109+
end
110+
end
111+
103112
# Reads a file from the userspace (Settings.source_dir) and returns
104113
# its contents.
105114
# If the file is not found, returns a string with a hint.
@@ -143,6 +152,15 @@ def short_flag_exist?(flag)
143152
flags.select { |f| f.short == flag }.any?
144153
end
145154

155+
# Returns the summary string
156+
def summary_string
157+
if default
158+
strings[:default_command_summary] % { summary: summary }
159+
else
160+
summary
161+
end
162+
end
163+
146164
# Returns a constructed string suitable for Usage pattern
147165
def usage_string
148166
result = [full_name]

0 commit comments

Comments
 (0)