pericmd 028: Environment support

Aside from config file, environment can also be a convenient way to input things. It’s more “secure” than command-line options because a casual “ps ax” command won’t show the values of environment variables unlike the command-line. By default, only the user running the program and the superuser can see the environment values of a process (exposed via /proc/PID/environ which has 0700 mode, while /proc/PID/cmdline has 0444 mode).

Perinci::CmdLine gives you environment support. By default, it reads PROGNAME_OPT for you (the value will be prepended to the command-line option). It has a higher precedence than config files (i.e. it can override values from config files) but can be overriden by command-line options.

Let’s see an example with pause. If you run without any configuration file:

% pause ls
ERROR 400: Missing required argument(s): password, username

If we set PAUSE_OPT environment to:

% export PAUSE_OPT="--username PERLANCAR --password xxx"

then run the program again:

% pause ls
...

then username and password arguments have been set from the environment.

Turning off environment (and configuration)

If you don’t want your CLI program to read from environment or configuration, you can turn these features off via the read_env and read_config attributes, respectively:

Perinci::CmdLine::Any->new(
    ...
    read_env => 0,
    read_config => 0,
)->run;
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