Skip to content

Commit 1e313a0

Browse files
authored
Merge pull request #162 from DannyBen/refactor/command-scopes
Refactor command scopes
2 parents e202478 + 2291269 commit 1e313a0

File tree

2 files changed

+69
-64
lines changed

2 files changed

+69
-64
lines changed

lib/bashly/concerns/command_scopes.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module Bashly
2+
# This is a `Command` concern responsible for providing additional scopes.
3+
module CommandScopes
4+
# Returns only the names of the Commands
5+
def command_names
6+
commands.map &:name
7+
end
8+
9+
# Returns a flat array containing all the commands in this tree.
10+
# This includes self + children + grandchildres + ...
11+
def deep_commands
12+
result = []
13+
commands.each do |command|
14+
result << command
15+
if command.commands.any?
16+
result += command.deep_commands
17+
end
18+
end
19+
result
20+
end
21+
22+
# If any of this command's subcommands has the default option set to
23+
# true, this default command will be returned, nil otherwise.
24+
def default_command
25+
commands.find { |c| c.default }
26+
end
27+
28+
# Returns an array of all the default Args
29+
def default_args
30+
args.select &:default
31+
end
32+
33+
# Returns an array of all the default Environment Variables
34+
def default_environment_variables
35+
environment_variables.select &:default
36+
end
37+
38+
# Returns an array of all the default Flags
39+
def default_flags
40+
flags.select &:default
41+
end
42+
43+
# Returns an array of all the required Arguments
44+
def required_args
45+
args.select &:required
46+
end
47+
48+
# Returns an array of all the required EnvironmentVariables
49+
def required_environment_variables
50+
environment_variables.select &:required
51+
end
52+
53+
# Returns an array of all the required Flags
54+
def required_flags
55+
flags.select &:required
56+
end
57+
58+
# Returns an array of all the args with a whitelist
59+
def whitelisted_args
60+
args.select &:allowed
61+
end
62+
63+
# Returns an array of all the flags with a whitelist arg
64+
def whitelisted_flags
65+
flags.select &:allowed
66+
end
67+
end
68+
end

lib/bashly/script/command.rb

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Bashly
22
module Script
33
class Command < Base
44
include Completions
5+
include CommandScopes
56

67
# Returns the name to be used as an action.
78
# - If it is the root command, the action is "root"
@@ -34,11 +35,6 @@ def catch_all
3435
@catch_all ||= CatchAll.from_config options['catch_all']
3536
end
3637

37-
# Returns only the names of the Commands
38-
def command_names
39-
commands.map &:name
40-
end
41-
4238
# Returns an array of the Commands
4339
def commands
4440
return [] unless options["commands"]
@@ -48,40 +44,6 @@ def commands
4844
end
4945
end
5046

51-
# Returns a flat array containing all the commands in this tree.
52-
# This includes self + children + grandchildres + ...
53-
def deep_commands
54-
result = []
55-
commands.each do |command|
56-
result << command
57-
if command.commands.any?
58-
result += command.deep_commands
59-
end
60-
end
61-
result
62-
end
63-
64-
# Returns an array of all the default Args
65-
def default_args
66-
args.select &:default
67-
end
68-
69-
# If any of this command's subcommands has the default option set to
70-
# true, this default command will be returned, nil otherwise.
71-
def default_command
72-
commands.find { |c| c.default }
73-
end
74-
75-
# Returns an array of all the default Environment Variables
76-
def default_environment_variables
77-
environment_variables.select &:default
78-
end
79-
80-
# Returns an array of all the default Flags
81-
def default_flags
82-
flags.select &:default
83-
end
84-
8547
# Returns an array of EnvironmentVariables
8648
def environment_variables
8749
return [] unless options["environment_variables"]
@@ -137,21 +99,6 @@ def parents
13799
options['parents'] || []
138100
end
139101

140-
# Returns an array of all the required Arguments
141-
def required_args
142-
args.select &:required
143-
end
144-
145-
# Returns an array of all the required EnvironmentVariables
146-
def required_environment_variables
147-
environment_variables.select &:required
148-
end
149-
150-
# Returns an array of all the required Flags
151-
def required_flags
152-
flags.select &:required
153-
end
154-
155102
# Returns trus if this is the root command (no parents)
156103
def root_command?
157104
parents.empty?
@@ -187,16 +134,6 @@ def validate_options
187134
Bashly::ConfigValidator.new(options).validate
188135
end
189136

190-
# Returns an array of all the args with a whitelist
191-
def whitelisted_args
192-
args.select &:allowed
193-
end
194-
195-
# Returns an array of all the flags with a whitelist arg
196-
def whitelisted_flags
197-
flags.select &:allowed
198-
end
199-
200137
end
201138
end
202139
end

0 commit comments

Comments
 (0)