Config-Cmd

 view release on metacpan or  search on metacpan

lib/Config/Cmd.pm  view on Meta::CPAN

package Config::Cmd;
use Mo qw(default);
use YAML qw'DumpFile LoadFile';
use Modern::Perl;
use Carp;

# ABSTRACT: Command line to config file two way interface
our $VERSION = '0.002'; # VERSION

use constant EXT => '_conf.yaml';

has section => ();
has filename => ();
has quote => ( default => sub { q(') } );

sub set {
    my $self = shift;
    my $args = shift;

    say STDERR "# writing into ". $self->_set_file;
    say STDERR "# options: ". "@$args";

    $self->set_silent($args);
}

sub set_silent {
    my $self = shift;
    my $args = shift;

    # process arguments
    my $section = $self->section;
    croak "Set section before writing into a file" unless $section;
    my $config->{$section} = [];

    my $key = '';
    while (my $e = shift @$args) {
	if ($e eq '=') {
	    next;
	}
	elsif ($e =~ /(-.+)=(.+)/) {
	    if ($key) {
		my $tuple->{key} = $key ;
		push @{$config->{$section}}, $tuple;
		$key = '';
	    }
	    my $tuple->{key} = $1 ;
	    $tuple->{value} = $2 ;
	    push @{$config->{$section}}, $tuple;
	}
	elsif ($e =~ /^-/) {
	    if ($key) {
		my $tuple->{key} = $key ;
		push @{$config->{$section}}, $tuple;
	    }
	    $key = $e;
	} else {
	    if ($key) {
		my $tuple->{key} = $key ;
		$tuple->{value} = $e ;
		push @{$config->{$section}}, $tuple;
	    }
	    $key = '';
	}



( run in 1.426 second using v1.01-cache-2.11-cpan-5837b0d9d2c )