Puppet Function: wmflib::argparse

Defined in:
modules/wmflib/functions/argparse.pp
Function type:
Puppet Language

Summary

take a hash of key value parse and return an argument string

Overview

wmflib::argparse(Hash[String[2], Variant[Boolean, String, Numeric, Array[Variant[String, Numeric]]]] $args, String $prefix = '', String[1,1] $separator = ' ')String

Examples:

wmflib::argparse({hostname => 'foo.example.org', port => 8080, ssl => true}) =>
 '--hostname foo.example.org --port 8080 --ssl'

Parameters:

  • args (Hash[String[2], Variant[Boolean, String, Numeric, Array[Variant[String, Numeric]]]])

    the arguments to parse

  • prefix (String) (defaults to: '')

    the prefix to put at the start of the command e.g. /usr/bin/binary

  • separator (String[1,1]) (defaults to: ' ')

    use to separator argument switches from the value

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'modules/wmflib/functions/argparse.pp', line 9

function wmflib::argparse (
    Hash[String[2], Variant[Boolean, String, Numeric, Array[Variant[String, Numeric]]]] $args,
    String                                                                              $prefix    = '',
    String[1,1]                                                                         $separator = ' ',
) >> String {
    $args.reduce($prefix) |$memo, $value| {
        $args_str = $value[1] ? {
            Boolean => $value[1].bool2str(" --${value[0]}", ''),
            Array   => " --${value[0]}${separator}${value[1].join(',').shell_escape}",
            # handle spaces, double quotes, etc.
            default => " --${value[0]}${separator}${value[1].shell_escape}",
        }
        "${memo}${args_str}".strip
    }
}