| 1 | package Getopt::Long::Descriptive::Dump; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | our $DATE = '2016-10-27'; # DATE | |||||||||||
| 4 | 1 | our $VERSION = '0.001'; # VERSION | |||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use Exporter qw(import); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | our @EXPORT_OK = qw(dump_getopt_long_descriptive_script); | |||||||||||
| 12 | |||||||||||||
| 13 | 1 | our %SPEC; | |||||||||||
| 14 | |||||||||||||
| 15 | $SPEC{dump_getopt_long_descriptive_script} = { | ||||||||||||
| 16 | 1 | v => 1.1, | |||||||||||
| 17 | summary => 'Run a Getopt::Long::Descriptive-based script but only to '. | ||||||||||||
| 18 | 'dump the spec', | ||||||||||||
| 19 | description => <<'_', | ||||||||||||
| 20 | |||||||||||||
| 21 | This function runs a CLI script that uses `Getopt::Long::Descriptive` but | ||||||||||||
| 22 | monkey-patches beforehand so that `describe_options()` will dump the object and | ||||||||||||
| 23 | then exit. The goal is to get the object without actually running the script. | ||||||||||||
| 24 | |||||||||||||
| 25 | This can be used to gather information about the script and then generate | ||||||||||||
| 26 | documentation about it or do other things (e.g. `App::shcompgen` to generate a | ||||||||||||
| 27 | completion script for the original script). | ||||||||||||
| 28 | |||||||||||||
| 29 | CLI script needs to use `Getopt::Long::Descriptive`. This is detected currently | ||||||||||||
| 30 | by a simple regex. If script is not detected as using | ||||||||||||
| 31 | `Getopt::Long::Descriptive`, status 412 is returned. | ||||||||||||
| 32 | |||||||||||||
| 33 | _ | ||||||||||||
| 34 | args => { | ||||||||||||
| 35 | filename => { | ||||||||||||
| 36 | summary => 'Path to the script', | ||||||||||||
| 37 | req => 1, | ||||||||||||
| 38 | pos => 0, | ||||||||||||
| 39 | schema => 'str*', | ||||||||||||
| 40 | cmdline_aliases => {f=>{}}, | ||||||||||||
| 41 | }, | ||||||||||||
| 42 | libs => { | ||||||||||||
| 43 | 'x.name.is_plural' => 1, | ||||||||||||
| 44 | 'x.name.singular' => 'lib', | ||||||||||||
| 45 | summary => 'Libraries to unshift to @INC when running script', | ||||||||||||
| 46 | schema => ['array*' => of => 'str*'], | ||||||||||||
| 47 | cmdline_aliases => {I=>{}}, | ||||||||||||
| 48 | }, | ||||||||||||
| 49 | skip_detect => { | ||||||||||||
| 50 | schema => ['bool', is=>1], | ||||||||||||
| 51 | cmdline_aliases => {D=>{}}, | ||||||||||||
| 52 | }, | ||||||||||||
| 53 | }, | ||||||||||||
| 54 | }; | ||||||||||||
| 55 | 0 | ▶ | sub dump_getopt_long_descriptive_script { | ||||||||||
|
Subroutine
dump_getopt_long_descriptive_script: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||
| 56 | 0 | require Capture::Tiny; | |||||||||||
| 57 | 0 | require Getopt::Long::Descriptive::Util; | |||||||||||
| 58 | 0 | require UUID::Random; | |||||||||||
| 59 | |||||||||||||
| 60 | 0 | my %args = @_; | |||||||||||
| 61 | |||||||||||||
| 62 | 0 | ▶ | my $filename = $args{filename} or return [400, "Please specify filename"]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 0 | my $detres; | |||||||||||
| 64 | 0 | ▶ | if ($args{skip_detect}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 0 | $detres = [200, "OK (skip_detect)", 1, {"func.module"=>"Getopt::Long::Descriptive", "func.reason"=>"skip detect, forced"}]; | |||||||||||
| 66 | } else { | ||||||||||||
| 67 | 0 | $detres = Getopt::Long::Descriptive::Util::detect_getopt_long_descriptive_script( | |||||||||||
| 68 | filename => $filename); | ||||||||||||
| 69 | 0 | ▶ | return $detres if $detres->[0] != 200; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | return [412, "File '$filename' is not script using Getopt::Long::Descriptive (". | ||||||||||||
| 71 | 0 | ▶ | $detres->[3]{'func.reason'}.")"] unless $detres->[2]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 0 | ▶ | my $libs = $args{libs} // []; | ||||||||||
|
Condition0%0/2
$args{"libs"} // []
MC/DC0%0/1
$args{"libs"} // []
$args{"libs"}
Truth table0%0/2
A
A= $args{"libs"}
| |||||||||||||
| 75 | |||||||||||||
| 76 | 0 | my $tag = UUID::Random::generate(); | |||||||||||
| 77 | my @cmd = ( | ||||||||||||
| 78 | 0 | $^X, (map {"-I$_"} @$libs), | |||||||||||
| 79 | "-MGetopt::Long::Descriptive::Patch::DumpAndExit=-tag,$tag", | ||||||||||||
| 80 | $filename, | ||||||||||||
| 81 | "--version", | ||||||||||||
| 82 | ); | ||||||||||||
| 83 | my ($stdout, $stderr, $exit) = Capture::Tiny::capture( | ||||||||||||
| 84 | 0 | ▶ | sub { local $ENV{GETOPT_LONG_DESCRIPTIVE_DUMP} = 1; system @cmd }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 85 | 0 | ); | |||||||||||
| 86 | |||||||||||||
| 87 | 0 | my $spec; | |||||||||||
| 88 | 0 | ▶ | if ($stdout =~ /^# BEGIN DUMP $tag\s+(.*)^# END DUMP $tag/ms) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 0 | $spec = eval $1; | |||||||||||
| 90 | 0 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 0 | return [500, "Script '$filename' looks like using ". | |||||||||||
| 92 | "Getopt::Long::Descriptive, but I got an error in eval-ing ". | ||||||||||||
| 93 | "captured option spec: $@, raw capture: <<<$1>>>"]; | ||||||||||||
| 94 | } | ||||||||||||
| 95 | 0 | ▶ | if (ref($spec) ne 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 0 | return [500, "Script '$filename' looks like using ". | |||||||||||
| 97 | "Getopt::Long::Descriptive, but I didn't get an array spec, ". | ||||||||||||
| 98 | "raw capture: stdout=<<$stdout>>"]; | ||||||||||||
| 99 | } | ||||||||||||
| 100 | } else { | ||||||||||||
| 101 | 0 | return [500, "Script '$filename' looks like using Getopt::Long::Descriptive, ". | |||||||||||
| 102 | "but I couldn't capture option spec, raw capture: ". | ||||||||||||
| 103 | "stdout=<<$stdout>>, stderr=<<$stderr>>"]; | ||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 0 | [200, "OK", $spec, { | |||||||||||
| 107 | 'func.detect_res' => $detres, | ||||||||||||
| 108 | }]; | ||||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 1 | 1; | |||||||||||
| 112 | # ABSTRACT: Run a Getopt::Long::Descriptive-based script but only to dump the spec | ||||||||||||
| 113 | |||||||||||||
| 114 | __END__ | ||||||||||||