Skip to content

Commit 5f1c200

Browse files
committed
added dump
fixed flat() added exception debugging to testee
1 parent f18d062 commit 5f1c200

2 files changed

Lines changed: 90 additions & 52 deletions

File tree

src/docopt.php

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,6 @@ function any($iterable)
3434
return false;
3535
}
3636

37-
/**
38-
* This function returns a list of tuples, where the i-th tuple contains the i-th
39-
* element from each of the argument sequences or iterables. The returned list is
40-
* truncated in length to the length of the shortest argument sequence.
41-
*/
42-
function zip()
43-
{
44-
$iterables = func_get_args();
45-
if (!$iterables)
46-
return array();
47-
48-
$len = min(array_map(function($v) { return count($v); }, $iterables));
49-
$iterCnt = count($iterables);
50-
51-
$zipped = array();
52-
for ($i=0; $i<$len; $i++) {
53-
$cur = array();
54-
for ($j=0; $j<$iterCnt; $j++)
55-
$cur[] = $iterables[$j][$i];
56-
$zipped[] = $cur;
57-
}
58-
return $zipped;
59-
}
60-
6137
/**
6238
* The PHP version of this function doesn't work properly if the values aren't scalar.
6339
*/
@@ -115,6 +91,40 @@ function ends_with($str, $test)
11591
return substr_compare($str, $test, -$len, $len) === 0;
11692
}
11793

94+
function get_class_name($obj)
95+
{
96+
$cls = get_class($obj);
97+
return substr($cls, strpos($cls, '\\')+1);
98+
}
99+
100+
function dump($val)
101+
{
102+
if (is_array($val) || $val instanceof \Traversable) {
103+
echo '[';
104+
$cur = array();
105+
foreach ($val as $i)
106+
$cur[] = $i->dump();
107+
echo implode(', ', $cur);
108+
echo ']';
109+
}
110+
else
111+
echo $val->dump();
112+
}
113+
114+
function dump_scalar($scalar)
115+
{
116+
if ($scalar === null)
117+
return 'None';
118+
elseif ($scalar === false)
119+
return 'False';
120+
elseif ($scalar === true)
121+
return 'True';
122+
elseif (is_int($scalar) || is_float($scalar))
123+
return $scalar;
124+
else
125+
return "'$scalar'";
126+
}
127+
118128
/**
119129
* Error in construction of usage-message by developer
120130
*/
@@ -165,13 +175,13 @@ public function fixIdentities($uniq=null)
165175
{
166176
if (!isset($this->children) || !$this->children)
167177
return $this;
168-
178+
169179
if (!$uniq) {
170180
$uniq = array_unique($this->flat());
171181
}
172-
182+
173183
foreach ($this->children as $i=>$c) {
174-
if (!isset($c->children) || !$c->children) {
184+
if (!$c instanceof ParentPattern) {
175185
if (!in_array($c, $uniq)) {
176186
// Not sure if this is a true substitute for 'assert c in uniq'
177187
throw new \UnexpectedValueException();
@@ -383,20 +393,25 @@ public function __construct($children=null)
383393
public function flat($types=array())
384394
{
385395
$types = is_array($types) ? $types : array($types);
386-
387-
if (!$this->children) {
396+
if (in_array(get_class_name($this), $types))
388397
return array($this);
398+
399+
$flat = array();
400+
foreach ($this->children as $c) {
401+
$flat = array_merge($flat, $c->flat($types));
389402
}
390-
else {
391-
if (in_array('ParentPattern', $types))
392-
return array($this);
403+
return $flat;
404+
}
393405

394-
$flat = array();
395-
foreach ($this->children as $c) {
396-
$flat = array_merge($flat, $c->flat($types));
397-
}
398-
return $flat;
406+
public function dump()
407+
{
408+
$out = get_class_name($this).'(';
409+
$cd = array();
410+
foreach ($this->children as $c) {
411+
$cd[] = $c->dump();
399412
}
413+
$out .= implode(', ', $cd).')';
414+
return $out;
400415
}
401416
}
402417

@@ -436,6 +451,11 @@ public static function parse($source)
436451

437452
return new static($name, $value);
438453
}
454+
455+
public function dump()
456+
{
457+
return "Argument('".dump_scalar($this->name)."', ".dump_scalar($this->value)."')";
458+
}
439459
}
440460

441461
class Command extends Argument
@@ -510,7 +530,7 @@ public static function parse($optionDescription)
510530
$value = $match[1];
511531
}
512532
}
513-
533+
514534
return new static($short, $long, $argcount, $value);
515535
}
516536

@@ -528,6 +548,11 @@ public function name()
528548
{
529549
return $this->long ?: $this->short;
530550
}
551+
552+
public function dump()
553+
{
554+
return "Option('{$this->short}', ".dump_scalar($this->long).", ".dump_scalar($this->argcount).", ".dump_scalar($this->value).")";
555+
}
531556
}
532557

