| 1 | package App::PermuteNamed; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 4 | 1 | our $DATE = '2019-11-29'; # DATE | |||||||||
| 5 | 1 | our $DIST = 'App-PermuteNamed'; # DIST | |||||||||
| 6 | 1 | our $VERSION = '0.004'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | our %SPEC; | |||||||||
| 13 | |||||||||||
| 14 | $SPEC{permute_named} = { | ||||||||||
| 15 | 1 | v => 1.1, | |||||||||
| 16 | summary => 'Permute multiple-valued key-value pairs', | ||||||||||
| 17 | description => <<'_', | ||||||||||
| 18 | |||||||||||
| 19 | This is a CLI for `Permute::Named::*` module (currently using | ||||||||||
| 20 | `Permute::Named::Iter`). | ||||||||||
| 21 | |||||||||||
| 22 | To enter a pair with multiple values, you enter a comma-separated list with the | ||||||||||
| 23 | first element is the key name and the rest are values. | ||||||||||
| 24 | |||||||||||
| 25 | The return will be array of hashes. | ||||||||||
| 26 | |||||||||||
| 27 | _ | ||||||||||
| 28 | args => { | ||||||||||
| 29 | aoaos => { | ||||||||||
| 30 | 'x.name.is_plural' => 1, | ||||||||||
| 31 | 'x.name.singular' => 'aos', | ||||||||||
| 32 | schema => ['array*', { | ||||||||||
| 33 | min_len => 2, | ||||||||||
| 34 | of => ['array*', { | ||||||||||
| 35 | min_len => 1, | ||||||||||
| 36 | of => 'str*', | ||||||||||
| 37 | 'x.perl.coerce_rules' => ['From_str::comma_sep'] | ||||||||||
| 38 | }], | ||||||||||
| 39 | }], | ||||||||||
| 40 | req => 1, | ||||||||||
| 41 | pos => 0, | ||||||||||
| 42 | greedy => 1, | ||||||||||
| 43 | }, | ||||||||||
| 44 | separator => { | ||||||||||
| 45 | summary => 'Separator character to use', | ||||||||||
| 46 | schema => 'str*', | ||||||||||
| 47 | cmdline_aliases => {s => {}}, | ||||||||||
| 48 | }, | ||||||||||
| 49 | }, | ||||||||||
| 50 | examples => [ | ||||||||||
| 51 | { | ||||||||||
| 52 | argv => ['bool,0,1','x,foo,bar,baz'], | ||||||||||
| 53 | }, | ||||||||||
| 54 | { | ||||||||||
| 55 | src => '[[prog]] bool,0,1 x,foo,bar,baz --format json-pretty --naked-res', | ||||||||||
| 56 | src_plang => 'bash', | ||||||||||
| 57 | summary => 'Like previous example, but outputs JSON', | ||||||||||
| 58 | }, | ||||||||||
| 59 | ], | ||||||||||
| 60 | }; | ||||||||||
| 61 | 0 | ▶ | sub permute_named { | ||||||||
|
Subroutine
permute_named: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 62 | 0 | require Permute::Named::Iter; | |||||||||
| 63 | |||||||||||
| 64 | 0 | my %args = @_; | |||||||||
| 65 | 0 | my $sep = $args{separator}; | |||||||||
| 66 | |||||||||||
| 67 | 0 | my @fields; | |||||||||
| 68 | my @permute; | ||||||||||
| 69 | 0 | for my $aos (@{$args{aoaos}}) { | |||||||||
| 70 | 0 | my $k = shift @$aos; | |||||||||
| 71 | 0 | push @permute, $k, $aos; | |||||||||
| 72 | 0 | push @fields, $k | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 0 | my $resmeta = {}; | |||||||||
| 76 | 0 | my $iter = Permute::Named::Iter::permute_named_iter(@permute); | |||||||||
| 77 | 0 | my @res; | |||||||||
| 78 | 0 | ▶ | while (my $h = $iter->()) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | ▶ | if (defined $sep) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | push @res, join($sep, @{$h}{@fields}); | |||||||||
| 81 | } else { | ||||||||||
| 82 | 0 | push @res, $h; | |||||||||
| 83 | } | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | unless (defined $sep) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | $resmeta->{'table.fields'} = \@fields; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | [200, "OK", \@res, $resmeta]; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 1 | 1; | |||||||||
| 94 | # ABSTRACT: Permute multiple-valued key-value pairs | ||||||||||
| 95 | |||||||||||
| 96 | __END__ | ||||||||||