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

Venus::Cli - Cli Class

ABSTRACT

Cli Class for Perl 5

SYNOPSIS

  package main;

  use Venus::Cli;

  my $cli = Venus::Cli->new(['example', '--help']);

  # $cli->program;

  # "/path/to/executable"

  # $cli->arg(0);

  # "example"

  # $cli->opt('help');

  # 1

DESCRIPTION

This package provides a superclass and methods for providing simple yet robust command-line interfaces.

ATTRIBUTES

This package has the following attributes:

args

  args(Args $data) (Args)

The args attribute holds a Venus::Args object.

Since 1.71

args example 1
  # given: synopsis

  package main;

  my $args = $cli->args;

data

  data(Data $data) (Data)

The data attribute holds a Venus::Data object.

Since 1.71

data example 1
  # given: synopsis

  package main;

  my $data = $cli->data;

init

  init(ArrayRef $data) (ArrayRef)

The init attribute holds the "initial" raw arguments provided to the CLI, defaulting to [@ARGV], used by "args" and "opts".

Since 1.68

init example 1
  # given: synopsis

  package main;

  my $init = $cli->init;

  # ["example", "--help"]

logs

  logs(Logs $logs) (Logs)

The logs attribute holds a Venus::Logs object.

Since 1.71

logs example 1
  # given: synopsis

  package main;

  my $logs = $cli->logs;

opts

  opts(Opts $opts) (Opts)

The opts attribute holds a Venus::Opts object.

Since 1.71

opts example 1
  # given: synopsis

  package main;

  my $opts = $cli->opts;

path

  path(Path $data) (Path)

The path attribute holds a Venus::Path object, meant to represent the path of the file where the CLI executable and POD is.

Since 1.71

path example 1
  # given: synopsis

  package main;

  my $path = $cli->path;

vars

  vars(Vars $vars) (Vars)

The vars attribute holds a Venus::Vars object.

Since 1.71

vars example 1
  # given: synopsis

  package main;

  my $vars = $cli->vars;

INHERITS

This package inherits behaviors from:

Venus::Kind::Utility

INTEGRATES

This package integrates behaviors from:

Venus::Role::Optional

METHODS

This package provides the following methods:

arg

  arg(Str $pos) (Str)

The arg method returns the element specified by the index in the unnamed arguments list, i.e. arguments not parsed as options.

Since 1.68

arg example 1
  # given: synopsis

  package main;

  my $arg = $cli->arg;

  # undef
arg example 2
  # given: synopsis

  package main;

  my $arg = $cli->arg(0);

  # "example"

execute

  execute() (Any)

The execute method is the default entrypoint of the program and runs the application.

Since 1.68

execute example 1
  package main;

  use Venus::Cli;

  my $cli = Venus::Cli->new([]);

  # e.g.

  # sub execute {
  #   my ($self) = @_;
  #
  #   return $self->opt('help') ? $self->okay : $self->fail;
  # }

  # my $result = $cli->execute;

  # ...
execute example 2
  package main;

  use Venus::Cli;

  my $cli = Venus::Cli->new(['--help']);

  # e.g.

  # sub execute {
  #   my ($self) = @_;
  #
  #   return $self->opt('help') ? $self->okay : $self->fail;
  # }

  # my $result = $cli->execute;

  # ...

exit

  exit(Int $code, Str|CodeRef $code, Any @args) (Any)

The exit method exits the program using the exit code provided. The exit code defaults to 0. Optionally, you can dispatch before exiting by providing a method name or coderef, and arguments.

Since 1.68

exit example 1
  # given: synopsis

  package main;

  my $exit = $cli->exit;

  # ()
exit example 2
  # given: synopsis

  package main;

  my $exit = $cli->exit(0);

  # ()
exit example 3
  # given: synopsis

  package main;

  my $exit = $cli->exit(1);

  # ()
exit example 4
  # given: synopsis

  package main;

  # my $exit = $cli->exit(1, 'log_info', 'Something failed!');

  # ()

fail

  fail(Str|CodeRef $code, Any @args) (Any)

The fail method exits the program with the exit code 1. Optionally, you can dispatch before exiting by providing a method name or coderef, and arguments.

Since 1.68

fail example 1
  # given: synopsis

  package main;

  my $fail = $cli->fail;

  # ()
fail example 2
  # given: synopsis

  package main;

  # my $fail = $cli->fail('log_info', 'Something failed!');

  # ()

help

  help(Str @data) (Str)

The help method returns the POD found in the file specified by the "podfile" method, defaulting to the =head1 OPTIONS section.

Since 1.68

help example 1
  # given: synopsis

  package main;

  my $help = $cli->help;

  # ""
help example 2
  # given: synopsis

  package main;

  # my $help = $cli->help('head1', 'NAME');

  #  "Example"

log_debug

  log_debug(Str @data) (Log)

The log_debug method logs debug information.

Since 1.68

log_debug example 1
  # given: synopsis

  package main;

  # $cli->logs->level('trace');

  # my $log = $cli->log_debug(time, 'Something failed!');

  # "0000000000 Something failed!"

log_error

  log_error(Str @data) (Log)

The log_error method logs error information.

Since 1.68

log_error example 1
  # given: synopsis

  package main;

  # $cli->logs->level('trace');

  # my $log = $cli->log_error(time, 'Something failed!');

  # "0000000000 Something failed!"

log_fatal

  log_fatal(Str @data) (Log)

The log_fatal method logs fatal information.

Since 1.68

log_fatal example 1
  # given: synopsis

  package main;

  # $cli->logs->level('trace');

  # my $log = $cli->log_fatal(time, 'Something failed!');

  # "0000000000 Something failed!"

log_info

  log_info(Str @data) (Log)

The log_info method logs info information.

Since 1.68

log_info example 1
  # given: synopsis

  package main;

  # $cli->logs->level('trace');

  # my $log = $cli->log_info(time, 'Something failed!');

  # "0000000000 Something failed!"

log_trace

  log_trace(Str @data) (Log)

The log_trace method logs trace information.

Since 1.68

log_trace example 1
  # given: synopsis

  package main;

  # $cli->logs->level('trace');

  # my $log = $cli->log_trace(time, 'Something failed!');

  # "0000000000 Something failed!"

log_warn

  log_warn(Str @data) (Log)

The log_warn method logs warn information.

Since 1.68

log_warn example 1
  # given: synopsis

  package main;

  # $cli->logs->level('trace');

  # my $log = $cli->log_warn(time, 'Something failed!');

  # "0000000000 Something failed!"

okay

  okay(Str|CodeRef $code, Any @args) (Any)

The okay method exits the program with the exit code 0. Optionally, you can dispatch before exiting by providing a method name or coderef, and arguments.

Since 1.68

okay example 1
  # given: synopsis

  package main;

  my $okay = $cli->okay;

  # ()
okay example 2
  # given: synopsis

  package main;

  # my $okay = $cli->okay('log_info', 'Something worked!');

  # ()

opt

  opt(Str $name) (Str)

The opt method returns the named option specified by the "options" method.

Since 1.68

opt example 1
  # given: synopsis

  package main;

  my $opt = $cli->opt;

  # undef
opt example 2
  # given: synopsis

  package main;

  my $opt = $cli->opt('help');

  # 1

options

  options() (ArrayRef)

The options method returns the list of Getopt::Long definitions.

Since 1.68

options example 1
  # given: synopsis

  package main;

  my $options = $cli->options;

  # ['help|h']

AUTHORS

Awncorp, awncorp@cpan.org

LICENSE

Copyright (C) 2000, Al Newkirk.

This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.