honeybadger 5.0.2 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +713 -701
- data/LICENSE +19 -19
- data/README.md +57 -57
- data/TROUBLESHOOTING.md +3 -3
- data/bin/honeybadger +5 -5
- data/lib/honeybadger/agent.rb +488 -488
- data/lib/honeybadger/backend/base.rb +116 -116
- data/lib/honeybadger/backend/debug.rb +22 -22
- data/lib/honeybadger/backend/null.rb +29 -29
- data/lib/honeybadger/backend/server.rb +62 -62
- data/lib/honeybadger/backend/test.rb +46 -46
- data/lib/honeybadger/backend.rb +27 -27
- data/lib/honeybadger/backtrace.rb +181 -181
- data/lib/honeybadger/breadcrumbs/active_support.rb +119 -119
- data/lib/honeybadger/breadcrumbs/breadcrumb.rb +53 -53
- data/lib/honeybadger/breadcrumbs/collector.rb +82 -82
- data/lib/honeybadger/breadcrumbs/logging.rb +51 -51
- data/lib/honeybadger/breadcrumbs/ring_buffer.rb +44 -44
- data/lib/honeybadger/breadcrumbs.rb +8 -8
- data/lib/honeybadger/cli/deploy.rb +43 -43
- data/lib/honeybadger/cli/exec.rb +143 -143
- data/lib/honeybadger/cli/helpers.rb +28 -28
- data/lib/honeybadger/cli/heroku.rb +129 -129
- data/lib/honeybadger/cli/install.rb +101 -101
- data/lib/honeybadger/cli/main.rb +237 -237
- data/lib/honeybadger/cli/notify.rb +67 -67
- data/lib/honeybadger/cli/test.rb +267 -267
- data/lib/honeybadger/cli.rb +14 -14
- data/lib/honeybadger/config/defaults.rb +336 -333
- data/lib/honeybadger/config/env.rb +42 -42
- data/lib/honeybadger/config/ruby.rb +146 -146
- data/lib/honeybadger/config/yaml.rb +76 -76
- data/lib/honeybadger/config.rb +413 -413
- data/lib/honeybadger/const.rb +20 -20
- data/lib/honeybadger/context_manager.rb +55 -55
- data/lib/honeybadger/conversions.rb +16 -16
- data/lib/honeybadger/init/rails.rb +38 -38
- data/lib/honeybadger/init/rake.rb +66 -66
- data/lib/honeybadger/init/ruby.rb +11 -11
- data/lib/honeybadger/init/sinatra.rb +51 -51
- data/lib/honeybadger/logging.rb +177 -177
- data/lib/honeybadger/notice.rb +579 -568
- data/lib/honeybadger/plugin.rb +210 -210
- data/lib/honeybadger/plugins/breadcrumbs.rb +111 -111
- data/lib/honeybadger/plugins/delayed_job/plugin.rb +56 -56
- data/lib/honeybadger/plugins/delayed_job.rb +22 -22
- data/lib/honeybadger/plugins/faktory.rb +52 -52
- data/lib/honeybadger/plugins/lambda.rb +71 -71
- data/lib/honeybadger/plugins/local_variables.rb +44 -44
- data/lib/honeybadger/plugins/passenger.rb +23 -23
- data/lib/honeybadger/plugins/rails.rb +72 -63
- data/lib/honeybadger/plugins/resque.rb +72 -72
- data/lib/honeybadger/plugins/shoryuken.rb +52 -52
- data/lib/honeybadger/plugins/sidekiq.rb +71 -62
- data/lib/honeybadger/plugins/sucker_punch.rb +18 -18
- data/lib/honeybadger/plugins/thor.rb +32 -32
- data/lib/honeybadger/plugins/warden.rb +19 -19
- data/lib/honeybadger/rack/error_notifier.rb +92 -92
- data/lib/honeybadger/rack/user_feedback.rb +88 -88
- data/lib/honeybadger/rack/user_informer.rb +45 -45
- data/lib/honeybadger/ruby.rb +2 -2
- data/lib/honeybadger/singleton.rb +103 -103
- data/lib/honeybadger/tasks.rb +22 -22
- data/lib/honeybadger/templates/feedback_form.erb +84 -84
- data/lib/honeybadger/util/http.rb +92 -92
- data/lib/honeybadger/util/lambda.rb +32 -32
- data/lib/honeybadger/util/request_hash.rb +73 -73
- data/lib/honeybadger/util/request_payload.rb +41 -41
- data/lib/honeybadger/util/revision.rb +39 -39
- data/lib/honeybadger/util/sanitizer.rb +214 -214
- data/lib/honeybadger/util/sql.rb +34 -34
- data/lib/honeybadger/util/stats.rb +50 -50
- data/lib/honeybadger/version.rb +4 -4
- data/lib/honeybadger/worker.rb +253 -253
- data/lib/honeybadger.rb +11 -11
- data/resources/ca-bundle.crt +3376 -3376
- data/vendor/capistrano-honeybadger/lib/capistrano/honeybadger.rb +5 -5
- data/vendor/capistrano-honeybadger/lib/capistrano/tasks/deploy.cap +89 -89
- data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano/legacy.rb +47 -47
- data/vendor/capistrano-honeybadger/lib/honeybadger/capistrano.rb +2 -2
- data/vendor/cli/inifile.rb +628 -628
- data/vendor/cli/thor/actions/create_file.rb +103 -103
- data/vendor/cli/thor/actions/create_link.rb +59 -59
- data/vendor/cli/thor/actions/directory.rb +118 -118
- data/vendor/cli/thor/actions/empty_directory.rb +135 -135
- data/vendor/cli/thor/actions/file_manipulation.rb +316 -316
- data/vendor/cli/thor/actions/inject_into_file.rb +107 -107
- data/vendor/cli/thor/actions.rb +319 -319
- data/vendor/cli/thor/base.rb +656 -656
- data/vendor/cli/thor/command.rb +133 -133
- data/vendor/cli/thor/core_ext/hash_with_indifferent_access.rb +77 -77
- data/vendor/cli/thor/core_ext/io_binary_read.rb +10 -10
- data/vendor/cli/thor/core_ext/ordered_hash.rb +98 -98
- data/vendor/cli/thor/error.rb +32 -32
- data/vendor/cli/thor/group.rb +281 -281
- data/vendor/cli/thor/invocation.rb +178 -178
- data/vendor/cli/thor/line_editor/basic.rb +35 -35
- data/vendor/cli/thor/line_editor/readline.rb +88 -88
- data/vendor/cli/thor/line_editor.rb +17 -17
- data/vendor/cli/thor/parser/argument.rb +73 -73
- data/vendor/cli/thor/parser/arguments.rb +175 -175
- data/vendor/cli/thor/parser/option.rb +125 -125
- data/vendor/cli/thor/parser/options.rb +218 -218
- data/vendor/cli/thor/parser.rb +4 -4
- data/vendor/cli/thor/rake_compat.rb +71 -71
- data/vendor/cli/thor/runner.rb +322 -322
- data/vendor/cli/thor/shell/basic.rb +421 -421
- data/vendor/cli/thor/shell/color.rb +149 -149
- data/vendor/cli/thor/shell/html.rb +126 -126
- data/vendor/cli/thor/shell.rb +81 -81
- data/vendor/cli/thor/util.rb +267 -267
- data/vendor/cli/thor/version.rb +3 -3
- data/vendor/cli/thor.rb +484 -484
- metadata +10 -5
@@ -1,125 +1,125 @@
|
|
1
|
-
class Thor
|
2
|
-
class Option < Argument #:nodoc:
|
3
|
-
attr_reader :aliases, :group, :lazy_default, :hide
|
4
|
-
|
5
|
-
VALID_TYPES = [:boolean, :numeric, :hash, :array, :string]
|
6
|
-
|
7
|
-
def initialize(name, options = {})
|
8
|
-
options[:required] = false unless options.key?(:required)
|
9
|
-
super
|
10
|
-
@lazy_default = options[:lazy_default]
|
11
|
-
@group = options[:group].to_s.capitalize if options[:group]
|
12
|
-
@aliases = Array(options[:aliases])
|
13
|
-
@hide = options[:hide]
|
14
|
-
end
|
15
|
-
|
16
|
-
# This parse quick options given as method_options. It makes several
|
17
|
-
# assumptions, but you can be more specific using the option method.
|
18
|
-
#
|
19
|
-
# parse :foo => "bar"
|
20
|
-
# #=> Option foo with default value bar
|
21
|
-
#
|
22
|
-
# parse [:foo, :baz] => "bar"
|
23
|
-
# #=> Option foo with default value bar and alias :baz
|
24
|
-
#
|
25
|
-
# parse :foo => :required
|
26
|
-
# #=> Required option foo without default value
|
27
|
-
#
|
28
|
-
# parse :foo => 2
|
29
|
-
# #=> Option foo with default value 2 and type numeric
|
30
|
-
#
|
31
|
-
# parse :foo => :numeric
|
32
|
-
# #=> Option foo without default value and type numeric
|
33
|
-
#
|
34
|
-
# parse :foo => true
|
35
|
-
# #=> Option foo with default value true and type boolean
|
36
|
-
#
|
37
|
-
# The valid types are :boolean, :numeric, :hash, :array and :string. If none
|
38
|
-
# is given a default type is assumed. This default type accepts arguments as
|
39
|
-
# string (--foo=value) or booleans (just --foo).
|
40
|
-
#
|
41
|
-
# By default all options are optional, unless :required is given.
|
42
|
-
#
|
43
|
-
def self.parse(key, value) # rubocop:disable MethodLength
|
44
|
-
if key.is_a?(Array)
|
45
|
-
name, *aliases = key
|
46
|
-
else
|
47
|
-
name, aliases = key, []
|
48
|
-
end
|
49
|
-
|
50
|
-
name = name.to_s
|
51
|
-
default = value
|
52
|
-
|
53
|
-
type = case value
|
54
|
-
when Symbol
|
55
|
-
default = nil
|
56
|
-
if VALID_TYPES.include?(value)
|
57
|
-
value
|
58
|
-
elsif required = (value == :required) # rubocop:disable AssignmentInCondition
|
59
|
-
:string
|
60
|
-
end
|
61
|
-
when TrueClass, FalseClass
|
62
|
-
:boolean
|
63
|
-
when Numeric
|
64
|
-
:numeric
|
65
|
-
when Hash, Array, String
|
66
|
-
value.class.name.downcase.to_sym
|
67
|
-
end
|
68
|
-
new(name.to_s, :required => required, :type => type, :default => default, :aliases => aliases)
|
69
|
-
end
|
70
|
-
|
71
|
-
def switch_name
|
72
|
-
@switch_name ||= dasherized? ? name : dasherize(name)
|
73
|
-
end
|
74
|
-
|
75
|
-
def human_name
|
76
|
-
@human_name ||= dasherized? ? undasherize(name) : name
|
77
|
-
end
|
78
|
-
|
79
|
-
def usage(padding = 0)
|
80
|
-
sample = if banner && !banner.to_s.empty?
|
81
|
-
"#{switch_name}=#{banner}"
|
82
|
-
else
|
83
|
-
switch_name
|
84
|
-
end
|
85
|
-
|
86
|
-
sample = "[#{sample}]" unless required?
|
87
|
-
|
88
|
-
if boolean?
|
89
|
-
sample << ", [#{dasherize("no-" + human_name)}]" unless name == "force"
|
90
|
-
end
|
91
|
-
|
92
|
-
if aliases.empty?
|
93
|
-
(" " * padding) << sample
|
94
|
-
else
|
95
|
-
"#{aliases.join(', ')}, #{sample}"
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
VALID_TYPES.each do |type|
|
100
|
-
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
101
|
-
def #{type}?
|
102
|
-
self.type == #{type.inspect}
|
103
|
-
end
|
104
|
-
RUBY
|
105
|
-
end
|
106
|
-
|
107
|
-
protected
|
108
|
-
|
109
|
-
def validate!
|
110
|
-
fail ArgumentError, "An option cannot be boolean and required." if boolean? && required?
|
111
|
-
end
|
112
|
-
|
113
|
-
def dasherized?
|
114
|
-
name.index("-") == 0
|
115
|
-
end
|
116
|
-
|
117
|
-
def undasherize(str)
|
118
|
-
str.sub(/^-{1,2}/, "")
|
119
|
-
end
|
120
|
-
|
121
|
-
def dasherize(str)
|
122
|
-
(str.length > 1 ? "--" : "-") + str.gsub("_", "-")
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
1
|
+
class Thor
|
2
|
+
class Option < Argument #:nodoc:
|
3
|
+
attr_reader :aliases, :group, :lazy_default, :hide
|
4
|
+
|
5
|
+
VALID_TYPES = [:boolean, :numeric, :hash, :array, :string]
|
6
|
+
|
7
|
+
def initialize(name, options = {})
|
8
|
+
options[:required] = false unless options.key?(:required)
|
9
|
+
super
|
10
|
+
@lazy_default = options[:lazy_default]
|
11
|
+
@group = options[:group].to_s.capitalize if options[:group]
|
12
|
+
@aliases = Array(options[:aliases])
|
13
|
+
@hide = options[:hide]
|
14
|
+
end
|
15
|
+
|
16
|
+
# This parse quick options given as method_options. It makes several
|
17
|
+
# assumptions, but you can be more specific using the option method.
|
18
|
+
#
|
19
|
+
# parse :foo => "bar"
|
20
|
+
# #=> Option foo with default value bar
|
21
|
+
#
|
22
|
+
# parse [:foo, :baz] => "bar"
|
23
|
+
# #=> Option foo with default value bar and alias :baz
|
24
|
+
#
|
25
|
+
# parse :foo => :required
|
26
|
+
# #=> Required option foo without default value
|
27
|
+
#
|
28
|
+
# parse :foo => 2
|
29
|
+
# #=> Option foo with default value 2 and type numeric
|
30
|
+
#
|
31
|
+
# parse :foo => :numeric
|
32
|
+
# #=> Option foo without default value and type numeric
|
33
|
+
#
|
34
|
+
# parse :foo => true
|
35
|
+
# #=> Option foo with default value true and type boolean
|
36
|
+
#
|
37
|
+
# The valid types are :boolean, :numeric, :hash, :array and :string. If none
|
38
|
+
# is given a default type is assumed. This default type accepts arguments as
|
39
|
+
# string (--foo=value) or booleans (just --foo).
|
40
|
+
#
|
41
|
+
# By default all options are optional, unless :required is given.
|
42
|
+
#
|
43
|
+
def self.parse(key, value) # rubocop:disable MethodLength
|
44
|
+
if key.is_a?(Array)
|
45
|
+
name, *aliases = key
|
46
|
+
else
|
47
|
+
name, aliases = key, []
|
48
|
+
end
|
49
|
+
|
50
|
+
name = name.to_s
|
51
|
+
default = value
|
52
|
+
|
53
|
+
type = case value
|
54
|
+
when Symbol
|
55
|
+
default = nil
|
56
|
+
if VALID_TYPES.include?(value)
|
57
|
+
value
|
58
|
+
elsif required = (value == :required) # rubocop:disable AssignmentInCondition
|
59
|
+
:string
|
60
|
+
end
|
61
|
+
when TrueClass, FalseClass
|
62
|
+
:boolean
|
63
|
+
when Numeric
|
64
|
+
:numeric
|
65
|
+
when Hash, Array, String
|
66
|
+
value.class.name.downcase.to_sym
|
67
|
+
end
|
68
|
+
new(name.to_s, :required => required, :type => type, :default => default, :aliases => aliases)
|
69
|
+
end
|
70
|
+
|
71
|
+
def switch_name
|
72
|
+
@switch_name ||= dasherized? ? name : dasherize(name)
|
73
|
+
end
|
74
|
+
|
75
|
+
def human_name
|
76
|
+
@human_name ||= dasherized? ? undasherize(name) : name
|
77
|
+
end
|
78
|
+
|
79
|
+
def usage(padding = 0)
|
80
|
+
sample = if banner && !banner.to_s.empty?
|
81
|
+
"#{switch_name}=#{banner}"
|
82
|
+
else
|
83
|
+
switch_name
|
84
|
+
end
|
85
|
+
|
86
|
+
sample = "[#{sample}]" unless required?
|
87
|
+
|
88
|
+
if boolean?
|
89
|
+
sample << ", [#{dasherize("no-" + human_name)}]" unless name == "force"
|
90
|
+
end
|
91
|
+
|
92
|
+
if aliases.empty?
|
93
|
+
(" " * padding) << sample
|
94
|
+
else
|
95
|
+
"#{aliases.join(', ')}, #{sample}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
VALID_TYPES.each do |type|
|
100
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
101
|
+
def #{type}?
|
102
|
+
self.type == #{type.inspect}
|
103
|
+
end
|
104
|
+
RUBY
|
105
|
+
end
|
106
|
+
|
107
|
+
protected
|
108
|
+
|
109
|
+
def validate!
|
110
|
+
fail ArgumentError, "An option cannot be boolean and required." if boolean? && required?
|
111
|
+
end
|
112
|
+
|
113
|
+
def dasherized?
|
114
|
+
name.index("-") == 0
|
115
|
+
end
|
116
|
+
|
117
|
+
def undasherize(str)
|
118
|
+
str.sub(/^-{1,2}/, "")
|
119
|
+
end
|
120
|
+
|
121
|
+
def dasherize(str)
|
122
|
+
(str.length > 1 ? "--" : "-") + str.gsub("_", "-")
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -1,218 +1,218 @@
|
|
1
|
-
class Thor
|
2
|
-
class Options < Arguments #:nodoc: # rubocop:disable ClassLength
|
3
|
-
LONG_RE = /^(--\w+(?:-\w+)*)$/
|
4
|
-
SHORT_RE = /^(-[a-z])$/i
|
5
|
-
EQ_RE = /^(--\w+(?:-\w+)*|-[a-z])=(.*)$/i
|
6
|
-
SHORT_SQ_RE = /^-([a-z]{2,})$/i # Allow either -x -v or -xv style for single char args
|
7
|
-
SHORT_NUM = /^(-[a-z])#{NUMERIC}$/i
|
8
|
-
OPTS_END = "--".freeze
|
9
|
-
|
10
|
-
# Receives a hash and makes it switches.
|
11
|
-
def self.to_switches(options)
|
12
|
-
options.map do |key, value|
|
13
|
-
case value
|
14
|
-
when true
|
15
|
-
"--#{key}"
|
16
|
-
when Array
|
17
|
-
"--#{key} #{value.map { |v| v.inspect }.join(' ')}"
|
18
|
-
when Hash
|
19
|
-
"--#{key} #{value.map { |k, v| "#{k}:#{v}" }.join(' ')}"
|
20
|
-
when nil, false
|
21
|
-
""
|
22
|
-
else
|
23
|
-
"--#{key} #{value.inspect}"
|
24
|
-
end
|
25
|
-
end.join(" ")
|
26
|
-
end
|
27
|
-
|
28
|
-
# Takes a hash of Thor::Option and a hash with defaults.
|
29
|
-
#
|
30
|
-
# If +stop_on_unknown+ is true, #parse will stop as soon as it encounters
|
31
|
-
# an unknown option or a regular argument.
|
32
|
-
def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false)
|
33
|
-
@stop_on_unknown = stop_on_unknown
|
34
|
-
options = hash_options.values
|
35
|
-
super(options)
|
36
|
-
|
37
|
-
# Add defaults
|
38
|
-
defaults.each do |key, value|
|
39
|
-
@assigns[key.to_s] = value
|
40
|
-
@non_assigned_required.delete(hash_options[key])
|
41
|
-
end
|
42
|
-
|
43
|
-
@shorts, @switches, @extra = {}, {}, []
|
44
|
-
|
45
|
-
options.each do |option|
|
46
|
-
@switches[option.switch_name] = option
|
47
|
-
|
48
|
-
option.aliases.each do |short|
|
49
|
-
name = short.to_s.sub(/^(?!\-)/, "-")
|
50
|
-
@shorts[name] ||= option.switch_name
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def remaining # rubocop:disable TrivialAccessors
|
56
|
-
@extra
|
57
|
-
end
|
58
|
-
|
59
|
-
def peek
|
60
|
-
return super unless @parsing_options
|
61
|
-
|
62
|
-
result = super
|
63
|
-
if result == OPTS_END
|
64
|
-
shift
|
65
|
-
@parsing_options = false
|
66
|
-
super
|
67
|
-
else
|
68
|
-
result
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def parse(args) # rubocop:disable MethodLength
|
73
|
-
@pile = args.dup
|
74
|
-
@parsing_options = true
|
75
|
-
|
76
|
-
while peek
|
77
|
-
if parsing_options?
|
78
|
-
match, is_switch = current_is_switch?
|
79
|
-
shifted = shift
|
80
|
-
|
81
|
-
if is_switch
|
82
|
-
case shifted
|
83
|
-
when SHORT_SQ_RE
|
84
|
-
unshift($1.split("").map { |f| "-#{f}" })
|
85
|
-
next
|
86
|
-
when EQ_RE, SHORT_NUM
|
87
|
-
unshift($2)
|
88
|
-
switch = $1
|
89
|
-
when LONG_RE, SHORT_RE
|
90
|
-
switch = $1
|
91
|
-
end
|
92
|
-
|
93
|
-
switch = normalize_switch(switch)
|
94
|
-
option = switch_option(switch)
|
95
|
-
@assigns[option.human_name] = parse_peek(switch, option)
|
96
|
-
elsif @stop_on_unknown
|
97
|
-
@parsing_options = false
|
98
|
-
@extra << shifted
|
99
|
-
@extra << shift while peek
|
100
|
-
break
|
101
|
-
elsif match
|
102
|
-
@extra << shifted
|
103
|
-
@extra << shift while peek && peek !~ /^-/
|
104
|
-
else
|
105
|
-
@extra << shifted
|
106
|
-
end
|
107
|
-
else
|
108
|
-
@extra << shift
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
check_requirement!
|
113
|
-
|
114
|
-
assigns = Thor::CoreExt::HashWithIndifferentAccess.new(@assigns)
|
115
|
-
assigns.freeze
|
116
|
-
assigns
|
117
|
-
end
|
118
|
-
|
119
|
-
def check_unknown!
|
120
|
-
# an unknown option starts with - or -- and has no more --'s afterward.
|
121
|
-
unknown = @extra.select { |str| str =~ /^--?(?:(?!--).)*$/ }
|
122
|
-
fail UnknownArgumentError, "Unknown switches '#{unknown.join(', ')}'" unless unknown.empty?
|
123
|
-
end
|
124
|
-
|
125
|
-
protected
|
126
|
-
|
127
|
-
# Check if the current value in peek is a registered switch.
|
128
|
-
#
|
129
|
-
# Two booleans are returned. The first is true if the current value
|
130
|
-
# starts with a hyphen; the second is true if it is a registered switch.
|
131
|
-
def current_is_switch?
|
132
|
-
case peek
|
133
|
-
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM
|
134
|
-
[true, switch?($1)]
|
135
|
-
when SHORT_SQ_RE
|
136
|
-
[true, $1.split("").any? { |f| switch?("-#{f}") }]
|
137
|
-
else
|
138
|
-
[false, false]
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def current_is_switch_formatted?
|
143
|
-
case peek
|
144
|
-
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM, SHORT_SQ_RE
|
145
|
-
true
|
146
|
-
else
|
147
|
-
false
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
def current_is_value?
|
152
|
-
peek && (!parsing_options? || super)
|
153
|
-
end
|
154
|
-
|
155
|
-
def switch?(arg)
|
156
|
-
switch_option(normalize_switch(arg))
|
157
|
-
end
|
158
|
-
|
159
|
-
def switch_option(arg)
|
160
|
-
if match = no_or_skip?(arg) # rubocop:disable AssignmentInCondition
|
161
|
-
@switches[arg] || @switches["--#{match}"]
|
162
|
-
else
|
163
|
-
@switches[arg]
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
# Check if the given argument is actually a shortcut.
|
168
|
-
#
|
169
|
-
def normalize_switch(arg)
|
170
|
-
(@shorts[arg] || arg).tr("_", "-")
|
171
|
-
end
|
172
|
-
|
173
|
-
def parsing_options?
|
174
|
-
peek
|
175
|
-
@parsing_options
|
176
|
-
end
|
177
|
-
|
178
|
-
# Parse boolean values which can be given as --foo=true, --foo or --no-foo.
|
179
|
-
#
|
180
|
-
def parse_boolean(switch)
|
181
|
-
if current_is_value?
|
182
|
-
if ["true", "TRUE", "t", "T", true].include?(peek)
|
183
|
-
shift
|
184
|
-
true
|
185
|
-
elsif ["false", "FALSE", "f", "F", false].include?(peek)
|
186
|
-
shift
|
187
|
-
false
|
188
|
-
else
|
189
|
-
true
|
190
|
-
end
|
191
|
-
else
|
192
|
-
@switches.key?(switch) || !no_or_skip?(switch)
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
# Parse the value at the peek analyzing if it requires an input or not.
|
197
|
-
#
|
198
|
-
def parse_peek(switch, option)
|
199
|
-
if parsing_options? && (current_is_switch_formatted? || last?)
|
200
|
-
if option.boolean?
|
201
|
-
# No problem for boolean types
|
202
|
-
elsif no_or_skip?(switch)
|
203
|
-
return nil # User set value to nil
|
204
|
-
elsif option.string? && !option.required?
|
205
|
-
# Return the default if there is one, else the human name
|
206
|
-
return option.lazy_default || option.default || option.human_name
|
207
|
-
elsif option.lazy_default
|
208
|
-
return option.lazy_default
|
209
|
-
else
|
210
|
-
fail MalformattedArgumentError, "No value provided for option '#{switch}'"
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
@non_assigned_required.delete(option)
|
215
|
-
send(:"parse_#{option.type}", switch)
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
1
|
+
class Thor
|
2
|
+
class Options < Arguments #:nodoc: # rubocop:disable ClassLength
|
3
|
+
LONG_RE = /^(--\w+(?:-\w+)*)$/
|
4
|
+
SHORT_RE = /^(-[a-z])$/i
|
5
|
+
EQ_RE = /^(--\w+(?:-\w+)*|-[a-z])=(.*)$/i
|
6
|
+
SHORT_SQ_RE = /^-([a-z]{2,})$/i # Allow either -x -v or -xv style for single char args
|
7
|
+
SHORT_NUM = /^(-[a-z])#{NUMERIC}$/i
|
8
|
+
OPTS_END = "--".freeze
|
9
|
+
|
10
|
+
# Receives a hash and makes it switches.
|
11
|
+
def self.to_switches(options)
|
12
|
+
options.map do |key, value|
|
13
|
+
case value
|
14
|
+
when true
|
15
|
+
"--#{key}"
|
16
|
+
when Array
|
17
|
+
"--#{key} #{value.map { |v| v.inspect }.join(' ')}"
|
18
|
+
when Hash
|
19
|
+
"--#{key} #{value.map { |k, v| "#{k}:#{v}" }.join(' ')}"
|
20
|
+
when nil, false
|
21
|
+
""
|
22
|
+
else
|
23
|
+
"--#{key} #{value.inspect}"
|
24
|
+
end
|
25
|
+
end.join(" ")
|
26
|
+
end
|
27
|
+
|
28
|
+
# Takes a hash of Thor::Option and a hash with defaults.
|
29
|
+
#
|
30
|
+
# If +stop_on_unknown+ is true, #parse will stop as soon as it encounters
|
31
|
+
# an unknown option or a regular argument.
|
32
|
+
def initialize(hash_options = {}, defaults = {}, stop_on_unknown = false)
|
33
|
+
@stop_on_unknown = stop_on_unknown
|
34
|
+
options = hash_options.values
|
35
|
+
super(options)
|
36
|
+
|
37
|
+
# Add defaults
|
38
|
+
defaults.each do |key, value|
|
39
|
+
@assigns[key.to_s] = value
|
40
|
+
@non_assigned_required.delete(hash_options[key])
|
41
|
+
end
|
42
|
+
|
43
|
+
@shorts, @switches, @extra = {}, {}, []
|
44
|
+
|
45
|
+
options.each do |option|
|
46
|
+
@switches[option.switch_name] = option
|
47
|
+
|
48
|
+
option.aliases.each do |short|
|
49
|
+
name = short.to_s.sub(/^(?!\-)/, "-")
|
50
|
+
@shorts[name] ||= option.switch_name
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def remaining # rubocop:disable TrivialAccessors
|
56
|
+
@extra
|
57
|
+
end
|
58
|
+
|
59
|
+
def peek
|
60
|
+
return super unless @parsing_options
|
61
|
+
|
62
|
+
result = super
|
63
|
+
if result == OPTS_END
|
64
|
+
shift
|
65
|
+
@parsing_options = false
|
66
|
+
super
|
67
|
+
else
|
68
|
+
result
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def parse(args) # rubocop:disable MethodLength
|
73
|
+
@pile = args.dup
|
74
|
+
@parsing_options = true
|
75
|
+
|
76
|
+
while peek
|
77
|
+
if parsing_options?
|
78
|
+
match, is_switch = current_is_switch?
|
79
|
+
shifted = shift
|
80
|
+
|
81
|
+
if is_switch
|
82
|
+
case shifted
|
83
|
+
when SHORT_SQ_RE
|
84
|
+
unshift($1.split("").map { |f| "-#{f}" })
|
85
|
+
next
|
86
|
+
when EQ_RE, SHORT_NUM
|
87
|
+
unshift($2)
|
88
|
+
switch = $1
|
89
|
+
when LONG_RE, SHORT_RE
|
90
|
+
switch = $1
|
91
|
+
end
|
92
|
+
|
93
|
+
switch = normalize_switch(switch)
|
94
|
+
option = switch_option(switch)
|
95
|
+
@assigns[option.human_name] = parse_peek(switch, option)
|
96
|
+
elsif @stop_on_unknown
|
97
|
+
@parsing_options = false
|
98
|
+
@extra << shifted
|
99
|
+
@extra << shift while peek
|
100
|
+
break
|
101
|
+
elsif match
|
102
|
+
@extra << shifted
|
103
|
+
@extra << shift while peek && peek !~ /^-/
|
104
|
+
else
|
105
|
+
@extra << shifted
|
106
|
+
end
|
107
|
+
else
|
108
|
+
@extra << shift
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
check_requirement!
|
113
|
+
|
114
|
+
assigns = Thor::CoreExt::HashWithIndifferentAccess.new(@assigns)
|
115
|
+
assigns.freeze
|
116
|
+
assigns
|
117
|
+
end
|
118
|
+
|
119
|
+
def check_unknown!
|
120
|
+
# an unknown option starts with - or -- and has no more --'s afterward.
|
121
|
+
unknown = @extra.select { |str| str =~ /^--?(?:(?!--).)*$/ }
|
122
|
+
fail UnknownArgumentError, "Unknown switches '#{unknown.join(', ')}'" unless unknown.empty?
|
123
|
+
end
|
124
|
+
|
125
|
+
protected
|
126
|
+
|
127
|
+
# Check if the current value in peek is a registered switch.
|
128
|
+
#
|
129
|
+
# Two booleans are returned. The first is true if the current value
|
130
|
+
# starts with a hyphen; the second is true if it is a registered switch.
|
131
|
+
def current_is_switch?
|
132
|
+
case peek
|
133
|
+
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM
|
134
|
+
[true, switch?($1)]
|
135
|
+
when SHORT_SQ_RE
|
136
|
+
[true, $1.split("").any? { |f| switch?("-#{f}") }]
|
137
|
+
else
|
138
|
+
[false, false]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def current_is_switch_formatted?
|
143
|
+
case peek
|
144
|
+
when LONG_RE, SHORT_RE, EQ_RE, SHORT_NUM, SHORT_SQ_RE
|
145
|
+
true
|
146
|
+
else
|
147
|
+
false
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def current_is_value?
|
152
|
+
peek && (!parsing_options? || super)
|
153
|
+
end
|
154
|
+
|
155
|
+
def switch?(arg)
|
156
|
+
switch_option(normalize_switch(arg))
|
157
|
+
end
|
158
|
+
|
159
|
+
def switch_option(arg)
|
160
|
+
if match = no_or_skip?(arg) # rubocop:disable AssignmentInCondition
|
161
|
+
@switches[arg] || @switches["--#{match}"]
|
162
|
+
else
|
163
|
+
@switches[arg]
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# Check if the given argument is actually a shortcut.
|
168
|
+
#
|
169
|
+
def normalize_switch(arg)
|
170
|
+
(@shorts[arg] || arg).tr("_", "-")
|
171
|
+
end
|
172
|
+
|
173
|
+
def parsing_options?
|
174
|
+
peek
|
175
|
+
@parsing_options
|
176
|
+
end
|
177
|
+
|
178
|
+
# Parse boolean values which can be given as --foo=true, --foo or --no-foo.
|
179
|
+
#
|
180
|
+
def parse_boolean(switch)
|
181
|
+
if current_is_value?
|
182
|
+
if ["true", "TRUE", "t", "T", true].include?(peek)
|
183
|
+
shift
|
184
|
+
true
|
185
|
+
elsif ["false", "FALSE", "f", "F", false].include?(peek)
|
186
|
+
shift
|
187
|
+
false
|
188
|
+
else
|
189
|
+
true
|
190
|
+
end
|
191
|
+
else
|
192
|
+
@switches.key?(switch) || !no_or_skip?(switch)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# Parse the value at the peek analyzing if it requires an input or not.
|
197
|
+
#
|
198
|
+
def parse_peek(switch, option)
|
199
|
+
if parsing_options? && (current_is_switch_formatted? || last?)
|
200
|
+
if option.boolean?
|
201
|
+
# No problem for boolean types
|
202
|
+
elsif no_or_skip?(switch)
|
203
|
+
return nil # User set value to nil
|
204
|
+
elsif option.string? && !option.required?
|
205
|
+
# Return the default if there is one, else the human name
|
206
|
+
return option.lazy_default || option.default || option.human_name
|
207
|
+
elsif option.lazy_default
|
208
|
+
return option.lazy_default
|
209
|
+
else
|
210
|
+
fail MalformattedArgumentError, "No value provided for option '#{switch}'"
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
@non_assigned_required.delete(option)
|
215
|
+
send(:"parse_#{option.type}", switch)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|