App-Table2YAML

 view release on metacpan or  search on metacpan

lib/App/Table2YAML/CLI.pm  view on Meta::CPAN

    }

    return @{ $self->errors() } ? 0 : 1;
} ## end sub parse_opts

sub _parse_opts_asciitable {
    my $self = shift;
    my %opt = splice @_;

    foreach my $opt ( keys %opt ) {
        next if $opt eq q(record_separator);
        my $msg = qq(asciitable and '--$opt' are incompatible.\nIgnored.);
        say $msg;
        delete $opt{$opt};
    }

    return %opt;
}

sub _parse_opts_dsv {
    my $self = shift;
    my %opt  = splice @_;

    foreach (q(field_separator)) {
        if ( exists $opt{$_} ) {
            my $value = delete $opt{$_};
            $value = qq(\t) if $value eq q(\t);
            if ( length($value) == 1 ) {
                $self->opts->{$_} = $value;
            }
            else {
                my $msg = qq('--$_' need be only one character);
                push @{ $self->errors() }, $msg;
            }
        }
        else {
            my $msg = qq('--$_' is mandatory for '--input_type=dsv');
            push @{ $self->errors() }, $msg;
        }
    } ## end foreach (q(field_separator))

    foreach (q(record_separator)) {
        last unless exists $opt{$_};
        my $value = delete $opt{$_};
        $value = $self->_parse_record_separator($value);
        $self->opts->{$_} = $value if defined $value;
    }

    return %opt;
} ## end sub _parse_opts_dsv

sub _parse_opts_fixedwidth {
    my $self = shift;
    my %opt  = splice @_;

    foreach (q(field_offset)) {
        if ( exists $opt{$_} ) {
            my $values = delete $opt{$_};
            my @value;
            foreach my $value ( @{$values} ) {
                push @value, split m{[\s,]+}msx, $value;
            }
            @value = grep { defined && $_ ne q() } @value;
            $self->opts->{$_} = [@value];
        }
        else {
            my $msg = qq('--$_' is mandatory for '--input_type=fixedwidth');
            push @{ $self->errors() }, $msg;
        }
    } ## end foreach (q(field_offset))

    foreach (q(record_separator)) {
        last unless exists $opt{$_};
        my $value = delete $opt{$_};
        $value = $self->_parse_record_separator($value);
        $self->opts->{$_} = $value if defined $value;
    }

    return %opt;
} ## end sub _parse_opts_fixedwidth

sub _parse_opts_html    {...}
sub _parse_opts_latex   {...}
sub _parse_opts_texinfo {...}

sub _parse_record_separator {
    my $self             = shift;
    my $record_separator = shift;

    return unless defined $record_separator;

    my %map = (
        q(\n)    => qq(\n),
        q(\r)    => qq(\r),
        q(\r\n)  => qq(\r\n),
        qq(\n)   => qq(\n),
        qq(\r)   => qq(\r),
        qq(\r\n) => qq(\r\n),
    );

    $record_separator
        = exists $map{$record_separator} ? $map{$record_separator} : ();

    return $record_separator;
} ## end sub _parse_record_separator

sub table2yaml {
    my $self = shift;

    my $table2yaml = App::Table2YAML->new( $self->opts() );
    my @yaml       = $table2yaml->convert();

    return @yaml;
}

no Moo;
__PACKAGE__->meta->make_immutable;

1;

__END__



( run in 1.172 second using v1.01-cache-2.11-cpan-39bf76dae61 )