-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathoption.rb
More file actions
168 lines (145 loc) · 3.29 KB
/
option.rb
File metadata and controls
168 lines (145 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# frozen_string_literal: true
module Dry
class CLI
# Command line option
#
# @since 0.1.0
# @api private
class Option
# @since 0.1.0
# @api private
attr_reader :name
# @since 0.1.0
# @api private
attr_reader :options
# @since 0.1.0
# @api private
def initialize(name, options = {})
@name = name
@options = options
end
# @since 0.1.0
# @api private
def aliases
options[:aliases] || []
end
# @since 0.1.0
# @api private
def desc
desc = options[:desc]
values ? "#{desc}: (#{values.join("/")})" : desc
end
# @since 0.1.0
# @api private
def required?
options[:required]
end
# @since 0.1.0
# @api private
def type
options[:type]
end
# @since 0.1.0
# @api private
def values
options[:values]
end
# @since 0.1.0
# @api private
def boolean?
type == :boolean
end
# @api private
def flag?
type == :flag
end
# @since 0.3.0
# @api private
def array?
type == :array
end
# @since 2.0.0
# @api private
def integer?
type == :integer
end
# @since 2.0.0
# @api private
def float?
type == :float
end
# @since 0.1.0
# @api private
def default
options[:default]
end
# @since 0.1.0
# @api private
def description_name
options[:label] || name.upcase
end
# @since 0.1.0
# @api private
def argument?
false
end
# @since 0.1.0
# @api private
#
def parser_options
dasherized_name = Inflector.dasherize(name)
parser_options = []
if boolean?
parser_options << "--[no-]#{dasherized_name}"
elsif flag?
parser_options << "--#{dasherized_name}"
else
parser_options << "--#{dasherized_name}=#{name}"
parser_options << "--#{dasherized_name} #{name}"
end
parser_options << Array if array?
parser_options << values if values
parser_options.unshift(*alias_names) if aliases.any?
parser_options << desc if desc
parser_options
end
# @since 0.1.0
# @api private
def alias_names
aliases
.map { |name| name.gsub(/^-{1,2}/, "") }
.compact
.uniq
.map { |name| name.size == 1 ? "-#{name}" : "--#{name}" }
.map { |name| boolean? || flag? ? name : "#{name} VALUE" }
end
# @since 2.0.0
# @api private
# rubocop:disable Metrics/PerceivedComplexity
def type_cast(value)
return value if value.nil?
if integer?
value.to_i
elsif float?
value.to_f
elsif argument? && (boolean? || flag?)
%w[0 f false off].include?(value.downcase) ? false : true
else
value
end
end
# rubocop:enable Metrics/PerceivedComplexity
end
# Command line argument
#
# @since 0.1.0
# @api private
class Argument < Option
# @since 0.1.0
# @api private
def argument?
true
end
end
end
end