| 1 | #============================================================================= | ||
| 2 | # | ||
| 3 | # Module: Term::CLI::HelpText | ||
| 4 | # | ||
| 5 | # Description: Class for sets of (sub-)commands in Term::CLI | ||
| 6 | # | ||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||
| 8 | # Created: 19/02/18 | ||
| 9 | # | ||
| 10 | # Copyright (c) 2018 Steven Bakker | ||
| 11 | # | ||
| 12 | # This module is free software; you can redistribute it and/or modify | ||
| 13 | # it under the same terms as Perl itself. See "perldoc perlartistic." | ||
| 14 | # | ||
| 15 | # This software is distributed in the hope that it will be useful, | ||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 18 | # | ||
| 19 | #============================================================================= | ||
| 20 | |||
| 21 | use 5.014_001; | ||
| 22 | |||
| 23 | package Term::CLI::Role::HelpText 0.051003 { | ||
| 24 | |||
| 25 | use Modern::Perl 1.20140107; | ||
| 26 | |||
| 27 | use Types::Standard 1.000005 qw( | ||
| 28 | Maybe | ||
| 29 | Str | ||
| 30 | ); | ||
| 31 | |||
| 32 | use Moo::Role; | ||
| 33 | use namespace::clean 0.25; | ||
| 34 | |||
| 35 | requires 'options'; | ||
| 36 | requires 'has_commands'; | ||
| 37 | requires 'commands'; | ||
| 38 | |||
| 39 | has usage => ( | ||
| 40 | is => 'rw', | ||
| 41 | isa => Maybe[Str], | ||
| 42 | ); | ||
| 43 | |||
| 44 | has description => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => Maybe[Str], | ||
| 47 | ); | ||
| 48 | |||
| 49 | has summary => ( | ||
| 50 | is => 'rw', | ||
| 51 | isa => Str, | ||
| 52 | default => sub{''}, | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | sub get_options_summary { | ||
| 57 | my $self = shift; | ||
| 58 | my %args = (with_options => 'both', @_); | ||
| 59 | |||
| 60 | my $with_options = 0x00; | ||
| 61 | |||
| 62 | if ($args{with_options} =~ /short/i) { | ||
| 63 | $with_options |= 0x01; | ||
| 64 | } | ||
| 65 | if ($args{with_options} =~ /long/i) { | ||
| 66 | $with_options |= 0x02; | ||
| 67 | } | ||
| 68 | if ($args{with_options} =~ /both/i) { | ||
| 69 | $with_options |= 0x03; | ||
| 70 | } | ||
| 71 | |||
| 72 | my @options; | ||
| 73 | my $short_opts_no_arg = ''; | ||
| 74 | if (my $opt_specs = $self->options) { | ||
| 75 | for my $spec (@$opt_specs) { | ||
| 76 | my $long_arg = my $short_arg = ''; | ||
| 77 | if ($spec =~ /=(.*)$/) { | ||
| 78 | $long_arg = "=I<$1>"; | ||
| 79 | $short_arg = "I<$1>"; | ||
| 80 | } | ||
| 81 | elsif ($spec =~ /:(.*)$/) { | ||
| 82 | $long_arg = "[=I<$1>]"; | ||
| 83 | $short_arg = "[I<$1>]"; | ||
| 84 | } | ||
| 85 | for my $optname (split(qr/\|/, $spec =~ s/^([^!+=:]+).*/$1/r)) { | ||
| 86 | if (length $optname == 1) { | ||
| 87 | if ($with_options & 0x01) { | ||
| 88 | if (length $short_arg == 0) { | ||
| 89 | $short_opts_no_arg .= $optname; | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | push @options, "[B<-$optname>$short_arg]"; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | elsif ($with_options & 0x02) { | ||
| 97 | push @options, "[B<--$optname>$long_arg]"; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | if (length $short_opts_no_arg) { | ||
| 103 | push @options, "[B<-$short_opts_no_arg>]"; | ||
| 104 | } | ||
| 105 | return join(' ', @options); | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub usage_text { | ||
| 110 | my $self = shift; | ||
| 111 | |||
| 112 | my %args = ( | ||
| 113 | with_options => 'both', | ||
| 114 | with_arguments => 1, | ||
| 115 | with_subcommands => 1, | ||
| 116 | @_ | ||
| 117 | ); | ||
| 118 | |||
| 119 | if ($self->usage) { | ||
| 120 | return $self->usage; | ||
| 121 | } | ||
| 122 | |||
| 123 | my $usage_prefix = 'B<'.$self->name.'>'; | ||
| 124 | my $usage_suffix = ''; | ||
| 125 | |||
| 126 | if ($args{with_arguments} and $self->has_arguments) { | ||
| 127 | my @args; | ||
| 128 | for my $arg ($self->arguments) { | ||
| 129 | #my $name = 'I<'.$arg->name.'>'; | ||
| 130 | my $name = $arg->name; | ||
| 131 | my $str = $arg->max_occur > 1 ? "I<${name}1>" : "I<$name>"; | ||
| 132 | |||
| 133 | if ($arg->min_occur > 1) { | ||
| 134 | for my $n (2..$arg->min_occur) { | ||
| 135 | $str .= " I<${name}$n>"; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | if ($arg->max_occur <= 0) { | ||
| 140 | $str .= ' ...'; | ||
| 141 | } | ||
| 142 | elsif ($arg->max_occur == $arg->min_occur + 1) { | ||
| 143 | $str .= " [I<${name}".$arg->max_occur.">]" if $arg->max_occur > 1; | ||
| 144 | } | ||
| 145 | elsif ($arg->max_occur == 2 && $arg->min_occur <= 1) { | ||
| 146 | $str .= " [I<${name}".$arg->max_occur.">]"; | ||
| 147 | } | ||
| 148 | elsif ($arg->max_occur > $arg->min_occur) { | ||
| 149 | $str .= ' [' | ||
| 150 | . "I<$name".($arg->min_occur+1).">" | ||
| 151 | . ' ... ' | ||
| 152 | . "I<$name".$arg->max_occur.">" | ||
| 153 | . ']' | ||
| 154 | ; | ||
| 155 | } | ||
| 156 | |||
| 157 | if ($arg->min_occur <= 0) { | ||
| 158 | $str = "[$str]"; | ||
| 159 | } | ||
| 160 | push @args, $str; | ||
| 161 | } | ||
| 162 | $usage_suffix = join(' ', @args); | ||
| 163 | } | ||
| 164 | |||
| 165 | if ($args{with_subcommands} and $self->has_commands) { | ||
| 166 | my @sub_commands = $self->commands; | ||
| 167 | my $sub_commands_text; | ||
| 168 | if (@sub_commands == 1) { | ||
| 169 | $sub_commands_text | ||
| 170 | = $sub_commands[0]->usage_text(%args, with_options => 'none'); | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | $sub_commands_text | ||
| 174 | = '{'.join('|', map { 'B<'.$_->name.'>' } @sub_commands).'}'; | ||
| 175 | } | ||
| 176 | $usage_suffix .= ' ' if length $usage_suffix; | ||
| 177 | $usage_suffix .= $sub_commands_text; | ||
| 178 | } | ||
| 179 | |||
| 180 | $usage_suffix = " $usage_suffix" if length $usage_suffix; | ||
| 181 | |||
| 182 | my $opts = $self->get_options_summary( with_options => $args{with_options} ); | ||
| 183 | |||
| 184 | if (length $opts) { | ||
| 185 | return "$usage_prefix $opts$usage_suffix"; | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | return "$usage_prefix$usage_suffix"; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | } | ||
| 194 | |||
| 195 | 1; | ||
| 196 | |||
| 197 | __END__ |