Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]&.split(" "))
}\z/o
name.sub(exts, "")
end

# for experimental cascading :-)
Expand Down
12 changes: 12 additions & 0 deletions test/optparse/test_optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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