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

Amon2::CLI - The way of CLI for Amon2 App

SYNOPSIS

First of all, in your MyApp class

    package MyApp;
    use strict;
    use warnings;
    use parent qw/Amon2/;

    1;

Then in your MyApp::CLI::Foo class

    package MyApp::CLI::Foo;
    use strict;
    use warnings;

    sub main {
        my ($class, $c) = @_;
        # do something
        print 'done!';
    }

    1;

Finally, in your script

    use Amon2::CLI 'MyApp';

    MyApp->bootstrap->run('Foo'); # done!

Or directly,

    use Amon2::CLI 'MyApp';

    MyApp->bootstrap->run(sub{
        my ($c) = @_;
        # do something
        print 'kaboom!';
    });

DESCRIPTION

Amon2::CLI is ALPHA QUALITY. I may change interfaces without a notice.

This module gives you the easy way of CLI for Amon2 App. It handles to load class, to get options and to throw error.

CONFIGURE PLUGIN

You can write your own MyApp::CLI class for customizing the way instead of Amon2::CLI.

    package MyApp::CLI;
    use strict;
    use warnings;
    use parent qw/Amon2/;
    use MyApp::Logger;

    __PACKAGE__->load_plugins(
        'CLI' => {
            base => 'MyApp::CLI',
            on_error => sub {
                my ($c, $e) = @_;
                MyApp::Logger->write('path/to/log', $e);
            },
        },
    );

And in your script

    use MyApp::CLI;

    MyApp::CLI->bootstrap->run(sub{
        my ($c) = @_;
        # do something
        print 'OK!';
    });

PLUGIN OPTIONS

base

The base class name

on_error

The code ref for handling error

before_run

The code ref which is invoked before running main method

after_run

The code ref which is invoked after running main method

method // 'main'

The name of method in class for script

run_method // 'run'

The name of method on $c for invoking CLI script

getopt // [qw/:config posix_default no_ignore_case gnu_compat/]

The options string for Getopt::Long

cli_opt_key

The key string for keeping CLI options in context

Handling CLI Options

By default, you can get CLI options '--baz' like below

    sub main {
        my ($class, $c) = @_;

        my $opt;

        $c->parse_opt(
            'baz' => \$opt->{baz},
        )->setopt($opt);

        print 'OK!' if $c->getopt('baz');
    }

parse_opt method requires hash arguments which is same as GetOptions arguments of Getopt::Long.

setopt method sets an argument in context.

getopt method gets a value of options. To get all options when you call getopt method without args.

REPOSITORY

Amon2::CLI is hosted on github: http://github.com/bayashi/Amon2-CLI

I appreciate any feedback :D

AUTHOR

Dai Okabayashi <bayashi@cpan.org>

SEE ALSO

Amon2::Plugin::CLI

Amon2

LICENSE

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.