| 1 | package Command::V2; # additional methods to produce documentation, TODO: turn into a real view | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Term::ANSIColor qw(); | ||
| 6 | use Pod::Simple::Text; | ||
| 7 | require Text::Wrap; | ||
| 8 | |||
| 9 | # This is changed with "local" where used in some places | ||
| 10 | $Text::Wrap::columns = 100; | ||
| 11 | |||
| 12 | # Required for color output | ||
| 13 | eval { | ||
| 14 | binmode STDOUT, ":utf8"; | ||
| 15 | binmode STDERR, ":utf8"; | ||
| 16 | }; | ||
| 17 | |||
| 18 | sub help_brief { | ||
| 19 | my $self = shift; | ||
| 20 | if (my $doc = $self->__meta__->doc) { | ||
| 21 | return $doc; | ||
| 22 | } | ||
| 23 | else { | ||
| 24 | my @parents = $self->__meta__->ancestry_class_metas; | ||
| 25 | for my $parent (@parents) { | ||
| 26 | if (my $doc = $parent->doc) { | ||
| 27 | return $doc; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | return "no description!!!: define 'doc' in the class definition for " | ||
| 31 | . $self->class; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | sub help_synopsis { | ||
| 36 | my $self = shift; | ||
| 37 | return ''; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub help_detail { | ||
| 41 | my $self = shift; | ||
| 42 | return "!!! define help_detail() in module " . ref($self) || $self . "!"; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub sub_command_category { | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub sub_command_sort_position { | ||
| 50 | # override to do something besides alpha sorting by name | ||
| 51 | return '9999999999 ' . $_[0]->command_name_brief; | ||
| 52 | } | ||
| 53 | |||
| 54 | # LEGACY: poorly named | ||
| 55 | sub help_usage_command_pod { | ||
| 56 | return shift->doc_manual(@_); | ||
| 57 | } | ||
| 58 | |||
| 59 | # LEGACY: poorly named | ||
| 60 | sub help_usage_complete_text { | ||
| 61 | shift->doc_help(@_) | ||
| 62 | } | ||
| 63 | |||
| 64 | sub doc_help { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | my $command_name = $self->command_name; | ||
| 68 | my $text; | ||
| 69 | |||
| 70 | my $extra_help = ''; | ||
| 71 | my @extra_help = $self->_additional_help_sections; | ||
| 72 | while (@extra_help) { | ||
| 73 | my $title = shift @extra_help || ''; | ||
| 74 | my $content = shift @extra_help || ''; | ||
| 75 | $extra_help .= sprintf( | ||
| 76 | "%s\n\n%s\n", | ||
| 77 | Term::ANSIColor::colored($title, 'underline'), | ||
| 78 | _pod2txt($content) | ||
| 79 | ), | ||
| 80 | } | ||
| 81 | |||
| 82 | # standard: update this to do the old --help format | ||
| 83 | my $synopsis = $self->help_synopsis; | ||
| 84 | my $required_inputs = $self->help_options(is_optional => 0, is_input => 1); | ||
| 85 | my $required_outputs = $self->help_options(is_optional => 0, is_output => 1); | ||
| 86 | my $required_params = $self->help_options(is_optional => 0, is_param => 1); | ||
| 87 | my $optional_inputs = $self->help_options(is_optional => 1, is_input => 1); | ||
| 88 | my $optional_outputs = $self->help_options(is_optional => 1, is_output => 1); | ||
| 89 | my $optional_params = $self->help_options(is_optional => 1, is_param => 1); | ||
| 90 | my @parts; | ||
| 91 | |||
| 92 | push @parts, Term::ANSIColor::colored('USAGE', 'underline'); | ||
| 93 | push @parts, | ||
| 94 | Text::Wrap::wrap( | ||
| 95 | ' ', | ||
| 96 | ' ', | ||
| 97 | Term::ANSIColor::colored($self->command_name, 'bold'), | ||
| 98 | $self->_shell_args_usage_string || '', | ||
| 99 | ); | ||
| 100 | |||
| 101 | push @parts, | ||
| 102 | ( $synopsis | ||
| 103 | ? sprintf("%s\n%s\n", Term::ANSIColor::colored("SYNOPSIS", 'underline'), $synopsis) | ||
| 104 | : '' | ||
| 105 | ); | ||
| 106 | push @parts, | ||
| 107 | ( $required_inputs | ||
| 108 | ? sprintf("%s\n%s\n", Term::ANSIColor::colored("REQUIRED INPUTS", 'underline'), $required_inputs) | ||
| 109 | : '' | ||
| 110 | ); | ||
| 111 | push @parts, | ||
| 112 | ( $required_params | ||
| 113 | ? sprintf("%s\n%s\n", Term::ANSIColor::colored("REQUIRED PARAMS", 'underline'), $required_params) | ||
| 114 | : '' | ||
| 115 | ); | ||
| 116 | push @parts, | ||
| 117 | ( $optional_inputs | ||
| 118 | ? sprintf("%s\n%s\n", Term::ANSIColor::colored("OPTIONAL INPUTS", 'underline'), $optional_inputs) | ||
| 119 | : '' | ||
| 120 | ); | ||
| 121 | push @parts, | ||
| 122 | ( $optional_params | ||
| 123 | ? sprintf("%s\n%s\n", Term::ANSIColor::colored("OPTIONAL PARAMS", 'underline'), $optional_params) | ||
| 124 | : '' | ||
| 125 | ); | ||
| 126 | push @parts, | ||
| 127 | ( $required_outputs | ||
| 128 | ? sprintf("%s\n%s\n", Term::ANSIColor::colored("REQUIRED OUTPUTS", 'underline'), $required_outputs) | ||
| 129 | : '' | ||
| 130 | ); | ||
| 131 | push @parts, | ||
| 132 | ( $optional_outputs | ||
| 133 | ? sprintf("%s\n%s\n", Term::ANSIColor::colored("OPTIONAL OUTPUTS", 'underline'), $optional_outputs) | ||
| 134 | : '' | ||
| 135 | ); | ||
| 136 | push @parts, | ||
| 137 | sprintf( | ||
| 138 | "%s\n%s\n", | ||
| 139 | Term::ANSIColor::colored("DESCRIPTION", 'underline'), | ||
| 140 | _pod2txt($self->help_detail || '') | ||
| 141 | ); | ||
| 142 | push @parts, | ||
| 143 | ( $extra_help ? $extra_help : '' ); | ||
| 144 | |||
| 145 | $text = sprintf( | ||
| 146 | "\n%s\n%s\n\n%s%s%s%s%s%s%s%s%s\n", | ||
| 147 | @parts | ||
| 148 | ); | ||
| 149 | |||
| 150 | return $text; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub parent_command_class { | ||
| 154 | my $class = shift; | ||
| 155 | $class = ref($class) if ref($class); | ||
| 156 | my @components = split("::", $class); | ||
| 157 | return if @components == 1; | ||
| 158 | my $parent = join("::", @components[0..$#components-1]); | ||
| 159 | return $parent if $parent->can("command_name"); | ||
| 160 | return; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub doc_sections { | ||
| 164 | my $self = shift; | ||
| 165 | my @sections; | ||
| 166 | |||
| 167 | my $command_name = $self->command_name; | ||
| 168 | |||
| 169 | my $version = do { no strict; ${ $self->class . '::VERSION' } }; | ||
| 170 | my $help_brief = $self->help_brief; | ||
| 171 | my $datetime = $self->__context__->now; | ||
| 172 | my ($date,$time) = split(' ',$datetime); | ||
| 173 | |||
| 174 | push(@sections, UR::Doc::Section->create( | ||
| 175 | title => "NAME", | ||
| 176 | content => "$command_name" . ($help_brief ? " - $help_brief" : ""), | ||
| 177 | format => "pod", | ||
| 178 | )); | ||
| 179 | |||
| 180 | push(@sections, UR::Doc::Section->create( | ||
| 181 | title => "VERSION", | ||
| 182 | content => "This document " # separated to trick the version updater | ||
| 183 | . "describes $command_name " | ||
| 184 | . ($version ? "version $version " : "") | ||
| 185 | . "($date at $time)", | ||
| 186 | format => "pod", | ||
| 187 | )); | ||
| 188 | |||
| 189 | my $synopsis = $self->command_name . ' ' . $self->_shell_args_usage_string . "\n\n" . $self->help_synopsis; | ||
| 190 | if ($synopsis) { | ||
| 191 | push(@sections, UR::Doc::Section->create( | ||
| 192 | title => "SYNOPSIS", | ||
| 193 | content => $synopsis, | ||
| 194 | format => 'pod' | ||
| 195 | )); | ||
| 196 | } | ||
| 197 | |||
| 198 | my $required_args = $self->help_options(is_optional => 0, format => "pod"); | ||
| 199 | if ($required_args) { | ||
| 200 | push(@sections, UR::Doc::Section->create( | ||
| 201 | title => "REQUIRED ARGUMENTS", | ||
| 202 | content => "=over\n\n$required_args\n\n=back\n\n", | ||
| 203 | format => 'pod' | ||
| 204 | )); | ||
| 205 | } | ||
| 206 | |||
| 207 | my $optional_args = $self->help_options(is_optional => 1, format => "pod"); | ||
| 208 | if ($optional_args) { | ||
| 209 | push(@sections, UR::Doc::Section->create( | ||
| 210 | title => "OPTIONAL ARGUMENTS", | ||
| 211 | content => "=over\n\n$optional_args\n\n=back\n\n", | ||
| 212 | format => 'pod' | ||
| 213 | )); | ||
| 214 | } | ||
| 215 | |||
| 216 | my $manual = $self->_doc_manual_body || $self->help_detail; | ||
| 217 | push(@sections, UR::Doc::Section->create( | ||
| 218 | title => "DESCRIPTION", | ||
| 219 | content => $manual, | ||
| 220 | format => 'pod', | ||
| 221 | )); | ||
| 222 | |||
| 223 | my @extra_help = $self->_additional_help_sections; | ||
| 224 | while (@extra_help) { | ||
| 225 | my $title = shift @extra_help || ''; | ||
| 226 | my $content = shift @extra_help || ''; | ||
| 227 | push (@sections, UR::Doc::Section->create( | ||
| 228 | title => $title, | ||
| 229 | content => $content, | ||
| 230 | format => 'pod' | ||
| 231 | )); | ||
| 232 | } | ||
| 233 | |||
| 234 | if ($self->can("doc_sub_commands")) { | ||
| 235 | my $sub_commands = $self->doc_sub_commands(brief => 1); | ||
| 236 | if ($sub_commands) { | ||
| 237 | push(@sections, UR::Doc::Section->create( | ||
| 238 | title => "SUB-COMMANDS", | ||
| 239 | content => $sub_commands, | ||
| 240 | format => "pod", | ||
| 241 | )); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | my @footer_section_methods = ( | ||
| 246 | 'LICENSE' => '_doc_license', | ||
| 247 | 'AUTHORS' => '_doc_authors', | ||
| 248 | 'CREDITS' => '_doc_credits', | ||
| 249 | 'BUGS' => '_doc_bugs', | ||
| 250 | 'SEE ALSO' => '_doc_see_also' | ||
| 251 | ); | ||
| 252 | |||
| 253 | while (@footer_section_methods) { | ||
| 254 | my $header = shift @footer_section_methods; | ||
| 255 | my $method = shift @footer_section_methods; | ||
| 256 | my @txt = $self->$method; | ||
| 257 | next if (@txt == 0 or (@txt == 1 and not $txt[0])); | ||
| 258 | my $content; | ||
| 259 | if (@txt == 1) { | ||
| 260 | $content = $txt[0]; | ||
| 261 | } else { | ||
| 262 | $content = join("\n", @txt); | ||
| 263 | } | ||
| 264 | |||
| 265 | push(@sections, UR::Doc::Section->create( | ||
| 266 | title => $header, | ||
| 267 | content => $content, | ||
| 268 | format => "pod", | ||
| 269 | )); | ||
| 270 | } | ||
| 271 | |||
| 272 | return @sections; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub doc_sub_commands { | ||
| 276 | my $self = shift; | ||
| 277 | return; | ||
| 278 | } | ||
| 279 | |||
| 280 | sub doc_manual { | ||
| 281 | my $self = shift; | ||
| 282 | my $pod = $self->_doc_name_version; | ||
| 283 | |||
| 284 | my $synopsis = $self->command_name . ' ' . $self->_shell_args_usage_string . "\n\n" . $self->help_synopsis; | ||
| 285 | my $required_args = $self->help_options(is_optional => 0, format => "pod"); | ||
| 286 | my $optional_args = $self->help_options(is_optional => 1, format => "pod"); | ||
| 287 | $pod .= | ||
| 288 | ( | ||
| 289 | $synopsis | ||
| 290 | ? "=head1 SYNOPSIS\n\n" . $synopsis . "\n\n" | ||
| 291 | : '' | ||
| 292 | ) | ||
| 293 | . ( | ||
| 294 | $required_args | ||
| 295 | ? "=head1 REQUIRED ARGUMENTS\n\n=over\n\n" . $required_args . "\n\n=back\n\n" | ||
| 296 | : '' | ||
| 297 | ) | ||
| 298 | . ( | ||
| 299 | $optional_args | ||
| 300 | ? "=head1 OPTIONAL ARGUMENTS\n\n=over\n\n" . $optional_args . "\n\n=back\n\n" | ||
| 301 | : '' | ||
| 302 | ); | ||
| 303 | |||
| 304 | my $manual = $self->_doc_manual_body; | ||
| 305 | my $help = $self->help_detail; | ||
| 306 | if ($manual or $help) { | ||
| 307 | $pod .= "=head1 DESCRIPTION:\n\n"; | ||
| 308 | |||
| 309 | my $txt = $manual || $help; | ||
| 310 | if ($txt =~ /^\=/) { | ||
| 311 | # pure POD | ||
| 312 | $pod .= $manual; | ||
| 313 | } | ||
| 314 | else { | ||
| 315 | $txt =~ s/\n/\n\n/g; | ||
| 316 | $pod .= $txt; | ||
| 317 | #$pod .= join('', map { " $_\n" } split ("\n",$txt)) . "\n"; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | $pod .= $self->_doc_footer(); | ||
| 322 | $pod .= "\n\n=cut\n\n"; | ||
| 323 | return "\n$pod"; | ||
| 324 | } | ||
| 325 | |||
| 326 | |||
| 327 | sub _doc_name_version { | ||
| 328 | my $self = shift; | ||
| 329 | |||
| 330 | my $command_name = $self->command_name; | ||
| 331 | my $pod; | ||
| 332 | |||
| 333 | # standard: update this to do the old --help format | ||
| 334 | my $synopsis = $self->command_name . ' ' . $self->_shell_args_usage_string . "\n\n" . $self->help_synopsis; | ||
| 335 | my $help_brief = $self->help_brief; | ||
| 336 | my $version = do { no strict; ${ $self->class . '::VERSION' } }; | ||
| 337 | my $datetime = $self->__context__->now; | ||
| 338 | my ($date,$time) = split(' ',$datetime); | ||
| 339 | |||
| 340 | $pod = | ||
| 341 | "\n=pod" | ||
| 342 | . "\n\n=head1 NAME" | ||
| 343 | . "\n\n" | ||
| 344 | . $self->command_name | ||
| 345 | . ($help_brief ? " - " . $self->help_brief : '') | ||
| 346 | . "\n\n"; | ||
| 347 | |||
| 348 | $pod .= | ||
| 349 | "\n\n=head1 VERSION" | ||
| 350 | . "\n\n" | ||
| 351 | . "This document " # separated to trick the version updater | ||
| 352 | . "describes " . $self->command_name; | ||
| 353 | |||
| 354 | if ($version) { | ||
| 355 | $pod .= " version " . $version . " ($date at $time).\n\n"; | ||
| 356 | } | ||
| 357 | else { | ||
| 358 | $pod .= " ($date at $time)\n\n"; | ||
| 359 | } | ||
| 360 | |||
| 361 | return $pod; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub _doc_manual_body { | ||
| 365 | return ''; | ||
| 366 | } | ||
| 367 | |||
| 368 | sub help_header { | ||
| 369 | my $class = shift; | ||
| 370 | return sprintf("%s - %-80s\n", | ||
| 371 | $class->command_name | ||
| 372 | ,$class->help_brief | ||
| 373 | ) | ||
| 374 | } | ||
| 375 | |||
| 376 | sub help_options { | ||
| 377 | my $self = shift; | ||
| 378 | my %params = @_; | ||
| 379 | |||
| 380 | my $format = delete $params{format}; | ||
| 381 | my @property_meta = $self->_shell_args_property_meta(%params); | ||
| 382 | |||
| 383 | my @data; | ||
| 384 | my $max_name_length = 0; | ||
| 385 | for my $property_meta (@property_meta) { | ||
| 386 | my $param_name = $self->_shell_arg_name_from_property_meta($property_meta); | ||
| 387 | if ($property_meta->{shell_args_position}) { | ||
| 388 | $param_name = uc($param_name); | ||
| 389 | } | ||
| 390 | |||
| 391 | #$param_name = "--$param_name"; | ||
| 392 | my $doc = $property_meta->doc; | ||
| 393 | my $valid_values = $property_meta->valid_values; | ||
| 394 | my $example_values = $property_meta->example_values; | ||
| 395 | unless ($doc) { | ||
| 396 | # Maybe a parent class has documentation for this property | ||
| 397 | eval { | ||
| 398 | foreach my $ancestor_class_meta ( $property_meta->class_meta->ancestry_class_metas ) { | ||
| 399 | my $ancestor_property_meta = $ancestor_class_meta->property_meta_for_name($property_meta->property_name); | ||
| 400 | if ($ancestor_property_meta and $doc = $ancestor_property_meta->doc) { | ||
| 401 | last; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | }; | ||
| 405 | } | ||
| 406 | |||
| 407 | if (!$doc) { | ||
| 408 | if (!$valid_values) { | ||
| 409 | $doc = "(undocumented)"; | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | $doc = ''; | ||
| 413 | } | ||
| 414 | } | ||
| 415 | if ($valid_values) { | ||
| 416 | $doc .= "\nvalid values:\n"; | ||
| 417 | for my $v (@$valid_values) { | ||
| 418 | $doc .= " " . $v . "\n"; | ||
| 419 | $max_name_length = length($v)+2 if $max_name_length < length($v)+2; | ||
| 420 | } | ||
| 421 | chomp $doc; | ||
| 422 | } | ||
| 423 | if ($example_values && @$example_values) { | ||
| 424 | $doc .= "\nexample" . (@$example_values > 1 and 's') . ":\n"; | ||
| 425 | $doc .= join(', ', | ||
| 426 | map { ref($_) ? Data::Dumper->new([$_])->Terse(1)->Dump() : $_ } @$example_values | ||
| 427 | ); | ||
| 428 | chomp($doc); | ||
| 429 | } | ||
| 430 | $max_name_length = length($param_name) if $max_name_length < length($param_name); | ||
| 431 | |||
| 432 | my $param_type = $property_meta->data_type || ''; | ||
| 433 | if (defined($param_type) and $param_type !~ m/::/) { | ||
| 434 | $param_type = ucfirst(lc($param_type)); | ||
| 435 | } | ||
| 436 | |||
| 437 | my $default_value; | ||
| 438 | if (defined($default_value = $property_meta->default_value) | ||
| 439 | || defined(my $calculated_default = $property_meta->calculated_default) | ||
| 440 | ) { | ||
| 441 | unless (defined $default_value) { | ||
| 442 | $default_value = $calculated_default->() | ||
| 443 | } | ||
| 444 | |||
| 445 | if ($param_type eq 'Boolean') { | ||
| 446 | $default_value = $default_value ? "'true'" : "'false' (--no$param_name)"; | ||
| 447 | } elsif ($property_meta->is_many && ref($default_value) eq 'ARRAY') { | ||
| 448 | if (@$default_value) { | ||
| 449 | $default_value = "('" . join("','",@$default_value) . "')"; | ||
| 450 | } else { | ||
| 451 | $default_value = "()"; | ||
| 452 | } | ||
| 453 | } else { | ||
| 454 | $default_value = "'$default_value'"; | ||
| 455 | } | ||
| 456 | $default_value = "\nDefault value $default_value if not specified"; | ||
| 457 | } | ||
| 458 | |||
| 459 | push @data, [$param_name, $param_type, $doc, $default_value]; | ||
| 460 | if ($param_type eq 'Boolean') { | ||
| 461 | push @data, ['no'.$param_name, $param_type, "Make $param_name 'false'" ]; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | my $text = ''; | ||
| 465 | for my $row (@data) { | ||
| 466 | if (defined($format) and $format eq 'pod') { | ||
| 467 | $text .= "\n=item " . $row->[0] . ($row->[1]? ' I<' . $row->[1] . '>' : '') . "\n\n" . $row->[2] . "\n". ($row->[3]? $row->[3] . "\n" : ''); | ||
| 468 | } | ||
| 469 | elsif (defined($format) and $format eq 'html') { | ||
| 470 | $text .= "\n\t<br>" . $row->[0] . ($row->[1]? ' <em>' . $row->[1] . '</em>' : '') . "<br> " . $row->[2] . ($row->[3]? "<br>" . $row->[3] : '') . "<br>\n"; | ||
| 471 | } | ||
| 472 | else { | ||
| 473 | $text .= sprintf( | ||
| 474 | " %s\n%s\n", | ||
| 475 | Term::ANSIColor::colored($row->[0], 'bold'), # . " " . $row->[1], | ||
| 476 | Text::Wrap::wrap( | ||
| 477 | " ", # 1st line indent, | ||
| 478 | " ", # all other lines indent, | ||
| 479 | $row->[2], | ||
| 480 | $row->[3] || '', | ||
| 481 | ), | ||
| 482 | ); | ||
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 | return $text; | ||
| 487 | } | ||
| 488 | |||
| 489 | |||
| 490 | sub _doc_footer { | ||
| 491 | my $self = shift; | ||
| 492 | my $pod = ''; | ||
| 493 | |||
| 494 | my @method_header_map = ( | ||
| 495 | 'LICENSE' => '_doc_license', | ||
| 496 | 'AUTHORS' => '_doc_authors', | ||
| 497 | 'CREDITS' => '_doc_credits', | ||
| 498 | 'BUGS' => '_doc_bugs', | ||
| 499 | 'SEE ALSO' => '_doc_see_also' | ||
| 500 | ); | ||
| 501 | |||
| 502 | while (@method_header_map) { | ||
| 503 | my $header = shift @method_header_map; | ||
| 504 | my $method = shift @method_header_map; | ||
| 505 | my @txt = $self->$method; | ||
| 506 | next if (@txt == 0 or (@txt == 1 and not $txt[0])); | ||
| 507 | if (@txt == 1) { | ||
| 508 | my @lines = split("\n",$txt[0]); | ||
| 509 | $pod .= "=head1 $header\n\n" | ||
| 510 | . join(" \n", @lines) | ||
| 511 | . "\n\n"; | ||
| 512 | } | ||
| 513 | else { | ||
| 514 | $pod .= "=head1 $header\n\n" | ||
| 515 | . join("\n ",@txt); | ||
| 516 | $pod .= "\n\n"; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | return $pod; | ||
| 521 | } | ||
| 522 | |||
| 523 | sub _doc_license { | ||
| 524 | return ''; | ||
| 525 | } | ||
| 526 | |||
| 527 | sub _doc_authors { | ||
| 528 | return (); | ||
| 529 | } | ||
| 530 | |||
| 531 | sub _doc_credits { | ||
| 532 | return ''; | ||
| 533 | } | ||
| 534 | |||
| 535 | sub _doc_bugs { | ||
| 536 | return ''; | ||
| 537 | } | ||
| 538 | |||
| 539 | sub _doc_see_also { | ||
| 540 | return (); | ||
| 541 | } | ||
| 542 | |||
| 543 | |||
| 544 | sub _shell_args_usage_string { | ||
| 545 | my $self = shift; | ||
| 546 | |||
| 547 | return eval { | ||
| 548 | if ( $self->isa('Command::Tree') ) { | ||
| 549 | return '...'; | ||
| 550 | } | ||
| 551 | elsif ($self->can("_execute_body") eq __PACKAGE__->can("_execute_body")) { | ||
| 552 | return '(no execute!)'; | ||
| 553 | } | ||
| 554 | elsif ($self->__meta__->is_abstract) { | ||
| 555 | return '(no sub commands!)'; | ||
| 556 | } | ||
| 557 | else { | ||
| 558 | return join( | ||
| 559 | " ", | ||
| 560 | map { | ||
| 561 | $self->_shell_arg_usage_string_from_property_meta($_) | ||
| 562 | } $self->_shell_args_property_meta() | ||
| 563 | |||
| 564 | ); | ||
| 565 | } | ||
| 566 | }; | ||
| 567 | } | ||
| 568 | |||
| 569 | sub _shell_args_usage_string_abbreviated { | ||
| 570 | my $self = shift; | ||
| 571 | my $detailed = $self->_shell_args_usage_string; | ||
| 572 | if (length($detailed) <= 20) { | ||
| 573 | return $detailed; | ||
| 574 | } | ||
| 575 | else { | ||
| 576 | return substr($detailed,0,17) . '...'; | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | sub sub_command_mapping { | ||
| 581 | my ($self, $class) = @_; | ||
| 582 | return if !$class; | ||
| 583 | no strict 'refs'; | ||
| 584 | my $mapping = ${ $class . '::SUB_COMMAND_MAPPING'}; | ||
| 585 | if (ref($mapping) eq 'HASH') { | ||
| 586 | return $mapping; | ||
| 587 | } else { | ||
| 588 | return; | ||
| 589 | } | ||
| 590 | }; | ||
| 591 | |||
| 592 | sub command_name { | ||
| 593 | my $self = shift; | ||
| 594 | my $class = ref($self) || $self; | ||
| 595 | my $prepend = ''; | ||
| 596 | |||
| 597 | |||
| 598 | # There can be a hash in the command entry point class that maps | ||
| 599 | # root level tools to classes so they can be in a different location | ||
| 600 | # ...this bit of code considers that misdirection: | ||
| 601 | my $entry_point_class = $Command::entry_point_class; | ||
| 602 | my $mapping = $self->sub_command_mapping($entry_point_class); | ||
| 603 | for my $k (%$mapping) { | ||
| 604 | my $v = $mapping->{$k}; | ||
| 605 | if ($v && $v eq $class) { | ||
| 606 | my @words = grep { $_ ne 'Command' } split(/::/,$class); | ||
| 607 | return join(' ', $self->_command_name_for_class_word($words[0]), $k); | ||
| 608 | } | ||
| 609 | } | ||
| 610 | |||
| 611 | |||
| 612 | if (defined($entry_point_class) and $class =~ /^($entry_point_class)(::.+|)$/) { | ||
| 613 | $prepend = $Command::entry_point_bin; | ||
| 614 | $class = $2; | ||
| 615 | if ($class =~ s/^:://) { | ||
| 616 | $prepend .= ' '; | ||
| 617 | } | ||
| 618 | } | ||
| 619 | my @words = grep { $_ ne 'Command' } split(/::/,$class); | ||
| 620 | my $n = join(' ', map { $self->_command_name_for_class_word($_) } @words); | ||
| 621 | return $prepend . $n; | ||
| 622 | } | ||
| 623 | |||
| 624 | sub command_name_brief { | ||
| 625 | my $self = shift; | ||
| 626 | my $class = ref($self) || $self; | ||
| 627 | my @words = grep { $_ ne 'Command' } split(/::/,$class); | ||
| 628 | my $n = join(' ', map { $self->_command_name_for_class_word($_) } $words[-1]); | ||
| 629 | return $n; | ||
| 630 | } | ||
| 631 | |||
| 632 | sub color_command_name { | ||
| 633 | my $text = shift; | ||
| 634 | |||
| 635 | my $colored_text = []; | ||
| 636 | |||
| 637 | my @COLOR_TEMPLATES = ('red', 'bold red', 'magenta', 'bold magenta'); | ||
| 638 | my @parts = split(/\s+/, $text); | ||
| 639 | for(my $i = 0 ; $i < @parts ; $i++ ){ | ||
| 640 | push @$colored_text, ($i < @COLOR_TEMPLATES) ? Term::ANSIColor::colored($parts[$i], $COLOR_TEMPLATES[$i]) : $parts[$i]; | ||
| 641 | } | ||
| 642 | |||
| 643 | return join(' ', @$colored_text); | ||
| 644 | } | ||
| 645 | |||
| 646 | sub _base_command_class_and_extension { | ||
| 647 | my $self = shift; | ||
| 648 | my $class = ref($self) || $self; | ||
| 649 | return ($class =~ /^(.*)::([^\:]+)$/); | ||
| 650 | } | ||
| 651 | |||
| 652 | sub _command_name_for_class_word { | ||
| 653 | my $self = shift; | ||
| 654 | my $s = shift; | ||
| 655 | $s =~ s/_/-/g; | ||
| 656 | $s =~ s/^([A-Z])/\L$1/; # ignore first capital because that is assumed | ||
| 657 | $s =~ s/([A-Z])/-$1/g; # all other capitals prepend a dash | ||
| 658 | $s =~ s/([a-zA-Z])([0-9])/$1$2/g; # treat number as begining word | ||
| 659 | $s = lc($s); | ||
| 660 | return $s; | ||
| 661 | } | ||
| 662 | |||
| 663 | sub _pod2txt { | ||
| 664 | my $txt = shift; | ||
| 665 | my $output = ''; | ||
| 666 | my $parser = Pod::Simple::Text->new; | ||
| 667 | $parser->no_errata_section(1); | ||
| 668 | $parser->output_string($output); | ||
| 669 | $parser->parse_string_document("=pod\n\n$txt"); | ||
| 670 | return $output; | ||
| 671 | } | ||
| 672 | |||
| 673 | sub _additional_help_sections { | ||
| 674 | return; | ||
| 675 | } | ||
| 676 | |||
| 677 | 1; |