From 1792cc6619d1c112b1f4bf2f93191a5695e4cd9c Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 14 Jul 2016 02:18:11 +0100 Subject: [PATCH] bin/ini2tsv --- bin/ini2tsv | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100755 bin/ini2tsv diff --git a/bin/ini2tsv b/bin/ini2tsv new file mode 100755 index 00000000..c6fcce6e --- /dev/null +++ b/bin/ini2tsv @@ -0,0 +1,206 @@ +#!/usr/bin/env perl +use strict; +use warnings; +################################################################################# +# +# VERSION +# +################################################################################# +my $VERSION = '0.0.1'; +# NB 14.07.16 +# - create script: ini2tsv + +################################################################################# +# +# GLOBALS +# +################################################################################# +my ($NAME) = $0 =~ m,([^/]+)$,; + +################################################################################# +# +# ARGS +# +################################################################################# +my $VERBOSE = $main::VERBOSE = 1; +my $DEBUG = $main::DEBUG = 0; + +my %Opt = ( +); +get_options(\%Opt); +help() unless @ARGV; +$main::_DATA_ = undef; + +################################################################################# +# +# BEGIN +# +################################################################################# +my $section; +while (<>) { + next unless /^\s*(\w|\s*\[)/; + chomp; + + if (/^\s*\[([^\]]+)\].*/) { + $section = $1; + #$section = join('.',split(/[^\w ]+/,$section)); + $section =~ s/ (\w)/vvvSPACEvvv$1/g; + $section = join('.',split(/\W+/,$section)); + $section =~ s/vvvSPACEvvv/ /g; + } + + if (/^\s*([^= ]+)\s*=\s*(.*?)\s*(#.*)?$/) { + my $keyword = $1; + my $value = $2 ; + #$keyword="<$keyword>"; + print join("\t",$section,$keyword,$value)."\n"; + next; + } + +} + +################################################################################# +# +# END +# +################################################################################# +exit 0; + +################################################################################# +# +# Functions +# +################################################################################# +sub help { +#------------------------------------------------------------------------------ +# Print help and exit +#------------------------------------------------------------------------------ + + require 'Pod/Usage.pm' unless $INC{'Pod/Usage.pm'}; + require 'Pod/Perldoc.pm' unless $INC{'Pod/Perldoc.pm'}; + + # Substitutions + sub pod_env { + my $v = ''; + eval '$v = ref(\\'.$_[0].') eq "ARRAY" ? join(" ",'.$_[0].') : '.$_[0].'; return defined $v ? $v : qq|UNDEF|;'; + return $v; + } + + $main::_DATA_ =~ s/([@\$][A-Z_a-z\{\}]+)/pod_env($1)/eg; + + # Create tmp + my $in_file = (-e '/dev/shm' ? '/dev/shm' : '/tmp')."/$NAME.$$"; + my $in; + open($in,">$in_file") or die "$NAME: Can't write into $in_file: $!"; + print $in $main::_DATA_; + close $in; + + # Output + open(STDOUT,"|perl -pe 's/\.$$//g'".(($ENV{PAGER}||'') eq 'less' ? "|less -FRi" : "")); + my $opts = { + -input => $in_file, + -ouput => \*STDOUT, + -exitval => 'noexit', + -sections => [qw(SYNOPSIS DESCRIPTION OPTIONS)], + -verbose => ($Opt{'help'} ? 99 : 3), + }; + + Pod::Usage::pod2usage($opts); + close STDOUT; + unlink $in_file if $in_file and -e $in_file; + + exit 0; +} + +#------------------------------------------------------------------------------ +# Print version and exit +#------------------------------------------------------------------------------ +sub version { print "$NAME: version [$VERSION]\n"; exit 0; } + +#------------------------------------------------------------------------------ +# Get options from pod +#------------------------------------------------------------------------------ +sub get_options { + + use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); + + my @Opt; + + sub pod_opt { + local $_; + my $o = shift; + $o =~ s/(=.|[\+\-\!]$)//; + $o = join(", ",map{"-$_"} split(/[\|,:;]/,$o)); + return "$o"; + } + + while () { + s/option\[([^\]]+)\]/push(@Opt,$1) and pod_opt($1)/eg; + $main::_DATA_ .= $_; + } + + GetOptions($_[0],@Opt) || exit -1; + + help() if $_[0]{'help'} or $_[0]{'man'}; + version() if $_[0]{'version'}; + + $main::VERBOSE = $VERBOSE = $_[0]{'verbose'} if defined $_[0]{'verbose'}; + $main::DEBUG = $DEBUG = $_[0]{'debug'} if defined $_[0]{'debug'}; + +} + +__DATA__ + +=head1 NAME + +$NAME - Transform a init file into a csv + +=head1 SYNOPSIS + +Quick usage: + +=over + +=item $NAME --verbose + +=item $NAME --help + +=back + +=head1 DESCRIPTION + +$NAME test.ini > test.csv + +=head1 OPTIONS + + -option[verbose|v+] Verbose mode: increase the verbosity level. + -option[debug+] Debug mode: increase the verbosity level. + -option[version|V] Print version (default: $VERSION) + -option[help|h|?] Print a brief help message and exits. + -option[man] Print the manual page and exits. + +=cut + +=head1 REQUIRES + +Getopt::Std, Pod::Usage + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2016 Nicolas Boisselier + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +See . + +=head1 SEE ALSO + +perl(1), http://perldoc.perl.org/perlpodstyle.html + +=head1 AUTHOR + +Nicolas Boisselier nicolas.boisselier@gmail.com + +=cut -- 2.47.3