Venus::Cli - Cli Class
Cli Class for Perl 5
package main; use Venus::Cli; my $cli = Venus::Cli->new(['--help']); $cli->set('opt', 'help', { help => 'Show help information', }); # $cli->opt('help'); # [1] # $cli->data; # {help => 1}
This package provides a superclass and methods for creating simple yet robust command-line interfaces.
This package has the following attributes:
data(ArrayRef $data) (ArrayRef)
The data attribute holds an arrayref of command-line arguments and defaults to @ARGV.
@ARGV
Since 2.55
2.55
# given: synopsis package main; my $data = $cli->data([]); # []
This package inherits behaviors from:
Venus::Kind::Utility
This package integrates behaviors from:
Venus::Role::Stashable
This package provides the following methods:
arg(Str $name) (Any)
The arg method returns the value passed to the CLI that corresponds to the registered argument using the name provided.
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--help']); my $name = $cli->arg('name'); # undef
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--help']); $cli->set('arg', 'name', { range => '0', }); my $name = $cli->arg('name'); # ["example"]
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--help']); $cli->set('arg', 'name', { range => '0', }); my ($name) = $cli->arg('name'); # "example"
package main; use Venus::Cli; my $cli = Venus::Cli->new(['--help']); $cli->set('arg', 'name', { prompt => 'Enter a name', range => '0', }); my ($name) = $cli->arg('name'); # prompts for name, e.g. # > name: Enter a name # > example # "example"
package main; use Venus::Cli; my $cli = Venus::Cli->new(['--help']); $cli->set('arg', 'name', { default => 'example', range => '0', }); my ($name) = $cli->arg('name'); # "example"
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--help']); $cli->set('arg', 'name', { type => 'string', range => '0', }); my ($name) = $cli->arg('name'); # "example"
package main; use Venus::Cli; my $cli = Venus::Cli->new(['--help']); $cli->set('arg', 'name', { type => 'string', range => '0', }); my ($name) = $cli->arg('name'); # Exception! (isa Venus::Cli::Error) (see error_on_arg_validation) # Invalid argument: name: received (undef), expected (string)
cmd(Str $name) (Any)
The cmd method returns truthy or falsy if the value passed to the CLI that corresponds to the argument registered and associated with the registered command using the name provided.
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', 'execute']); my $name = $cli->cmd('name'); # undef
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', 'execute']); $cli->set('arg', 'action', { range => '1', }); $cli->set('cmd', 'execute', { arg => 'action', }); my $is_execute = $cli->cmd('execute'); # 1
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', 'execute']); $cli->set('arg', 'action', { range => '1', }); $cli->set('cmd', 'execute', { arg => 'action', }); my ($is_execute) = $cli->cmd('execute'); # 1
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example']); $cli->set('arg', 'action', { prompt => 'Enter the desired action', range => '1', }); $cli->set('cmd', 'execute', { arg => 'action', }); my ($is_execute) = $cli->cmd('execute'); # prompts for action, e.g. # > name: Enter the desired action # > execute # 1
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example']); $cli->set('arg', 'action', { default => 'execute', range => '1', }); $cli->set('cmd', 'execute', { arg => 'action', }); my ($is_execute) = $cli->cmd('execute'); # 1
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', 'execute']); $cli->set('arg', 'action', { type => 'string', range => '1', }); $cli->set('cmd', 'execute', { arg => 'action', }); my ($is_execute) = $cli->cmd('execute'); # 1
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example']); $cli->set('arg', 'action', { type => 'string', range => '1', }); $cli->set('cmd', 'execute', { arg => 'action', }); my ($is_execute) = $cli->cmd('execute'); # 0
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.
0
# given: synopsis package main; my $exit = $cli->exit; # ()
# given: synopsis package main; my $exit = $cli->exit(0); # ()
# given: synopsis package main; my $exit = $cli->exit(1); # ()
# given: synopsis package main; my $exit = $cli->exit(1, 'stash', 'executed', 1); # ()
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.
1
# given: synopsis package main; my $fail = $cli->fail; # ()
# given: synopsis package main; my $fail = $cli->fail('stash', 'executed', 1); # ()
get(Str $type, Str $name) (Any)
The get method returns arg, opt, cmd, or str configuration values from the configuration database.
arg
opt
cmd
str
package main; use Venus::Cli; my $cli = Venus::Cli->new; my $get = $cli->get; # undef
package main; use Venus::Cli; my $cli = Venus::Cli->new; my $get = $cli->get('opt', 'help'); # undef
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('opt', 'help', { alias => 'h', }); my $get = $cli->get('opt', 'help'); # {name => 'help', alias => 'h'}
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('opt', 'help', { alias => 'h', }); my $get = $cli->get('opt'); # {help => {name => 'help', alias => 'h'}}
help() (Str)
The help method returns a string representing "usage" information based on the configuration of the CLI.
package main; use Venus::Cli; my $cli = Venus::Cli->new; my $help = $cli->help; # "Usage: application"
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('str', 'name', 'program'); my $help = $cli->help; # "Usage: program"
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('str', 'name', 'program'); $cli->set('arg', 'command', { help => 'Command to execute', }); my $help = $cli->help; # "Usage: program [<argument>] # # Arguments: # # command # Command to execute # (optional)"
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('str', 'name', 'program'); $cli->set('arg', 'command', { help => 'Command to execute', required => 1 }); my $help = $cli->help; # "Usage: program <argument> # # Arguments: # # command # Command to execute # (required)"
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('str', 'name', 'program'); $cli->set('arg', 'command', { help => 'Command to execute', type => 'string', required => 1, }); my $help = $cli->help; # "Usage: program <argument> # # Arguments: # # command # Command to execute # (required) # (string)"
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('str', 'name', 'program'); $cli->set('arg', 'command', { help => 'Command to execute', required => 1, }); $cli->set('cmd', 'create', { help => 'Create new resource', arg => 'command', }); my $help = $cli->help; # "Usage: program <argument> # # Arguments: # # command # Command to execute # (required) # # Commands: # # create # Create new resource # (ccommand)"
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('str', 'name', 'program'); $cli->set('arg', 'command', { help => 'Command to execute', required => 1, }); $cli->set('opt', 'help', { help => 'Show help information', alias => ['?', 'h'], }); $cli->set('cmd', 'create', { help => 'Create new resource', arg => 'command', }); my $help = $cli->help; # "Usage: program <argument> [<option>] # # Arguments: # # command # Command to execute # (required) # # Options: # # -?, -h, --help # Show help information # (optional) # # Commands: # # create # Create new resource # (command)"
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('str', 'name', 'program'); $cli->set('arg', 'files', { help => 'File paths', required => 1, range => '0:', }); $cli->set('opt', 'verbose', { help => 'Show details during processing', alias => ['v'], }); my $help = $cli->help; # "Usage: program <argument>, ... [<option>] # # Arguments: # # files, ... # File paths # (required) # # Options: # # -v, --verbose # Show details during processing # (optional)"
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.
# given: synopsis package main; my $okay = $cli->okay; # ()
# given: synopsis package main; my $okay = $cli->okay('stash', 'executed', 1); # ()
opt(Str $name) (Any)
The opt method returns the value passed to the CLI that corresponds to the registered option using the name provided.
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--help']); my $name = $cli->opt('help'); # undef
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--help']); $cli->set('opt', 'help', {}); my $name = $cli->opt('help'); # [1]
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--help']); $cli->set('opt', 'help', {}); my ($name) = $cli->opt('help'); # 1
package main; use Venus::Cli; my $cli = Venus::Cli->new([]); $cli->set('opt', 'name', { prompt => 'Enter a name', type => 'string', multi => 0, }); my ($name) = $cli->opt('name'); # prompts for name, e.g. # > name: Enter a name # > example # "example"
package main; use Venus::Cli; my $cli = Venus::Cli->new(['--name', 'example']); $cli->set('opt', 'name', { prompt => 'Enter a name', type => 'string', multi => 0, }); my ($name) = $cli->opt('name'); # Does not prompt # "example"
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--name', 'example', '--name', 'example']); $cli->set('opt', 'name', { type => 'string', multi => 1, }); my (@name) = $cli->opt('name'); # ("example", "example")
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--name', 'example']); $cli->set('opt', 'name', { type => 'number', multi => 1, }); my ($name) = $cli->opt('name'); # Exception! (isa Venus::Cli::Error) (see error_on_opt_validation) # Invalid option: name: received (undef), expected (number)
parsed() (HashRef)
The parsed method returns the values provided to the CLI for all registered arguments and options as a hashref.
package main; use Venus::Cli; my $cli = Venus::Cli->new(['example', '--help']); $cli->set('arg', 'name', { range => '0', }); $cli->set('opt', 'help', { alias => 'h', }); my $parsed = $cli->parsed; # {name => "example", help => 1}
parser() (Opts)
The parser method returns a Venus::Opts object using the "spec" returned based on the CLI configuration.
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('opt', 'help', { help => 'Show help information', alias => 'h', }); my $parser = $cli->parser; # bless({...}, 'Venus::Opts')
set(Str $type, Str $name, Str|HashRef $data) (Any)
The set method stores configuration values for arg, opt, cmd, or str data in the configuration database, and returns the invocant.
The following are configurable arg properties:
The default property specifies the "default" value to be used if none is provided.
default
The help property specifies the help text to output in usage instructions.
help
The label property specifies the label text to output in usage instructions.
label
The name property specifies the name of the argument.
name
The prompt property specifies the text to be used in a prompt for input if no value is provided.
prompt
The range property specifies the zero-indexed position where the CLI arguments can be found, using range notation.
range
The required property specifies whether the argument is required and throws an exception is missing when fetched.
required
The type property specifies the data type of the argument. Valid types are number parsed as a Getopt::Long integer, string parsed as a Getopt::Long string, float parsed as a Getopt::Long float, boolean parsed as a Getopt::Long flag, or yesno parsed as a Getopt::Long string. Otherwise, the type will default to boolean.
type
number
string
float
boolean
yesno
The following are configurable cmd properties:
The arg property specifies the CLI argument where the command can be found.
The name property specifies the name of the command.
The following are configurable opt properties:
The alias property specifies the alternate identifiers that can be provided.
alias
The multi property denotes whether the CLI will accept multiple occurrences of the option.
multi
The name property specifies the name of the option.
The required property specifies whether the option is required and throws an exception is missing when fetched.
The type property specifies the data type of the option. Valid types are number parsed as a Getopt::Long integer, string parsed as a Getopt::Long string, float parsed as a Getopt::Long float, boolean parsed as a Getopt::Long flag, or yesno parsed as a Getopt::Long string. Otherwise, the type will default to boolean.
package main; use Venus::Cli; my $cli = Venus::Cli->new; my $set = $cli->set; # undef
package main; use Venus::Cli; my $cli = Venus::Cli->new; my $set = $cli->set('opt', 'help'); # bless({...}, 'Venus::Cli')
package main; use Venus::Cli; my $cli = Venus::Cli->new; my $set = $cli->set('opt', 'help', { alias => 'h', }); # bless({...}, 'Venus::Cli')
package main; use Venus::Cli; my $cli = Venus::Cli->new; my $set = $cli->set('opt', 'help', { alias => ['?', 'h'], }); # bless({...}, 'Venus::Cli')
str(Str $name) (Any)
The str method gets or sets configuration strings used in CLI help text based on the arguments provided. The "help" method uses "name", "description", "header", and "footer" strings.
"name"
"description"
"header"
"footer"
package main; use Venus::Cli; my $cli = Venus::Cli->new; $cli->set('str', 'name', 'program'); my $str = $cli->str('name'); # "program"
This package may raise the following errors:
error_on_arg_validation
This package may raise an error_on_arg_validation exception.
example 1
# given: synopsis; my @args = ("...", "example", "string"); my $error = $cli->throw('error_on_arg_validation', @args)->catch('error'); # my $name = $error->name; # "on_arg_validation" # my $message = $error->message; # "Invalid argument: example: ..." # my $name = $error->stash('name'); # "example" # my $type = $error->stash('type'); # "string"
error_on_opt_validation
This package may raise an error_on_opt_validation exception.
# given: synopsis; my @args = ("...", "example", "string"); my $error = $cli->throw('error_on_opt_validation', @args)->catch('error'); # my $name = $error->name; # "on_opt_validation" # my $message = $error->message; # "Invalid option: example: ..." # my $name = $error->stash('name'); # "example" # my $type = $error->stash('type'); # "string"
Awncorp, awncorp@cpan.org
awncorp@cpan.org
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.
To install Venus, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Venus
CPAN shell
perl -MCPAN -e shell install Venus
For more information on module installation, please visit the detailed CPAN module installation guide.