Skip to content

Commit 82dca31

Browse files
committed
Another attempt at API change for autoloaders
1 parent 1ba198b commit 82dca31

2 files changed

Lines changed: 62 additions & 82 deletions

File tree

src/docopt.php

Lines changed: 61 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,12 @@
1111

1212
namespace
1313
{
14-
use Docopt\ExitException;
15-
use Docopt\TokenStream;
16-
use Docopt\Response;
17-
18-
/**
19-
* Use a class in PHP because we can't autoload functions yet.
20-
*/
2114
class Docopt
2215
{
23-
public $exit = true;
24-
public $help = true;
25-
public $optionsFirst = false;
26-
public $version;
27-
2816
/**
2917
* API compatibility with python docopt
3018
*/
31-
static function docopt($doc, $params=array())
19+
static function handle($doc, $params=array())
3220
{
3321
$argv = null;
3422
if (isset($params['argv'])) {
@@ -40,81 +28,14 @@ static function docopt($doc, $params=array())
4028
$params = array();
4129
}
4230

43-
$h = new static($params);
31+
$h = new \Docopt\Handler($params);
4432
return $h->handle($doc, $argv);
4533
}
46-
47-
public function __construct($options=array())
48-
{
49-
foreach ($options as $k=>$v)
50-
$this->$k = $v;
51-
}
52-
53-
function handle($doc, $argv=null)
54-
{
55-
try {
56-
if ($argv === null && isset($_SERVER['argv']))
57-
$argv = array_slice($_SERVER['argv'], 1);
58-
59-
ExitException::$usage = Docopt\printable_usage($doc);
60-
$options = Docopt\parse_defaults($doc);
61-
62-
$formalUse = Docopt\formal_usage(ExitException::$usage);
63-
$pattern = Docopt\parse_pattern($formalUse, $options);
64-
$argv = Docopt\parse_argv(new TokenStream($argv, 'ExitException'), $options, $this->optionsFirst);
65-
foreach ($pattern->flat('AnyOptions') as $ao) {
66-
$docOptions = Docopt\parse_defaults($doc);
67-
$ao->children = array_diff((array)$docOptions, $pattern->flat('Option'));
68-
}
69-
70-
Docopt\extras($this->help, $this->version, $argv, $doc);
71-
72-
list($matched, $left, $collected) = $pattern->fix()->match($argv);
73-
if ($matched && !$left) {
74-
$return = array();
75-
foreach (array_merge($pattern->flat(), $collected) as $a) {
76-
$name = $a->name;
77-
if ($name)
78-
$return[$name] = $a->value;
79-
}
80-
return new Response($return);
81-
}
82-
throw new ExitException();
83-
}
84-
catch (ExitException $ex) {
85-
$this->handleExit($ex);
86-
return new Response(null, $ex->status, $ex->getMessage());
87-
}
88-
}
89-
90-
function handleExit(ExitException $ex)
91-
{
92-
if ($this->exit) {
93-
echo $ex->getMessage().PHP_EOL;
94-
exit($ex->status);
95-
}
96-
}
9734
}
98-
9935
}
10036

10137
namespace Docopt
10238
{
103-
/**
104-
* @deprecated Use Docopt class as-is
105-
*/
106-
class Handler extends \Docopt
107-
{
108-
}
109-
110-
/**
111-
* @deprecated Use Docopt::docopt(...)
112-
*/
113-
function docopt()
114-
{
115-
return call_user_func_array(array('Docopt', 'docopt'), func_get_args());
116-
}
117-
11839
/**
11940
* Return true if all cased characters in the string are uppercase and there is
12041
* at least one cased character, false otherwise.
@@ -1112,6 +1033,65 @@ function extras($help, $version, $options, $doc)
11121033
}
11131034
}
11141035

1036+
class Handler
1037+
{
1038+
public $exit = true;
1039+
public $help = true;
1040+
public $optionsFirst = false;
1041+
public $version;
1042+
1043+
public function __construct($options=array())
1044+
{
1045+
foreach ($options as $k=>$v)
1046+
$this->$k = $v;
1047+
}
1048+
1049+
function handle($doc, $argv=null)
1050+
{
1051+
try {
1052+
if ($argv === null && isset($_SERVER['argv']))
1053+
$argv = array_slice($_SERVER['argv'], 1);
1054+
1055+
ExitException::$usage = printable_usage($doc);
1056+
$options = parse_defaults($doc);
1057+
1058+
$formalUse = formal_usage(ExitException::$usage);
1059+
$pattern = parse_pattern($formalUse, $options);
1060+
$argv = parse_argv(new TokenStream($argv, 'ExitException'), $options, $this->optionsFirst);
1061+
foreach ($pattern->flat('AnyOptions') as $ao) {
1062+
$docOptions = parse_defaults($doc);
1063+
$ao->children = array_diff((array)$docOptions, $pattern->flat('Option'));
1064+
}
1065+
1066+
extras($this->help, $this->version, $argv, $doc);
1067+
1068+
list($matched, $left, $collected) = $pattern->fix()->match($argv);
1069+
if ($matched && !$left) {
1070+
$return = array();
1071+
foreach (array_merge($pattern->flat(), $collected) as $a) {
1072+
$name = $a->name;
1073+
if ($name)
1074+
$return[$name] = $a->value;
1075+
}
1076+
return new Response($return);
1077+
}
1078+
throw new ExitException();
1079+
}
1080+
catch (ExitException $ex) {
1081+
$this->handleExit($ex);
1082+
return new Response(null, $ex->status, $ex->getMessage());
1083+
}
1084+
}
1085+
1086+
function handleExit(ExitException $ex)
1087+
{
1088+
if ($this->exit) {
1089+
echo $ex->getMessage().PHP_EOL;
1090+
exit($ex->status);
1091+
}
1092+
}
1093+
}
1094+
11151095
class Response implements \ArrayAccess, \IteratorAggregate
11161096
{
11171097
public $status;

test/testee.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$result = null;
1313
ob_start();
1414
try {
15-
$result = Docopt\docopt($in, array('exit'=>false));
15+
$result = Docopt::handle($in, array('exit'=>false));
1616
}
1717
catch (Exception $ex) {
1818
}

0 commit comments

Comments
 (0)