Skip to content

Commit 740ffa7

Browse files
committed
Fix OptionParser#program_name not to strip suffix unexpectedly
1 parent 3869000 commit 740ffa7

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/optparse.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,15 @@ def banner
12941294
# to $0.
12951295
#
12961296
def program_name
1297-
@program_name || File.basename($0, '.*')
1297+
@program_name || strip_ext(File.basename($0))
1298+
end
1299+
1300+
private def strip_ext(name) # :nodoc:
1301+
exts = /#{
1302+
require "rbconfig"
1303+
Regexp.union(RbConfig::CONFIG["EXECUTABLE_EXTS"])
1304+
}\z/o
1305+
name.sub(exts, "")
12981306
end
12991307

13001308
# for experimental cascading :-)

test/optparse/test_optparse.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,16 @@ def stdout.tty?; true; end
216216
end
217217
end
218218
end
219+
220+
def test_program_name
221+
program = $0
222+
$0 = "rdbg3.5"
223+
assert_equal "rdbg3.5", OptionParser.new.program_name
224+
RbConfig::CONFIG["EXECUTABLE_EXTS"].split(" ") do |ext|
225+
$0 = "rdbg3.5" + ext
226+
assert_equal "rdbg3.5", OptionParser.new.program_name
227+
end
228+
ensure
229+
$0 = program
230+
end
219231
end

0 commit comments

Comments
 (0)