NAME

Venus::Cli - Cli Class

ABSTRACT

Cli Class for Perl 5

SYNOPSIS

package main;

use Venus::Cli;

@ARGV = ('example', '--help');

my $cli = Venus::Cli->new;

# $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.

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;

@ARGV = ();

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;

@ARGV = ('--help');

my $cli = Venus::Cli->new;

# 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"

init

init() (ArrayRef)

The init method returns the raw arguments provided, defaulting to @ARGV, used by "args" and "opts".

Since 1.68

init example 1
# given: synopsis

package main;

my $init = $cli->init;

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

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']

podfile

podfile() (Str)

The podfile method returns the full path to the file where the CLI POD is.

Since 1.68

podfile example 1
# given: synopsis

package main;

my $podfile = $cli->podfile;

# "lib/Venus/Cli.pm"

program

program() (Str)

The program method returns the full path to the CLI executable.

Since 1.68

program example 1
# given: synopsis

package main;

my $program = $cli->program;

# "lib/Venus/Cli.pm"