The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojo::Console - Extend Mojolicious::Command to be able to ask for things from command line

SYNOPSIS

    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;

DESCRIPTION

Mojo::Console is an extension of Mojolicious::Command

ATTRIBUTES

Mojo::Console inherits all attributes from Mojolicious::Commandand implements the following new ones.

args

    my $args    = $console->args;

Uses Getopt::Long::GetOptions to parse passed args.

defaults

    my $defaults = $console->defaults;
    $console = $console->defaults({ arg1 => 'value', verbose => 1 });

Default values for args.

input

    my $input = $console->input;
    $console = $console->input(Mojo::Console::Input->new);

Input collector interface.

max_attempts

    my $max_attempts = $console->max_attempts;
    $console = $console->max_attempts(5);

How many times to allow a user to enter wrong value.

options

    my $options = $console->options;
    $console = $console->options(['arg1=s', 'confirm']);

Args descriptor, see GetOpt::Long, method GetOptions.

output

    my $output = $console->output;
    $console = $console->output(Mojo::Console::Output->new);

Output interface.

METHODS

Mojo::Console inherits all methods from Mojolicious::Command and implements the following new ones.

arg

    my $value = $self->arg('verbose');

Get the value of an argument.

ask

    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.

confirm

    my $bool = $self->confirm("Are you sure?");
    my $bool_with_default_answer = $self->confirm("Are you sure?", 'yes');

Ask the user to confirm something.

choice

    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.

error

    $self->error("The program will stop here");

Write a message on the output and exit.

info

    $self->info("This is just an info message");

Write an info message to the output.

line

    $self->line("This message will not have a new line at the end");

Write a line to the output.

newline

    $self->newline("This message will have a new line at the end");

Write a line to the output, followed by a newline.

required

    $self->required->ask('What is your name?');

Mark the question as being required.

success

    $self->success("This is just a success message");

Write a success message to the output.

warn

    $self->success("This is just a warning message");

Write a warn message to the output.

SEE ALSO

Mojolicious::Command, Mojolicious, Mojolicious::Guides, https://mojolicious.org.