From 740ffa76c05b9627d8be481e2a7b5869c7b0c46a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 29 Jun 2025 16:07:07 +0900 Subject: [PATCH 1/2] Fix OptionParser#program_name not to strip suffix unexpectedly --- lib/optparse.rb | 10 +++++++++- test/optparse/test_optparse.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/optparse.rb b/lib/optparse.rb index 9cbd377..7838af7 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1294,7 +1294,15 @@ def banner # to $0. # def program_name - @program_name || File.basename($0, '.*') + @program_name || strip_ext(File.basename($0)) + end + + private def strip_ext(name) # :nodoc: + exts = /#{ + require "rbconfig" + Regexp.union(RbConfig::CONFIG["EXECUTABLE_EXTS"]) + }\z/o + name.sub(exts, "") end # for experimental cascading :-) diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb index d50203b..ae12ae5 100644 --- a/test/optparse/test_optparse.rb +++ b/test/optparse/test_optparse.rb @@ -216,4 +216,16 @@ def stdout.tty?; true; end end end end + + def test_program_name + program = $0 + $0 = "rdbg3.5" + assert_equal "rdbg3.5", OptionParser.new.program_name + RbConfig::CONFIG["EXECUTABLE_EXTS"].split(" ") do |ext| + $0 = "rdbg3.5" + ext + assert_equal "rdbg3.5", OptionParser.new.program_name + end + ensure + $0 = program + end end From 15b2f00b6bed354bf66a98a4759626ed41aedd4b Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 29 Jun 2025 16:50:09 +0900 Subject: [PATCH 2/2] JRuby does not have EXECUTABLE_EXTS in RbConfg::CONFIG --- lib/optparse.rb | 2 +- test/optparse/test_optparse.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/optparse.rb b/lib/optparse.rb index 7838af7..332aea2 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1300,7 +1300,7 @@ def program_name private def strip_ext(name) # :nodoc: exts = /#{ require "rbconfig" - Regexp.union(RbConfig::CONFIG["EXECUTABLE_EXTS"]) + Regexp.union(*RbConfig::CONFIG["EXECUTABLE_EXTS"]&.split(" ")) }\z/o name.sub(exts, "") end diff --git a/test/optparse/test_optparse.rb b/test/optparse/test_optparse.rb index ae12ae5..ff33400 100644 --- a/test/optparse/test_optparse.rb +++ b/test/optparse/test_optparse.rb @@ -221,7 +221,7 @@ def test_program_name program = $0 $0 = "rdbg3.5" assert_equal "rdbg3.5", OptionParser.new.program_name - RbConfig::CONFIG["EXECUTABLE_EXTS"].split(" ") do |ext| + RbConfig::CONFIG["EXECUTABLE_EXTS"]&.split(" ") do |ext| $0 = "rdbg3.5" + ext assert_equal "rdbg3.5", OptionParser.new.program_name end