Skip to content

Commit fa6ddb2

Browse files
authored
Add option to create and revert scripts to show version (#48)
1 parent 954bea7 commit fa6ddb2

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

exe/revert-github-release

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ class Parser
7474
COMMAND
7575
end
7676

77-
DESCRIPTION = <<~DESCRIPTION
77+
BANNER = <<~BANNER.freeze
78+
Usage:
79+
#{File.basename($PROGRAM_NAME)} [--help | --version]
80+
7881
This script reverts the effect of running the create-github-release script.
7982
It must be run in the root directory of the work tree with the release
8083
branch checked out (which is the state create-github-release leaves you in).
@@ -84,20 +87,17 @@ class Parser
8487
This script removes the release branch and release tag both in the local
8588
repository and on the remote. Deleting the branch on GitHub will close the
8689
GitHub PR automatically.
87-
DESCRIPTION
90+
91+
Options:
92+
BANNER
8893

8994
# Define the options for OptionParser
9095
# @return [void]
9196
# @api private
9297
def define_options
93-
option_parser.banner = "Usage: #{command_template}"
94-
option_parser.separator ''
95-
option_parser.separator DESCRIPTION
96-
option_parser.separator ''
97-
option_parser.separator 'Options:'
98-
98+
option_parser.banner = BANNER
9999
%i[
100-
define_help_option
100+
define_help_option define_version_option
101101
].each { |m| send(m) }
102102
end
103103

@@ -111,6 +111,16 @@ class Parser
111111
end
112112
end
113113

114+
# Define the version option
115+
# @return [void]
116+
# @api private
117+
def define_version_option
118+
option_parser.on_tail('-v', '--version', 'Output the version of this script') do
119+
puts CreateGithubRelease::VERSION
120+
exit 0
121+
end
122+
end
123+
114124
# Parse non-option arguments
115125
# @return [void]
116126
# @api private

lib/create_github_release/command_line/parser.rb

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,27 @@ def report_errors(*errors)
118118
exit 1
119119
end
120120

121-
# The command line template as a string
122-
# @return [String]
123-
# @api private
124-
def command_template
125-
<<~COMMAND
126-
#{File.basename($PROGRAM_NAME)} --help | RELEASE_TYPE [options]
127-
COMMAND
128-
end
121+
# The banner for the option parser
122+
BANNER = <<~BANNER.freeze
123+
Usage:
124+
#{File.basename($PROGRAM_NAME)} --help | RELEASE_TYPE [options]
125+
126+
Version #{CreateGithubRelease::VERSION}
127+
128+
RELEASE_TYPE must be 'major', 'minor', 'patch', 'pre', 'release', or 'first'
129+
130+
Options:
131+
BANNER
129132

130133
# Define the options for OptionParser
131134
# @return [void]
132135
# @api private
133136
def define_options
134137
# @sg-ignore
135-
option_parser.banner = "Usage:\n#{command_template}"
136-
option_parser.separator ''
137-
option_parser.separator "RELEASE_TYPE must be 'major', 'minor', 'patch', 'pre', 'release', or 'first'"
138-
option_parser.separator ''
139-
option_parser.separator 'Options:'
138+
option_parser.banner = BANNER
140139
%i[
141140
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
141+
define_pre_type_option define_remote_option define_last_release_version_option define_version_option
143142
define_next_release_version_option define_changelog_path_option define_quiet_option define_verbose_option
144143
].each { |m| send(m) }
145144
end
@@ -176,7 +175,7 @@ def define_quiet_option
176175
# @return [void]
177176
# @api private
178177
def define_verbose_option
179-
option_parser.on('-v', '--[no-]verbose', 'Show extra output') do |verbose|
178+
option_parser.on('-V', '--[no-]verbose', 'Show extra output') do |verbose|
180179
options.verbose = verbose
181180
end
182181
end
@@ -247,6 +246,16 @@ def define_changelog_path_option
247246
options.changelog_path = name
248247
end
249248
end
249+
250+
# Define the version option
251+
# @return [void]
252+
# @api private
253+
def define_version_option
254+
option_parser.on_tail('-v', '--version', 'Output the version of this script') do
255+
puts CreateGithubRelease::VERSION
256+
exit 0
257+
end
258+
end
250259
end
251260
# rubocop:enable Metrics/ClassLength
252261
end

spec/create_github_release/command_line/parser_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@
145145
it { is_expected.to have_attributes(release_type: 'patch', quiet: true) }
146146
end
147147

148+
context 'when the --version option is given' do
149+
let(:args) { ['--version'] }
150+
it 'should display the version and then exit' do
151+
stub_const('CreateGithubRelease::VERSION', '9.9.9')
152+
expect { subject }.to raise_error(SystemExit).and output(/^9.9.9\n$/).to_stdout
153+
end
154+
end
155+
148156
context 'when the --help options is given' do
149157
let(:args) { ['--help'] }
150158
it 'should exit and display the command usage' do

0 commit comments

Comments
 (0)