pericmd 017: Using command-line options and arguments

Why don’t we try out the program we’ve made in the previous post (pericmd 015):

% ./hello --help
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])
  --no-config         Do not use any configuration file
  --salutation=s, -s  Salutation
  --version, -v       

After we’ve added our two arguments, the help message reflects it (lines 15, 17 and also line 7). Each argument is represented with a command-line option. As you might expect, the properties summary will be used in the help message. Note that the short options are also listed (-n and -s). We can say any of below:

% ./hello --name Ahmad
Hello, Ahmad!

% ./hello -n Ahmad
Hello, Ahmad!

% ./hello -n Ahmad -s Mr
Hello, Mr Ahmad!

Since name is specified as having the position of 0 (via pos => 0 in the metadata), Perinci::CmdLine also accepts it as arguments. Notice how the help message in lines 7 and 15 also hints about this.

% ./hello Ahmad
Hello, Ahmad!

% ./hello "Ahmad Dahlan"
Hello, Ahmad Dahlan!

However, to avoid conflict, an error will be raised if both argument and command-line option are specified:

% ./hello -n Ahmad Achmad
ERROR 400: You specified option --name but also argument #0

If we want to make some arguments as required, we can use the property req:

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

Now when we do not specify name, the program will exit with an error:

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

The help message will also reflects this (see how the bracket surrounding the name in the usage line 7 turns into an angle bracket, also notice how an asterisk * is added in line 15 to signify required arguments):

% ./hello -h
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])
  --no-config         Do not use any configuration file
  --salutation=s, -s  Salutation
  --version, -v       
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