11# frozen_string_literal: true
22
33require 'English'
4- require 'optparse '
4+ require 'command_line_boss '
55require 'create_github_release/command_line/options'
66require 'create_github_release/version'
77
88module CreateGithubRelease
99 module CommandLine
10- # rubocop:disable Metrics/ClassLength
11-
1210 # Parses the options for this script
1311 #
1412 # @example Specify the release type
@@ -30,43 +28,8 @@ module CommandLine
3028 #
3129 # @api public
3230 #
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
7033 #
7134 # The options to used for the create-github-release script
7235 #
@@ -82,45 +45,26 @@ def parse(*args)
8245 #
8346 attr_reader :options
8447
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
10249
103- # An error message constructed from the given errors array
104- # @return [String ]
50+ # Set the default options
51+ # @return [Void ]
10552 # @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
11255 end
11356
114- # Output an error message and useage to stderr and exit
57+ # Parse non-option arguments (the release type)
11558 # @return [void]
11659 # @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
12062 end
12163
12264 # The banner for the option parser
123- BANNER = <<~BANNER . freeze
65+ # @return [String] the banner
66+ # @api private
67+ def banner = <<~BANNER . freeze
12468 Usage:
12569 #{ File . basename ( $PROGRAM_NAME) } --help | RELEASE_TYPE [options]
12670
@@ -131,25 +75,11 @@ def report_errors(*errors)
13175 Options:
13276 BANNER
13377
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-
14878 # Define the release_pr_label option which requires a value
14979 # @return [void]
15080 # @api private
15181 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 |
15383 options . release_pr_label = label
15484 end
15585 end
@@ -158,7 +88,7 @@ def define_release_pr_label_option
15888 # @return [void]
15989 # @api private
16090 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 |
16292 options . pre = pre
16393 end
16494 end
@@ -168,7 +98,7 @@ def define_pre_option
16898 # @api private
16999 def define_pre_type_option
170100 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 |
172102 options . pre_type = pre_type
173103 end
174104 end
@@ -177,7 +107,7 @@ def define_pre_type_option
177107 # @return [void]
178108 # @api private
179109 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 |
181111 options . quiet = quiet
182112 end
183113 end
@@ -186,7 +116,7 @@ def define_quiet_option
186116 # @return [void]
187117 # @api private
188118 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 |
190120 options . verbose = verbose
191121 end
192122 end
@@ -195,8 +125,8 @@ def define_verbose_option
195125 # @return [void]
196126 # @api private
197127 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
200130 exit 0
201131 end
202132 end
@@ -205,7 +135,7 @@ def define_help_option
205135 # @return [void]
206136 # @api private
207137 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 |
209139 options . default_branch = name
210140 end
211141 end
@@ -214,7 +144,7 @@ def define_default_branch_option
214144 # @return [void]
215145 # @api private
216146 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 |
218148 options . release_branch = name
219149 end
220150 end
@@ -223,7 +153,7 @@ def define_release_branch_option
223153 # @return [void]
224154 # @api private
225155 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 |
227157 options . remote = name
228158 end
229159 end
@@ -232,8 +162,10 @@ def define_remote_option
232162 # @return [void]
233163 # @api private
234164 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 |
237169 options . last_release_version = version
238170 end
239171 end
@@ -242,7 +174,7 @@ def define_last_release_version_option
242174 # @return [void]
243175 # @api private
244176 def define_next_release_version_option
245- option_parser . on (
177+ parser . on (
246178 '--next-release-version=VERSION' ,
247179 'Use this version instead `gem-version-boss next-RELEASE_TYPE`'
248180 ) do |version |
@@ -254,7 +186,7 @@ def define_next_release_version_option
254186 # @return [void]
255187 # @api private
256188 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 |
258190 options . changelog_path = name
259191 end
260192 end
@@ -263,12 +195,19 @@ def define_changelog_path_option
263195 # @return [void]
264196 # @api private
265197 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
267199 puts CreateGithubRelease ::VERSION
268200 exit 0
269201 end
270202 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
271211 end
272- # rubocop:enable Metrics/ClassLength
273212 end
274213end
0 commit comments