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

Getopt::Chain::Declare - Syntactic sugar for command-line processing like svn and git

SYNPOSIS

package My::Command;

use Getopt::Chain::Declare;

start [qw/ verbose|v /]; # These are "global"
                         # my-command --verbose ...

# my-command ? initialize ... --> my-command help initialize ...
rewrite qr/^\?(.*)/ => sub { "help ".($1||'') };

# NOTE: Rewriting applies to the command sequence, NOT options

# my-command about ... --> my-command help about
rewrite [ ['about', 'copying'] ] => sub { "help $1" };

# my-command initialize --dir=...
on initialize => [qw/ dir|d=s /], sub {
    my $context = shift;

    my $dir = $context->option( 'dir' )

    # Do initialize stuff with $dir
};

# my-command help
on help => undef, sub {
    my $context = shift;

    # Do help stuff ...
    # First argument is undef because help
    # doesn't take any options
    
};

under help => sub {

    # my-command help create
    # my-command help initialize
    on [ [ qw/create initialize/ ] ] => undef, sub {
        my $context = shift;

        # Do help for create/initialize
        # Both: "help create" and "help initialize" go here
    };

    # my-command help about
    on 'about' => undef, sub {
        my $context = shift;

        # Help for about...
    };

    # my-command help copying
    on 'copying' => undef, sub {
        my $context = shift;

        # Help for copying...
    };

    # my-command help ...
    on qr/^(\S+)$/ => undef, sub {
       my $context = shift;
       my $topic = $1;

        # Catch-all for anything not fitting into the above...
        
        warn "I don't know about \"$topic\"\n"
    };
};

# ... elsewhere ...

My::Command->new->run( [ @arguments ] )
My::Command->new->run # Just run with @ARGV

DESCRIPTION

For more information, see Getopt::Chain