| 1 | package App::JobLog::Command; | ||
| 2 | $App::JobLog::Command::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: common functionality of App::JobLog commands | ||
| 4 | |||
| 5 | use App::Cmd::Setup -command; | ||
| 6 | use Modern::Perl; | ||
| 7 | use App::JobLog::Config qw(columns); | ||
| 8 | |||
| 9 | sub opt_spec { | ||
| 10 | my ( $class, $app ) = @_; | ||
| 11 | |||
| 12 | return ( $class->options($app), [ 'help' => "this usage screen" ] ); | ||
| 13 | } | ||
| 14 | |||
| 15 | # makes sure everything has some sort of description | ||
| 16 | sub description { | ||
| 17 | my ($self) = @_; | ||
| 18 | |||
| 19 | # abstract provides default text | ||
| 20 | my $desc = $self->full_description; | ||
| 21 | unless ($desc) { | ||
| 22 | ( $desc = $self->abstract ) =~ s/^\s++|\s++$//g; | ||
| 23 | |||
| 24 | # ensure initial capitalization | ||
| 25 | $desc =~ s/^(\p{Ll})/uc $1/e; | ||
| 26 | |||
| 27 | # add sentence-terminal punctuation as necessary | ||
| 28 | $desc =~ s/(\w)$/$1./; | ||
| 29 | } | ||
| 30 | |||
| 31 | # make sure things are wrapped nicely | ||
| 32 | _wrap( \$desc ); | ||
| 33 | |||
| 34 | # space between description and options text | ||
| 35 | $desc .= "\n"; | ||
| 36 | return $desc; | ||
| 37 | } | ||
| 38 | |||
| 39 | # performs text wrapping while preserving the formatting of lines beginning with whitespace | ||
| 40 | sub _wrap { | ||
| 41 | my $desc = shift; | ||
| 42 | require Text::WrapI18N; | ||
| 43 | $Text::WrapI18N::columns = columns; | ||
| 44 | my ( $current, @gathered ); | ||
| 45 | for my $line ( $$desc =~ /^(.*?)\s*$/mg ) { | ||
| 46 | if ( $line =~ /^\S/ ) { | ||
| 47 | if ($current) { | ||
| 48 | $current .= " $line"; | ||
| 49 | } | ||
| 50 | else { | ||
| 51 | $current = $line; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | push @gathered, Text::WrapI18N::wrap( '', '', $current ) | ||
| 56 | if defined $current; | ||
| 57 | push @gathered, $line; | ||
| 58 | $current = undef; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | push @gathered, Text::WrapI18N::wrap( '', '', $current ) | ||
| 62 | if defined $current; | ||
| 63 | $$desc = join "\n", @gathered; | ||
| 64 | } | ||
| 65 | |||
| 66 | # override to make full description | ||
| 67 | sub full_description { } | ||
| 68 | |||
| 69 | sub validate_args { | ||
| 70 | my ( $self, $opt, $args ) = @_; | ||
| 71 | die $self->_usage_text if $opt->{help}; | ||
| 72 | $self->validate( $opt, $args ); | ||
| 73 | } | ||
| 74 | |||
| 75 | # obtains command name | ||
| 76 | sub name { | ||
| 77 | ( my $command = shift ) =~ s/.*:://; | ||
| 78 | return $command; | ||
| 79 | } | ||
| 80 | |||
| 81 | # by default a command has no options other than --help | ||
| 82 | sub options { } | ||
| 83 | |||
| 84 | # by default a command does no argument validation | ||
| 85 | sub validate { } | ||
| 86 | |||
| 87 | # add to simple commands after argument signature so they'll complain if given arguments | ||
| 88 | sub simple_command_check { | ||
| 89 | my ( $self, $args ) = @_; | ||
| 90 | $self->usage_error("This command does not expect any arguments! No action taken.") if @$args; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | |||
| 95 | __END__ |