1
1
# frozen_string_literal: true
2
2
3
3
require 'English'
4
- require 'optparse '
4
+ require 'command_line_boss '
5
5
require 'create_github_release/command_line/options'
6
6
require 'create_github_release/version'
7
7
8
8
module CreateGithubRelease
9
9
module CommandLine
10
- # rubocop:disable Metrics/ClassLength
11
-
12
10
# Parses the options for this script
13
11
#
14
12
# @example Specify the release type
@@ -30,43 +28,8 @@ module CommandLine
30
28
#
31
29
# @api public
32
30
#
33
- class Parser
34
- # Create a new command line parser
35
- #
36
- # @example
37
- # parser = CommandLineParser.new
38
- #
39
- def initialize
40
- @option_parser = OptionParser . new
41
- define_options
42
- @options = CreateGithubRelease ::CommandLine ::Options . new
43
- end
44
-
45
- # Parse the command line arguements returning the options
46
- #
47
- # @example
48
- # parser = CommandLineParser.new
49
- # options = parser.parse(['major'])
50
- #
51
- # @param args [Array<String>] the command line arguments
52
- #
53
- # @return [CreateGithubRelease::CommandLine::Options] the options
54
- #
55
- def parse ( *args )
56
- begin
57
- option_parser . parse! ( remaining_args = args . dup )
58
- rescue OptionParser ::InvalidOption , OptionParser ::MissingArgument => e
59
- report_errors ( e . message )
60
- end
61
- parse_remaining_args ( remaining_args )
62
- # puts options unless options.quiet
63
- report_errors ( *options . errors ) unless options . valid?
64
- options
65
- end
66
-
67
- private
68
-
69
- # @!attribute [rw] options
31
+ class Parser < CommandLineBoss
32
+ # @!attribute [r] options
70
33
#
71
34
# The options to used for the create-github-release script
72
35
#
@@ -82,45 +45,26 @@ def parse(*args)
82
45
#
83
46
attr_reader :options
84
47
85
- # @!attribute [rw] option_parser
86
- #
87
- # The option parser
88
- #
89
- # @return [OptionParser] the option parser
90
- #
91
- # @api private
92
- #
93
- attr_reader :option_parser
94
-
95
- # Parse non-option arguments (the release type)
96
- # @return [void]
97
- # @api private
98
- def parse_remaining_args ( remaining_args )
99
- options . release_type = remaining_args . shift || nil
100
- report_errors ( 'Too many args' ) unless remaining_args . empty?
101
- end
48
+ private
102
49
103
- # An error message constructed from the given errors array
104
- # @return [String ]
50
+ # Set the default options
51
+ # @return [Void ]
105
52
# @api private
106
- def error_message ( errors )
107
- <<~MESSAGE
108
- #{ errors . map { |e | "ERROR: #{ e } " } . join ( "\n " ) }
109
-
110
- Use --help for usage
111
- MESSAGE
53
+ def set_defaults
54
+ @options = CreateGithubRelease ::CommandLine ::Options . new
112
55
end
113
56
114
- # Output an error message and useage to stderr and exit
57
+ # Parse non-option arguments (the release type)
115
58
# @return [void]
116
59
# @api private
117
- def report_errors ( *errors )
118
- warn error_message ( errors )
119
- exit 1
60
+ def parse_arguments
61
+ options . release_type = args . shift
120
62
end
121
63
122
64
# The banner for the option parser
123
- BANNER = <<~BANNER . freeze
65
+ # @return [String] the banner
66
+ # @api private
67
+ def banner = <<~BANNER . freeze
124
68
Usage:
125
69
#{ File . basename ( $PROGRAM_NAME) } --help | RELEASE_TYPE [options]
126
70
@@ -131,25 +75,11 @@ def report_errors(*errors)
131
75
Options:
132
76
BANNER
133
77
134
- # Define the options for OptionParser
135
- # @return [void]
136
- # @api private
137
- def define_options
138
- # @sg-ignore
139
- option_parser . banner = BANNER
140
- %i[
141
- define_help_option define_default_branch_option define_release_branch_option define_pre_option
142
- define_pre_type_option define_remote_option define_last_release_version_option define_version_option
143
- define_next_release_version_option define_release_pr_label_option define_changelog_path_option
144
- define_quiet_option define_verbose_option
145
- ] . each { |m | send ( m ) }
146
- end
147
-
148
78
# Define the release_pr_label option which requires a value
149
79
# @return [void]
150
80
# @api private
151
81
def define_release_pr_label_option
152
- option_parser . on ( '--release-pr-label=LABEL' , 'The label to apply to the release pull request' ) do |label |
82
+ parser . on ( '--release-pr-label=LABEL' , 'The label to apply to the release pull request' ) do |label |
153
83
options . release_pr_label = label
154
84
end
155
85
end
@@ -158,7 +88,7 @@ def define_release_pr_label_option
158
88
# @return [void]
159
89
# @api private
160
90
def define_pre_option
161
- option_parser . on ( '-p' , '--pre' , 'Create a pre-release' ) do |pre |
91
+ parser . on ( '-p' , '--pre' , 'Create a pre-release' ) do |pre |
162
92
options . pre = pre
163
93
end
164
94
end
@@ -168,7 +98,7 @@ def define_pre_option
168
98
# @api private
169
99
def define_pre_type_option
170
100
description = 'Type of pre-release to create (e.g. alpha, beta, etc.)'
171
- option_parser . on ( '-t' , '--pre-type=TYPE' , description ) do |pre_type |
101
+ parser . on ( '-t' , '--pre-type=TYPE' , description ) do |pre_type |
172
102
options . pre_type = pre_type
173
103
end
174
104
end
@@ -177,7 +107,7 @@ def define_pre_type_option
177
107
# @return [void]
178
108
# @api private
179
109
def define_quiet_option
180
- option_parser . on ( '-q' , '--[no-]quiet' , 'Do not show output' ) do |quiet |
110
+ parser . on ( '-q' , '--[no-]quiet' , 'Do not show output' ) do |quiet |
181
111
options . quiet = quiet
182
112
end
183
113
end
@@ -186,7 +116,7 @@ def define_quiet_option
186
116
# @return [void]
187
117
# @api private
188
118
def define_verbose_option
189
- option_parser . on ( '-V' , '--[no-]verbose' , 'Show extra output' ) do |verbose |
119
+ parser . on ( '-V' , '--[no-]verbose' , 'Show extra output' ) do |verbose |
190
120
options . verbose = verbose
191
121
end
192
122
end
@@ -195,8 +125,8 @@ def define_verbose_option
195
125
# @return [void]
196
126
# @api private
197
127
def define_help_option
198
- option_parser . on_tail ( '-h' , '--help' , 'Show this message' ) do
199
- puts option_parser
128
+ parser . on_tail ( '-h' , '--help' , 'Show this message' ) do
129
+ puts parser
200
130
exit 0
201
131
end
202
132
end
@@ -205,7 +135,7 @@ def define_help_option
205
135
# @return [void]
206
136
# @api private
207
137
def define_default_branch_option
208
- option_parser . on ( '--default-branch=BRANCH_NAME' , 'Override the default branch' ) do |name |
138
+ parser . on ( '--default-branch=BRANCH_NAME' , 'Override the default branch' ) do |name |
209
139
options . default_branch = name
210
140
end
211
141
end
@@ -214,7 +144,7 @@ def define_default_branch_option
214
144
# @return [void]
215
145
# @api private
216
146
def define_release_branch_option
217
- option_parser . on ( '--release-branch=BRANCH_NAME' , 'Override the release branch to create' ) do |name |
147
+ parser . on ( '--release-branch=BRANCH_NAME' , 'Override the release branch to create' ) do |name |
218
148
options . release_branch = name
219
149
end
220
150
end
@@ -223,7 +153,7 @@ def define_release_branch_option
223
153
# @return [void]
224
154
# @api private
225
155
def define_remote_option
226
- option_parser . on ( '--remote=REMOTE_NAME' , "Use this remote name instead of 'origin'" ) do |name |
156
+ parser . on ( '--remote=REMOTE_NAME' , "Use this remote name instead of 'origin'" ) do |name |
227
157
options . remote = name
228
158
end
229
159
end
@@ -232,8 +162,10 @@ def define_remote_option
232
162
# @return [void]
233
163
# @api private
234
164
def define_last_release_version_option
235
- option_parser . on ( '--last-release-version=VERSION' ,
236
- 'Use this version instead `gem-version-boss current`' ) do |version |
165
+ parser . on (
166
+ '--last-release-version=VERSION' ,
167
+ 'Use this version instead `gem-version-boss current`'
168
+ ) do |version |
237
169
options . last_release_version = version
238
170
end
239
171
end
@@ -242,7 +174,7 @@ def define_last_release_version_option
242
174
# @return [void]
243
175
# @api private
244
176
def define_next_release_version_option
245
- option_parser . on (
177
+ parser . on (
246
178
'--next-release-version=VERSION' ,
247
179
'Use this version instead `gem-version-boss next-RELEASE_TYPE`'
248
180
) do |version |
@@ -254,7 +186,7 @@ def define_next_release_version_option
254
186
# @return [void]
255
187
# @api private
256
188
def define_changelog_path_option
257
- option_parser . on ( '--changelog-path=PATH' , 'Use this file instead of CHANGELOG.md' ) do |name |
189
+ parser . on ( '--changelog-path=PATH' , 'Use this file instead of CHANGELOG.md' ) do |name |
258
190
options . changelog_path = name
259
191
end
260
192
end
@@ -263,12 +195,19 @@ def define_changelog_path_option
263
195
# @return [void]
264
196
# @api private
265
197
def define_version_option
266
- option_parser . on_tail ( '-v' , '--version' , 'Output the version of this script' ) do
198
+ parser . on_tail ( '-v' , '--version' , 'Output the version of this script' ) do
267
199
puts CreateGithubRelease ::VERSION
268
200
exit 0
269
201
end
270
202
end
203
+
204
+ # Validate the options once they have been parsed
205
+ # @return [Void]
206
+ # @raise [SystemExit] if the options are invalid
207
+ # @api private
208
+ def validate_options
209
+ options . errors . each { |error_message | add_error_message ( error_message ) }
210
+ end
271
211
end
272
- # rubocop:enable Metrics/ClassLength
273
212
end
274
213
end
0 commit comments