pericmd 018: More on default values

Specifying default values is straightforward enough: you just need to set the default property. Unlike in Moose/Moo, you just specify the value and not a coderef to produce the value.

$SPEC{hello} = {
    v => 1.1,
    summary => 'Say hello',
    args => {
        name => {
            summary => 'Name',
            schema  => 'str*',
            cmdline_aliases => {n=>{}},
            req => 1,
            pos => 0,
            default => 'Nobody',
        },
        salutation => {
            summary => 'Salutation',
            schema  => 'str*',
            cmdline_aliases => {s=>{}},
            default => 'Mr',
        },
    },
};

Now when the arguments are not set, Perinci::CmdLine will be giving them default values. But since name is required, there is no way for us to use the value because Perinci::CmdLine will complain about missing required argument:

% ./hello
ERROR 400: Missing required argument(s): name

So we should probably remove the req => 1 property first. After that:

% ./hello
Hello, Mr Nobody!

% ./hello -s Mrs
Hello, Mrs Nobody!

% ./hello -n Somebody
Hello, Mr Somebody!

The help message will show the default values as well:

% ./hello -?
hello - Say hello

Usage:
  hello --help (or -h, -?)
  hello --version (or -v)
  hello [options] [name]
Options:
  --config-path=s     Set path to configuration file
  --config-profile=s  Set configuration profile to use
  --format=s          Choose output format, e.g. json, text
  --help, -h, -?      Display this help message
  --json              Set output format to json
  --naked-res         When outputing as JSON, strip result envelope
  --name=s, -n        Name (=arg[0]) ["Nobody"]
  --no-config         Do not use any configuration file
  --salutation=s, -s  Salutation ["Mr"]
  --version, -v       

If you need to set default using some kind of logic (instead of some static value), currently you’ll need to do it in the function body. (Though I might add a Rinci property default_code or perhaps default.code in the future, akin to default or builder clause in Moose/Moo.):

$SPEC{hello} = {
    v => 1.1,
    summary => 'Say hello',
    args => {
        name => {
            summary => 'Name',
            schema  => 'str*',
            cmdline_aliases => {n=>{}},
            pos => 0,
        },
        salutation => {
            summary => 'Salutation',
            schema  => 'str*',
            cmdline_aliases => {s=>{}},
        },
    },
};
sub hello {
    my %args = @_;

    my $name = $args{name} // do {
        # pick an Indonesian default name if locale is Indonesia
        $ENV{LANG} =~ /id_ID/ ? 'Tak Dikenal' : 'Nobody';
    };
    my $salutation = $args{salutation} // do {
        # pick an Indonesian default salutation if locale is Indonesia
        $ENV{LANG} =~ /id_ID/ ? 'Bung' : 'Mr';
    };

    my $msg = "Hello";
    $msg .= "," if $name || $salutation;
    $msg .= " $salutation" if $salutation;
    $msg .= " $name" if $name;
    $msg .= "!";

    [200, "OK", $msg];
}
% LANG=C ./hello
Hello, Mr Nobody!

% LANG=id_ID.UTF-8 ./hello
Hello, Bung Tak Dikenal!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s