The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Data::Object::Cli

ABSTRACT

Data-Object Cli Class

SYNOPSIS

  package Cli;

  use Data::Object::Class;

  extends 'Data::Object::Cli';

  method main(:$args) {
    # do something with $args, $opts, $env
  }

  run Cli;

DESCRIPTION

Data::Object::Cli provides an abstract base class for defining command-line interface classes, which can be run as scripts or passed as objects in a more complex system.

METHODS

This package implements the following methods.

args

  args() : ArrayRef

The args method returns the ordered arguments passed to the constructor or cli.

args example
  # given $cli

  $cli->args;

env

  env() : HashRef

The env method returns the environment variables in the running process.

env example
  # given $cli

  $cli->env;

main

  main(HashRef :$env, ArrayRef :$args, HashRef :$opts) : Any

The main method is (by convention) the starting point for an automatically executed subclass, i.e. this method is run by default if the subclass is run as a script. This method should be overriden by the subclass. This method is called with the named arguments env, args and opts.

main example
  # given $cli

  $cli->main(%args);

opts

  opts() : HashRef

The opts method returns the parsed options passed to the constructor or cli, based on the specifications defined in the specs method.

opts example
  # given $cli

  $cli->opts;

parse

  parse(ArrayRef $arg1, ArrayRef $arg2, ArrayRef $arg3) : HashRef

The parse method parses command-line options using Getopt::Long and does not mutate @ARGV. The first argument should be an arrayref containing the data to be parsed; E.g. [@ARGV]. The second argument should be an arrayref of Getopt::Long option specifications. The third argument (optionally) should be additional options to be passed along to Getopt::Long::Configure.

parse example
  # given $cli

  $cli->parse($data, $specs, $meta);

run

  run() : Any

The run method automatically executes the subclass unless it's being imported by another package.

run example
  # given $cli

  $cli->run;

specs

  specs() : (Str)

The specs method (if present) returns a list of Getopt::Long option specifications. This method should be overriden by the subclass.

specs example
  # given $cli

  $cli->specs;