File tree Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Expand file tree Collapse file tree 2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ def initialize(name, options = {})
13
13
@group = options [ :group ] . to_s . capitalize if options [ :group ]
14
14
@aliases = Array ( options [ :aliases ] )
15
15
@hide = options [ :hide ]
16
+ @inverse = options [ :inverse ]
16
17
end
17
18
18
19
# This parse quick options given as method_options. It makes several
@@ -88,10 +89,7 @@ def usage(padding = 0)
88
89
end
89
90
90
91
sample = "[#{ sample } ]" . dup unless required?
91
-
92
- if boolean?
93
- sample << ", [#{ dasherize ( 'no-' + human_name ) } ]" unless ( name == "force" ) || name . start_with? ( "no-" )
94
- end
92
+ sample << ", [#{ dasherize ( 'no-' + human_name ) } ]" if inverse?
95
93
96
94
if aliases . empty?
97
95
( " " * padding ) << sample
@@ -100,6 +98,11 @@ def usage(padding = 0)
100
98
end
101
99
end
102
100
101
+ def inverse?
102
+ return false if ( name == "force" ) || name . start_with? ( "no-" )
103
+ boolean? && @inverse . nil? || @inverse . eql? ( true )
104
+ end
105
+
103
106
VALID_TYPES . each do |type |
104
107
class_eval <<-RUBY , __FILE__ , __LINE__ + 1
105
108
def #{ type } ?
Original file line number Diff line number Diff line change @@ -119,6 +119,18 @@ def option(name, options = {})
119
119
expect ( option ( "--foo" ) . switch_name ) . to eq ( "--foo" )
120
120
end
121
121
122
+ it "suppresses the creation of a --no-option when explicitly requested" do
123
+ expect ( option ( "bar" , type : :boolean , :inverse => false ) . usage ) . to_not include ( "[--no-bar]" )
124
+ end
125
+
126
+ it "creates the inversion flag by default" do
127
+ expect ( option ( "bar" , type : :boolean ) . usage ) . to include ( "[--no-bar]" )
128
+ end
129
+
130
+ it "creates the inversion flag when requested" do
131
+ expect ( option ( "bar" , type : :boolean , :inverse => true ) . usage ) . to include ( "[--no-bar]" )
132
+ end
133
+
122
134
it "returns the human name" do
123
135
expect ( option ( "foo" ) . human_name ) . to eq ( "foo" )
124
136
expect ( option ( "--foo" ) . human_name ) . to eq ( "foo" )
You can’t perform that action at this time.
0 commit comments