]> git.nbdom.net Git - nb.git/commitdiff
curl options array
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 1 Mar 2016 01:01:45 +0000 (01:01 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 1 Mar 2016 01:01:45 +0000 (01:01 +0000)
bin/dbq

diff --git a/bin/dbq b/bin/dbq
index dbc9886a64be2437a1bea6bd0eaa00169ed06927..01e0ebf40c53f4149319d462fef1de781b2a6c16 100755 (executable)
--- a/bin/dbq
+++ b/bin/dbq
@@ -2,6 +2,7 @@
 use strict;
 use warnings;
 use Encode;
+use NB::Functions qw/html2txt/;
 #################################################################################
 #
 # VERSION
@@ -31,22 +32,18 @@ my %CMD_ALIASE = (
   'l' => 'limit',
 );
 
-my @CURL_OPT = (
-  'dump-header|D=s',
-  'get|G!',
-  'compressed|z!',
+## Vim: r!% --curl_help_hash | grep X
+my @H;
+my %CURL_OPT = (
+  'compressed|z!' => 1, # Request compressed response (using deflate or gzip)
+  'dump-header|D=s' => undef, # FILE  Write the headers to FILE
+  'get|G!' => undef, # Send the -d data with a HTTP GET (H)
+  'silent|s!' => 1, # Silent mode (don't output anything)
+  'header|H=s' => \@H, # LINE   Pass custom header LINE to server (H)
+  #'header|H=s' => 1, #\@H, # LINE   Pass custom header LINE to server (H)
 );
-my $CURL_OPT_EXP = join('|',map{ s/\|.*$//; $_ } @CURL_OPT);
-# NB 29.02.16 add_argv('--compressed');
-# NB 29.02.16 sub add_argv {
-# NB 29.02.16   #my $search = shift;
-# NB 29.02.16   my $found = 0;
-# NB 29.02.16   foreach my $search (@_) {
-# NB 29.02.16     foreach (@ARGV) { $found == 1 and last if $_ eq $search; }
-# NB 29.02.16     #die @ARGV,$search unless $found;
-# NB 29.02.16     #push @ARGV,$search unless $found;
-# NB 29.02.16   }
-# NB 29.02.16 }
+my @CURL_OPT = keys %CURL_OPT;
+my $CURL_OPT_EXP = join('|',map{ /^(.*?)\|/ ? $1 : $_ } @CURL_OPT);
 
 #################################################################################
 #
@@ -61,18 +58,21 @@ curl_help() if @ARGV and $ARGV[0] eq '--curl_help' and shift @ARGV;
 parse_debug() if @ARGV and $ARGV[0] eq '--parse_debug' and shift @ARGV;
 curl_help_hash() if @ARGV and $ARGV[0] eq '--curl_help_hash' and shift @ARGV;
 
-my %Opt; get_options(\%Opt,\@CURL_OPT);
-#use Data::Dumper; die Dumper(\%Opt);
+my %Opt = map{ (/^(.*?)\|/ ? $1 : $_) => $CURL_OPT{$_} } keys %CURL_OPT;
 
-$Opt{compressed} = 1 if exists $Opt{compressed};
+get_options(\%Opt,\@CURL_OPT);
+
+for my $o (@CURL_OPT) {
+  $o =~ s/\|.*$//;
+  next unless $Opt{$o};
+  if (ref($Opt{$o}) eq 'ARRAY') {
+    push(@ARGV,map{ ("--$o",$_) } @{$Opt{$o}});
+
+  } elsif ($Opt{$o} ne '1') {
+    push @ARGV,"--$o",$Opt{$o};
 
-for (@CURL_OPT) {
-  s/\|.*$//;
-  next unless $Opt{$_};
-  if ($Opt{$_} ne '1') {
-    push @ARGV,"--$_",$Opt{$_}
   } else {
-    push @ARGV,"--$_";
+    push @ARGV,"--$o";
   }
 }
 
@@ -103,7 +103,7 @@ push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
 my %keys = ();
 while ($_ = shift @ARGV) {
 
-  if (/^([^=]+)=(.*)$/) {
+  if (/^([\w\._:-]+)=(.*)$/) {
     $keys{($CMD_ALIASE{$1} ? $CMD_ALIASE{$1} : $1)} = $2;
   } else {
     push(@cmd,$_);
@@ -132,7 +132,7 @@ if ($keys{format} eq 'nc') {
 } elsif ( $keys{format} eq 'jq' and $keys{format}='json' ) {
   open(STDOUT,"|$0 --parse_debug|jq .")
 
-} elsif ( $keys{debug} ) {
+} elsif ( $keys{debug} or $Opt{debug}) {
   open(STDOUT,"|$0 --parse_debug")
 
 }
@@ -163,7 +163,9 @@ sub parse_debug {
   while (<>) {
 
     if (/DEBUG: /) { print STDERR $_ }
-    elsif ( /^(<\w+>)?Fatal error/ .. /^\s+thrown in/) { s/<[\w\/ ]+>//g; print STDERR $_ }
+    elsif ( /^(<\w+>)?(Parse error|Warning)/) { print STDERR html2txt($_) }
+    elsif ( /^<pre class="debug">/) { print STDERR html_unescape($_) }
+    elsif ( /^(<\w+>)?Fatal error/ .. /^\s+thrown in/) { print STDERR html2txt($_) }
     else { print $_ }
 
   }