Mojo::Console - Extend Mojolicious::Command to be able to ask for things from command line
package MyApp::Command::helloworld; use Mojo::Base 'Mojo::Console'; sub run { my $self = shift; my $name = $self->ask('What is your name?'); my $gender = $self->choice('Are you a male or a female?', ['male', 'female']); my $bool = $self->confirm("Do you have a cat?"); $self->line("Hi $name\n"); $self->line("We found out that you are a $gender "); if ($bool) { $self->line("and you have a cat"); } else { $self->line("and you don't have a cat"); } if ($self->confirm("Would you like an icecream?")) { $self->success("Thanks"); } else { $self->error("Oh no!"); } $self->info("You got here because you took an icecream"); } 1;
Mojo::Console is an extension of Mojolicious::Command
Mojo::Console inherits all attributes from Mojolicious::Commandand implements the following new ones.
my $args = $console->args;
Uses Getopt::Long::GetOptions to parse passed args.
my $defaults = $console->defaults; $console = $console->defaults({ arg1 => 'value', verbose => 1 });
Default values for args.
my $input = $console->input; $console = $console->input(Mojo::Console::Input->new);
Input collector interface.
my $max_attempts = $console->max_attempts; $console = $console->max_attempts(5);
How many times to allow a user to enter wrong value.
my $options = $console->options; $console = $console->options(['arg1=s', 'confirm']);
Args descriptor, see GetOpt::Long, method GetOptions.
my $output = $console->output; $console = $console->output(Mojo::Console::Output->new);
Output interface.
Mojo::Console inherits all methods from Mojolicious::Command and implements the following new ones.
my $value = $self->arg('verbose');
Get the value of an argument.
my $answer = $self->ask('What is your name?'); my $required_answer = $self->required->ask('What is your name?'); # this will ask for an answer maximum 10 times and will exit in case the answer is empty
Ask a question.
my $bool = $self->confirm("Are you sure?"); my $bool_with_default_answer = $self->confirm("Are you sure?", 'yes');
Ask the user to confirm something.
my $choice = $self->choice('Are you a male or a female?', ['male', 'female']); my $choice_with_default_answer = $self->choice('Are you a male or a female?', ['male', 'female'], 'male');
Ask the user to pick a value.
$self->error("The program will stop here");
Write a message on the output and exit.
$self->info("This is just an info message");
Write an info message to the output.
$self->line("This message will not have a new line at the end");
Write a line to the output.
$self->newline("This message will have a new line at the end");
Write a line to the output, followed by a newline.
$self->required->ask('What is your name?');
Mark the question as being required.
$self->success("This is just a success message");
Write a success message to the output.
$self->success("This is just a warning message");
Write a warn message to the output.
Mojolicious::Command, Mojolicious, Mojolicious::Guides, https://mojolicious.org.
To install Mojo::Console, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::Console
CPAN shell
perl -MCPAN -e shell install Mojo::Console
For more information on module installation, please visit the detailed CPAN module installation guide.