| 1 | package Config::Cmd; | ||||||||||||||||||
| 2 | 3 | ▶ | use Mo qw(default); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 3 | ▶ | use YAML qw'DumpFile LoadFile'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 3 | ▶ | use Modern::Perl; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 3 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | # ABSTRACT: Command line to config file two way interface | ||||||||||||||||||
| 8 | 3 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 3 | ▶ | use constant EXT => '_conf.yaml'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 3 | has section => (); | |||||||||||||||||
| 13 | 3 | has filename => (); | |||||||||||||||||
| 14 | 1 | ▶ | has quote => ( default => sub { q(') } ); | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 0 | ▶ | sub set { | ||||||||||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 17 | 0 | my $self = shift; | |||||||||||||||||
| 18 | 0 | my $args = shift; | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 0 | say STDERR "# writing into ". $self->_set_file; | |||||||||||||||||
| 21 | 0 | say STDERR "# options: ". "@$args"; | |||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 0 | $self->set_silent($args); | |||||||||||||||||
| 24 | } | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 10 | ▶ | sub set_silent { | ||||||||||||||||
|
Subroutine
set_silent: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||
| 27 | 10 | my $self = shift; | |||||||||||||||||
| 28 | 10 | my $args = shift; | |||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | # process arguments | ||||||||||||||||||
| 31 | 10 | my $section = $self->section; | |||||||||||||||||
| 32 | 10 | ▶ | croak "Set section before writing into a file" unless $section; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 33 | 9 | my $config->{$section} = []; | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 9 | my $key = ''; | |||||||||||||||||
| 36 | 9 | ▶ | while (my $e = shift @$args) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 37 | 37 | ▶ | if ($e eq '=') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 38 | 2 | next; | |||||||||||||||||
| 39 | } | ||||||||||||||||||
| 40 | elsif ($e =~ /(-.+)=(.+)/) { | ||||||||||||||||||
| 41 | 2 | ▶ | if ($key) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | 1 | my $tuple->{key} = $key ; | |||||||||||||||||
| 43 | 1 | push @{$config->{$section}}, $tuple; | |||||||||||||||||
| 44 | 1 | $key = ''; | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | 2 | my $tuple->{key} = $1 ; | |||||||||||||||||
| 47 | 2 | $tuple->{value} = $2 ; | |||||||||||||||||
| 48 | 2 | push @{$config->{$section}}, $tuple; | |||||||||||||||||
| 49 | } | ||||||||||||||||||
| 50 | elsif ($e =~ /^-/) { | ||||||||||||||||||
| 51 | 20 | ▶ | if ($key) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | 5 | my $tuple->{key} = $key ; | |||||||||||||||||
| 53 | 5 | push @{$config->{$section}}, $tuple; | |||||||||||||||||
| 54 | } | ||||||||||||||||||
| 55 | 20 | $key = $e; | |||||||||||||||||
| 56 | } else { | ||||||||||||||||||
| 57 | 13 | ▶ | if ($key) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 58 | 12 | my $tuple->{key} = $key ; | |||||||||||||||||
| 59 | 12 | $tuple->{value} = $e ; | |||||||||||||||||
| 60 | 12 | push @{$config->{$section}}, $tuple; | |||||||||||||||||
| 61 | } | ||||||||||||||||||
| 62 | 13 | $key = ''; | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | 9 | ▶ | if ($key) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 67 | 2 | my $tuple->{key} = $key; | |||||||||||||||||
| 68 | 2 | push @{$config->{$section}}, $tuple; | |||||||||||||||||
| 69 | } | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | 9 | DumpFile $self->_set_file, $config; | |||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 9 | ▶ | sub get { | ||||||||||||||||
|
Subroutine
get: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||
| 75 | 9 | my $self = shift; | |||||||||||||||||
| 76 | 9 | my $section = $self->section; | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 9 | my $config = LoadFile($self->_get_file); | |||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 9 | my @list; | |||||||||||||||||
| 81 | 9 | for my $tuple (@{$config->{$section}}) { | |||||||||||||||||
| 82 | 22 | ▶ | if (defined $tuple->{value}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 83 | 14 | my $value = $tuple->{value}; | |||||||||||||||||
| 84 | 14 | ▶ | $value = $self->quote. $value. $self->quote if $value =~ /\s/; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | 14 | push @list, $tuple->{key}, $value; | |||||||||||||||||
| 86 | } else { | ||||||||||||||||||
| 87 | 8 | push @list, $tuple->{key}; | |||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | 9 | return join ' ', @list; | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | # internal methods | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 24 | ▶ | sub _default_filename { | ||||||||||||||||
|
Subroutine
_default_filename: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 96 | 24 | return shift->section. EXT; | |||||||||||||||||
| 97 | } | ||||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | 9 | ▶ | sub _set_file { | ||||||||||||||||
|
Subroutine
_set_file: called CC=3 SCAR=11.4
| |||||||||||||||||||
| 100 | 9 | my $self = shift; | |||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 9 | ▶ | return $self->filename if defined $self->filename; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 103 | 8 | ▶ | return $self->_default_filename if $self->section; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 104 | 0 | return; # if no matching files were found | |||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 9 | ▶ | sub _get_file { | ||||||||||||||||
|
Subroutine
_get_file: called CC=4 SCAR=16.4
| |||||||||||||||||||
| 109 | 9 | my $self = shift; | |||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 9 | ▶ | return $self->filename if defined $self->filename; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 112 | 8 | ▶ | if (-e $self->_default_filename) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 113 | 8 | return $self->_default_filename; | |||||||||||||||||
| 114 | } elsif (-e $ENV{HOME}. '.'. $self->_default_filename) { #check home directory | ||||||||||||||||||
| 115 | 0 | return $ENV{HOME}. '.'. $self->_default_filename; | |||||||||||||||||
| 116 | } | ||||||||||||||||||
| 117 | 0 | return; # if no matching files were found | |||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 3 | 1; | |||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||