pericmd 027: Configuration file support (2)

This post is still on config files: I want to mention a couple of features that might be useful.

Specifying arrays

In a traditional INI file, array of strings are written as multiple lines of parameters with the same name, so:

array=value1
array=value2

which will result in array value ["value1", "value2"]. There is a problem with this approach though: you can’t specify an array with zero elements. (Actually, specifying array with one element is also problematic in general because you can’t tell whether you want to specify a string/scalar or a one-element array, but this is not a problem in Perinci::CmdLine because argument schema helps pointing out which.)

So the IOD format allows specifying parameter value as JSON:

array=!json ["value1","value2"]
array2=!j []

Or, if you specify an array, you can skip the “!json” or “!j” part and use “[…]” notation directly. IOD recognizes “[” as a marker of JSON arrays:

array=["value1","value2"]
array2=[]

Specifying hashes

In a traditional INI format you can’t specify a hash parameter value. Usually, when an INI file is read into a data structure by a reader module, a section is represented by a hash of parameters and their values.

IOD allows specifying values of JSON hashes (objects), or any valid JSON values for that matter. Like in the case of array, you can omit the “!json” or “!j” part because “{” is regarded as the marker for a hash.

hash=!json {"father":50, "mother":45}
also hash={"father":50, "mother":45}

Let’s see this in action using fatten, a CLI program that uses Perinci::CmdLine. I have the following ~/.config/fatten.conf:

[profile=parse-id-phone]
trace_method=require
overwrite=1
include=Parse::PhoneNumber::ID
include=Perinci::CmdLine::Lite

fatten has a feature that, instead of using subcommands as section names, it looks for script name in the section, for convenience.

parse-id-phone, in turn, is another Perinci::CmdLine-based script:

use Perinci::CmdLine::Any -prefer_lite=>1;

Perinci::CmdLine::Any->new(
    url => "/Parse/PhoneNumber/ID/parse_id_phone",
)->run;

Since fatten detects modules used through trapping the require() statement, modules like Perinci::CmdLine::Lite (the backend used by Perinci::CmdLine::Any) and Parse::PhoneNumber::ID (the backend module for the script itself) fail to be detected and we need to tell fatten via the –include option (or include parameter in the config file). Thus, when we run fatten to fatpack the parse-id-phone script:

% fatten --input-file `which parse-id-phone` --output-file /tmp/parse-id-phone --debug
fatten: Created tempdir /tmp/KYa79g6Qan
fatten: Will be targetting perl bless( {original => "v5.18.4",qv => 1,version => [5,18,4]}, 'version' )
fatten: Tracing dependencies ...
fatten:   Tracing with method 'require' ...
ERROR 400: Missing required argument(s): text
fatten: Building lib/ ...
fatten:   Adding module: Perinci::CmdLine::Any (traced)
fatten:   Adding module: Perinci::CmdLine::Lite (traced)
fatten:   Adding module: Log::Any (traced)
fatten:   Adding module: Log::Any::Manager (traced)
fatten:   Adding module: Log::Any::Adapter::Util (traced)
fatten:   Adding module: Log::Any::Adapter::Null (traced)
fatten:   Adding module: Log::Any::Adapter::Base (traced)
fatten:   Adding module: Log::Any::Proxy (traced)
fatten:   Adding module: Mo (traced)
fatten:   Adding module: Mo::build (traced)
fatten:   Adding module: Mo::default (traced)
fatten:   Adding module: experimental (traced)
fatten:   Adding module: Perinci::CmdLine::Base (traced)
fatten:   Adding module: Perinci::Access::Lite (traced)
fatten:   Adding module: Perinci::AccessUtil (traced)
fatten:   Adding module: Perinci::CmdLine::Util::Config (traced)
fatten:   Adding module: Parse::PhoneNumber::ID (traced)
fatten:   Adding module: Function::Fallback::CoreOrPP (traced)
fatten:   Adding module: Perinci::Sub::Util (traced)
fatten:   Adding module: Perinci::Sub::Normalize (traced)
fatten:   Adding module: Sah::Schema::Rinci (traced)
fatten:   Adding module: Data::Sah::Normalize (traced)
fatten:   Adding module: Perinci::Object (traced)
fatten:   Adding module: Perinci::Object::Function (traced)
fatten:   Adding module: Perinci::Object::Metadata (traced)
fatten:   Adding module: String::Trim::More (traced)
fatten:   Adding module: Config::IOD::Reader (traced)
fatten:   Adding module: Perinci::Sub::GetArgs::Argv (traced)
fatten:   Adding module: Getopt::Long::Util (traced)
fatten:   Adding module: Perinci::Sub::GetArgs::Array (traced)
fatten:   Adding module: Data::Sah::Util::Type (traced)
fatten:   Adding module: Parse::PhoneNumber::ID (included)
fatten:   Adding module: Perinci::CmdLine::Lite (included)
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/CmdLine/Any.pm --> /tmp/KYa79g6Qan/lib/Perinci/CmdLine/Any.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/CmdLine/Lite.pm --> /tmp/KYa79g6Qan/lib/Perinci/CmdLine/Lite.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Log/Any.pm --> /tmp/KYa79g6Qan/lib/Log/Any.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Log/Any/Manager.pm --> /tmp/KYa79g6Qan/lib/Log/Any/Manager.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Log/Any/Adapter/Util.pm --> /tmp/KYa79g6Qan/lib/Log/Any/Adapter/Util.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Log/Any/Adapter/Null.pm --> /tmp/KYa79g6Qan/lib/Log/Any/Adapter/Null.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Log/Any/Adapter/Base.pm --> /tmp/KYa79g6Qan/lib/Log/Any/Adapter/Base.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Log/Any/Proxy.pm --> /tmp/KYa79g6Qan/lib/Log/Any/Proxy.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Mo.pm --> /tmp/KYa79g6Qan/lib/Mo.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Mo/build.pm --> /tmp/KYa79g6Qan/lib/Mo/build.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Mo/default.pm --> /tmp/KYa79g6Qan/lib/Mo/default.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/experimental.pm --> /tmp/KYa79g6Qan/lib/experimental.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/CmdLine/Base.pm --> /tmp/KYa79g6Qan/lib/Perinci/CmdLine/Base.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/Access/Lite.pm --> /tmp/KYa79g6Qan/lib/Perinci/Access/Lite.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/AccessUtil.pm --> /tmp/KYa79g6Qan/lib/Perinci/AccessUtil.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/CmdLine/Util/Config.pm --> /tmp/KYa79g6Qan/lib/Perinci/CmdLine/Util/Config.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Parse/PhoneNumber/ID.pm --> /tmp/KYa79g6Qan/lib/Parse/PhoneNumber/ID.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Function/Fallback/CoreOrPP.pm --> /tmp/KYa79g6Qan/lib/Function/Fallback/CoreOrPP.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/Sub/Util.pm --> /tmp/KYa79g6Qan/lib/Perinci/Sub/Util.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/Sub/Normalize.pm --> /tmp/KYa79g6Qan/lib/Perinci/Sub/Normalize.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Sah/Schema/Rinci.pm --> /tmp/KYa79g6Qan/lib/Sah/Schema/Rinci.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Data/Sah/Normalize.pm --> /tmp/KYa79g6Qan/lib/Data/Sah/Normalize.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/Object.pm --> /tmp/KYa79g6Qan/lib/Perinci/Object.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/Object/Function.pm --> /tmp/KYa79g6Qan/lib/Perinci/Object/Function.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/Object/Metadata.pm --> /tmp/KYa79g6Qan/lib/Perinci/Object/Metadata.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/String/Trim/More.pm --> /tmp/KYa79g6Qan/lib/String/Trim/More.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Config/IOD/Reader.pm --> /tmp/KYa79g6Qan/lib/Config/IOD/Reader.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/Sub/GetArgs/Argv.pm --> /tmp/KYa79g6Qan/lib/Perinci/Sub/GetArgs/Argv.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Getopt/Long/Util.pm --> /tmp/KYa79g6Qan/lib/Getopt/Long/Util.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Perinci/Sub/GetArgs/Array.pm --> /tmp/KYa79g6Qan/lib/Perinci/Sub/GetArgs/Array.pm ...
fatten:   Copying /home/s1/perl5/perlbrew/perls/perl-5.18.4/lib/site_perl/5.18.4/Data/Sah/Util/Type.pm --> /tmp/KYa79g6Qan/lib/Data/Sah/Util/Type.pm ...
fatten:   Added 31 files (340.6 KB)
fatten: Packing ...
fatten:   Produced /tmp/parse-id-phone (372.7 KB)
fatten: Deleting tempdir /tmp/KYa79g6Qan ...
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