File tree 3 files changed +40
-3
lines changed
3 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,25 @@ def description(desc = nil)
45
45
@description
46
46
end
47
47
48
+ # Public: Sets the default command
49
+ #
50
+ # command_name - the command name to be executed in the event no args are
51
+ # present
52
+ #
53
+ # Returns the default command if there is one, `nil` otherwise
54
+ def default_command ( command_name = nil )
55
+ if command_name
56
+ if commands . has_key? ( command_name )
57
+ @default_command = commands [ command_name ] if command_name
58
+ @default_command
59
+ else
60
+ raise ArgumentError . new ( "'#{ command_name } ' couldn't be found in this command's list of commands." )
61
+ end
62
+ else
63
+ @default_command
64
+ end
65
+ end
66
+
48
67
# Public: Adds an option switch
49
68
#
50
69
# sym - the variable key which is used to identify the value of the switch
@@ -148,7 +167,11 @@ def process_options(opts, config)
148
167
#
149
168
# Returns nothing
150
169
def execute ( argv = [ ] , config = { } )
151
- actions . each { |a | a . call ( argv , config ) }
170
+ if actions . empty? && !default_command . nil?
171
+ default_command . execute
172
+ else
173
+ actions . each { |a | a . call ( argv , config ) }
174
+ end
152
175
end
153
176
154
177
# Public: Check if this command has a subcommand
Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ def go(argv)
46
46
47
47
logger . debug ( "Parsed config: #{ @config . inspect } " )
48
48
49
- cmd . actions . each { | a | a . call ( argv , @config ) }
49
+ cmd . execute ( argv , @config )
50
50
end
51
51
end
52
52
end
Original file line number Diff line number Diff line change 4
4
5
5
context "a basic command" do
6
6
let ( :command ) { Mercenary ::Command . new ( :my_name ) }
7
+ let ( :parent ) { Mercenary ::Command . new ( :my_parent ) }
8
+ let ( :with_sub ) do
9
+ c = Mercenary ::Command . new ( :i_have_subcommand )
10
+ add_sub . call ( c )
11
+ c
12
+ end
7
13
let ( :command_with_parent ) do
8
14
Mercenary ::Command . new (
9
15
:i_have_parent ,
10
16
parent
11
17
)
12
18
end
13
- let ( :parent ) { Mercenary ::Command . new ( :my_parent ) }
14
19
let ( :add_sub ) do
15
20
Proc . new do |c |
16
21
c . command ( :sub_command ) { |p | }
50
55
expect ( command . options ) . to eq ( [ opt ] )
51
56
expect ( command . map ) . to include ( { opt . first => name } )
52
57
end
58
+
59
+ it "raises an ArgumentError if I specify a default_command that isn't there" do
60
+ c = command # some weird NameError with the block below?
61
+ expect { c . default_command ( :nope ) } . to raise_error ( ArgumentError )
62
+ end
63
+
64
+ it "sets the default_command" do
65
+ expect ( with_sub . default_command ( :sub_command ) . name ) . to eq ( :sub_command )
66
+ end
53
67
end
54
68
55
69
end
You can’t perform that action at this time.
0 commit comments