Puppet Function: basename

Defined in:
vendor_modules/stdlib/lib/puppet/parser/functions/basename.rb
Function type:
Ruby 3.x API

Summary

Strips directory (and optional suffix) from a filename

Overview

basename()String

Returns:

  • (String)

    The stripped filename



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/basename.rb', line 7

newfunction(:basename, type: :rvalue, doc: <<-DOC
  @summary
    Strips directory (and optional suffix) from a filename

  @return [String] The stripped filename
  DOC
) do |arguments|
  raise(Puppet::ParseError, 'basename(): No arguments given') if arguments.empty?
  raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") if arguments.size > 2
  raise(Puppet::ParseError, 'basename(): Requires string as first argument') unless arguments[0].is_a?(String)

  rv = File.basename(arguments[0]) if arguments.size == 1
  if arguments.size == 2
    raise(Puppet::ParseError, 'basename(): Requires string as second argument') unless arguments[1].is_a?(String)
    rv = File.basename(arguments[0], arguments[1])
  end

  return rv
end