Collection of mostly command line tools / PHP scripts. Somewhat out of date.

⌈⌋ ⎇ branch:  scripts + snippets


Check-in [e4e8e5030f]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Use array_shift for multiple positional - - - stdin/json placeholders. Don't populate $args from optional args.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e4e8e5030f4675db5e3c59c2135ae5cf8be295d7
User & Date: mario 2014-12-12 20:10:35
Context
2014-12-13
17:36
Retrieve count of backlinks to URL from various web services (Facebook, Google+, Twitter, Reddit, StumbleUpon, LinkedIn, Delicious, Pinterest) <em>Shallow merge of previous <code>php-scripts.fossil</code> repo.</em> check-in: c49ea11126 user: mario tags: trunk
2014-12-12
20:10
Use array_shift for multiple positional - - - stdin/json placeholders. Don't populate $args from optional args. check-in: e4e8e5030f user: mario tags: trunk
19:41
Reworked wrapper script to allow for piping and `-` placeholder parameters, and array functions be called (implicit JSON input/output). check-in: cb3d40f15b user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to php/pipefunc.php.

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87
88
89
90
91
92
       if (preg_match("/^ \s* [\[\{] .+ [\]\}] \s* $/smix", $stdin)) {
           $stdin = json_decode($stdin, TRUE);
       }

       // Allot array values to multiple params
       if (is_array($stdin) and ($dashes >= 2)) {
           while (count($stdin)) {
               $args[array_search("-", $args, TRUE)] = array_pop($stdin);
           }
       }
       // Substitute `-` args
       elseif ($dashes == 1) {
           $args[array_search("-", $args, TRUE)] = $stdin;
       }
       // Just use as first param
       else {
           $args = array_merge([$stdin], $args);
       }
   }
#print_r($args);


   #-- Run target function, capture result value
   list($fref, $i_result) = reflect_args($func, $args);
#$func =$fref;
   if (is_int($i_result)) {
       $_ = $fref->invokeArgs($args);

       $r = $args[$i_result];
   }
   else {
       $r = call_user_func_array($func, $args);
#var_dump($r);
   }

   #-- Output result
   if (in_array($func, str_getcsv("print_r,print,var_dump")) && !is_string($r)) {
       // as errno
       exit(intval($r));
   }
   else {







|

















<
|
>
|

<
<
|
<







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

78
79
80
81


82

83
84
85
86
87
88
89
       if (preg_match("/^ \s* [\[\{] .+ [\]\}] \s* $/smix", $stdin)) {
           $stdin = json_decode($stdin, TRUE);
       }

       // Allot array values to multiple params
       if (is_array($stdin) and ($dashes >= 2)) {
           while (count($stdin)) {
               $args[array_search("-", $args, TRUE)] = array_shift($stdin);
           }
       }
       // Substitute `-` args
       elseif ($dashes == 1) {
           $args[array_search("-", $args, TRUE)] = $stdin;
       }
       // Just use as first param
       else {
           $args = array_merge([$stdin], $args);
       }
   }
#print_r($args);


   #-- Run target function, capture result value
   list($fref, $i_result) = reflect_args($func, $args);
#$func =$fref;

   $r = $fref->invokeArgs($args);
   if (is_int($i_result) && is_scalar($r) && !is_string($r)) {
      $r = $args[$i_result];
   }


var_dump($func, $args, $r);


   #-- Output result
   if (in_array($func, str_getcsv("print_r,print,var_dump")) && !is_string($r)) {
       // as errno
       exit(intval($r));
   }
   else {
102
103
104
105
106
107
108
109
110
111

112
113
114
115
116

117
118
119
120
121
   $errname = array_search($errno, get_defined_constants());
   global $func;
   fwrite(STDERR, "\x1b[31m$errname \x1b[0m(\x1b[32m$func\x1b[0m): \x1b[33m$errstr\x1b[0m\n");
}


// Apply & references, if one of the function parameters expects one, keep that as result parameter index
function reflect_args($func, & $args) {
   $fref = new ReflectionFunction($func);
   foreach ($fref->getParameters() as $i => $param) {

       $args[$i] = & $args[$i];
       if ($param->isPassedByReference()) {
           return [$fref, $i];
       }
   }

   return [$fref, false];
}


?>







|


>
|
|
|
|
|
>
|




99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
   $errname = array_search($errno, get_defined_constants());
   global $func;
   fwrite(STDERR, "\x1b[31m$errname \x1b[0m(\x1b[32m$func\x1b[0m): \x1b[33m$errstr\x1b[0m\n");
}


// Apply & references, if one of the function parameters expects one, keep that as result parameter index
function reflect_args($func, & $args, $i_result=NULL) {
   $fref = new ReflectionFunction($func);
   foreach ($fref->getParameters() as $i => $param) {
       if ($i < count($args)) {
           $args[$i] = & $args[$i];
           if ($param->isPassedByReference()) {
               $i_result = $i;
           }
       }
   }
   return [$fref, $i_result];
}


?>