| 1 | package CLI::Dispatch::Command; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | sub new { | ||
| 7 | my $class = shift; | ||
| 8 | my $self = bless {}, $class; | ||
| 9 | $self->set_options(@_) if @_; | ||
| 10 | |||
| 11 | if (!$self->can('log')) { | ||
| 12 | require Log::Dump; Log::Dump->import; | ||
| 13 | } | ||
| 14 | |||
| 15 | $self; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub set_options { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | %{ $self } = @_; | ||
| 22 | |||
| 23 | if ($self->can('logger')) { | ||
| 24 | $self->logger( $self->{verbose} || $self->{debug} || $self->{logfilter} ? 1 : 0 ); | ||
| 25 | } | ||
| 26 | if ($self->can('logfilter')) { | ||
| 27 | my @filters = $self->{logfilter} ? split ',', $self->{logfilter} : (); | ||
| 28 | push @filters, '!debug' unless $self->{debug}; | ||
| 29 | $self->logfilter(@filters); | ||
| 30 | } | ||
| 31 | |||
| 32 | $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub options {} | ||
| 36 | |||
| 37 | sub option { | ||
| 38 | my ($self, $name) = @_; | ||
| 39 | |||
| 40 | defined $self->{$name} ? $self->{$name} : ''; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub run { | ||
| 44 | my ($self, @args) = @_; | ||
| 45 | |||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub run_directly { | ||
| 50 | my $self = shift; | ||
| 51 | my $class = ref $self || $self; | ||
| 52 | require CLI::Dispatch; | ||
| 53 | CLI::Dispatch->run_directly($class); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub usage { | ||
| 57 | my ($self, $no_print) = @_; | ||
| 58 | |||
| 59 | my $class = ref $self; | ||
| 60 | $class =~ s{::}{/}g; | ||
| 61 | $class .= '.pm'; | ||
| 62 | |||
| 63 | my $file = $INC{$class} || $0 or return; | ||
| 64 | my $content = do { local $/; open my $fh, '<', $file; <$fh> }; | ||
| 65 | |||
| 66 | require CLI::Dispatch::Help; | ||
| 67 | my $help = CLI::Dispatch::Help->new(%$self); | ||
| 68 | |||
| 69 | my $pod = $help->_parse_pod($content); | ||
| 70 | $pod = $help->extract_pod_body($pod); | ||
| 71 | |||
| 72 | $pod =~ /^(\S+\s+.+?)\n(?=\S)/s; # extract first paragraph | ||
| 73 | $help->output( $1 || '', $no_print ); | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | __END__ |