@@ -2,7 +2,6 @@ module Bashly
22 module Script
33 class Command < Base
44 include Completions ::Command
5- include CommandScopes
65
76 class << self
87 def option_keys
@@ -32,6 +31,16 @@ def aliases
3231 [ name ] + alt
3332 end
3433
34+ # Returns an array of all full names (including aliases and aliases of
35+ # parents)
36+ def all_full_names
37+ if parent_command
38+ parent_command . all_full_names . product ( aliases ) . map { |a | a . join ' ' }
39+ else
40+ aliases
41+ end
42+ end
43+
3544 # Returns an array of alternative aliases if any
3645 def alt
3746 # DEPRECATION 0.8.0
@@ -57,6 +66,33 @@ def catch_all
5766 @catch_all ||= CatchAll . from_config options [ 'catch_all' ]
5867 end
5968
69+ # Returns a full list of the Command names and aliases combined
70+ def command_aliases
71+ commands . map ( &:aliases ) . flatten
72+ end
73+
74+ # Returns a data structure for displaying subcommands help
75+ def command_help_data
76+ result = { }
77+
78+ public_commands . each do |command |
79+ result [ command . group_string ] ||= { }
80+ result [ command . group_string ] [ command . name ] = command . summary_string
81+ next unless command . expose
82+
83+ command . public_commands . each do |subcommand |
84+ result [ command . group_string ] [ "#{ command . name } #{ subcommand . name } " ] = subcommand . summary_string
85+ end
86+ end
87+
88+ result
89+ end
90+
91+ # Returns only the names of the Commands
92+ def command_names
93+ commands . map &:name
94+ end
95+
6096 # Returns an array of the Commands
6197 def commands
6298 return [ ] unless options [ "commands" ]
@@ -67,6 +103,40 @@ def commands
67103 end
68104 end
69105
106+ # Returns a flat array containing all the commands in this tree.
107+ # This includes self + children + grandchildres + ...
108+ def deep_commands
109+ result = [ ]
110+ commands . each do |command |
111+ result << command
112+ if command . commands . any?
113+ result += command . deep_commands
114+ end
115+ end
116+ result
117+ end
118+
119+ # If any of this command's subcommands has the default option set to
120+ # true, this default command will be returned, nil otherwise.
121+ def default_command
122+ commands . find { |c | c . default }
123+ end
124+
125+ # Returns an array of all the default Args
126+ def default_args
127+ args . select &:default
128+ end
129+
130+ # Returns an array of all the default Environment Variables
131+ def default_environment_variables
132+ environment_variables . select &:default
133+ end
134+
135+ # Returns an array of all the default Flags
136+ def default_flags
137+ flags . select &:default
138+ end
139+
70140 # Returns an array of EnvironmentVariables
71141 def environment_variables
72142 return [ ] unless options [ "environment_variables" ]
@@ -109,23 +179,6 @@ def group_string
109179 end
110180 end
111181
112- # Reads a file from the userspace (Settings.source_dir) and returns
113- # its contents.
114- # If the file is not found, returns a string with a hint.
115- def load_user_file ( file , placeholder : true )
116- path = "#{ Settings . source_dir } /#{ file } "
117-
118- content = if File . exist? path
119- File . read ( path ) . remove_front_matter
120- elsif placeholder
121- %q[echo "error: cannot load file"]
122- else
123- ''
124- end
125-
126- Settings . production? ? content : "#{ view_marker path } \n #{ content } "
127- end
128-
129182 # Returns the Command instance of the direct parent
130183 def parent_command
131184 options [ 'parent_command' ]
@@ -137,11 +190,31 @@ def parents
137190 options [ 'parents' ] || [ ]
138191 end
139192
193+ # Returns only commands that are not private
194+ def public_commands
195+ commands . reject &:private
196+ end
197+
140198 # Returns true if one of the args is repeatable
141199 def repeatable_arg_exist?
142200 args . select ( &:repeatable ) . any?
143201 end
144202
203+ # Returns an array of all the required Arguments
204+ def required_args
205+ args . select &:required
206+ end
207+
208+ # Returns an array of all the required EnvironmentVariables
209+ def required_environment_variables
210+ environment_variables . select &:required
211+ end
212+
213+ # Returns an array of all the required Flags
214+ def required_flags
215+ flags . select &:required
216+ end
217+
145218 # Returns true if this is the root command (no parents)
146219 def root_command?
147220 parents . empty?
@@ -180,6 +253,15 @@ def user_lib
180253 @user_lib ||= Dir [ "#{ Settings . full_lib_dir } /**/*.sh" ] . sort
181254 end
182255
256+ # Returns an array of all the args with a whitelist
257+ def whitelisted_args
258+ args . select &:allowed
259+ end
260+
261+ # Returns an array of all the flags with a whitelist arg
262+ def whitelisted_flags
263+ flags . select &:allowed
264+ end
183265 end
184266 end
185267end
0 commit comments