533558
class Required extends ParentPattern
@@ -1021,6 +1046,7 @@ function handle($doc, $argv=null)
10211046

10221047
ExitException::$usage = printable_usage($doc);
10231048
$options = parse_defaults($doc);
1049+
10241050
$formalUse = formal_usage(ExitException::$usage);
10251051
$pattern = parse_pattern($formalUse, $options);
10261052
$argv = parse_argv(new TokenStream($argv, 'ExitException'), $options, $this->optionsFirst);
@@ -1030,7 +1056,7 @@ function handle($doc, $argv=null)
10301056
}
10311057

10321058
extras($this->help, $this->version, $argv, $doc);
1033-
1059+
10341060
list($matched, $left, $collected) = $pattern->fix()->match($argv);
10351061
if ($matched && !$left) {
10361062
$return = array();
@@ -1079,22 +1105,22 @@ public function __get($name)
10791105
throw new \BadMethodCallException("Unknown property $name");
10801106
}
10811107

1082-
public function offsetExists ($offset)
1108+
public function offsetExists($offset)
10831109
{
10841110
return isset($this->args[$offset]);
10851111
}
10861112

1087-
public function offsetGet ($offset)
1113+
public function offsetGet($offset)
10881114
{
10891115
return $this->args[$offset];
10901116
}
10911117

1092-
public function offsetSet ($offset, $value)
1118+
public function offsetSet($offset, $value)
10931119
{
10941120
$this->args[$offset] = $value;
10951121
}
10961122

1097-
public function offsetUnset ($offset)
1123+
public function offsetUnset($offset)
10981124
{
10991125
unset($this->args[$offset]);
11001126
}

test/testee.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@
88
$in .= fread(STDIN, 1024);
99
}
1010

11+
$ex = null;
12+
$result = null;
1113
ob_start();
12-
$result = Docopt\docopt($in, array('exit'=>false));
14+
try {
15+
$result = Docopt\docopt($in, array('exit'=>false));
16+
}
17+
catch (Exception $ex) {
18+
}
1319
$out = ob_get_clean();
1420

15-
if (getenv('DOCOPT_DEBUG'))
16-
echo $out;
21+
if (getenv('DOCOPT_DEBUG')) {
22+
if ($out)
23+
echo "Output:\n".rtrim($out)."\n\n";
24+
if ($ex)
25+
echo "Exception:\n".$ex;
26+
}
1727

18-
if (!$result->success)
19-
print '"user-error"';
20-
elseif (empty($result->args))
21-
echo '{}';
22-
else
23-
echo json_encode($result->args);
28+
if ($result) {
29+
if (!$result->success)
30+
echo '"user-error"';
31+
elseif (empty($result->args))
32+
echo '{}';
33+
else
34+
echo json_encode($result->args);
35+
}

0 commit comments

Comments
 (0)