summaryrefslogtreecommitdiffstats
path: root/Console_CommandLine-bug18682.patch
blob: 81537be971dfd83c6e365ee7669aa1698131bf1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
From 987dd5793550a1cb8930bb06cf8f384bdbbbf2a4 Mon Sep 17 00:00:00 2001
From: David Jean Louis <izimobil@gmail.com>
Date: Thu, 25 Oct 2012 10:13:42 +0200
Subject: [PATCH] * Fixed bug #18682 (columnWrap() in Default Renderer eats up
 lines with only a EOL) [izi, thanks Helgi] * Fixed bug
 #18703 (No way to override reading of stdin with -) [izi,
 thanks Gwynne Raskind] * Fixed unit tests [izi]

---
 Console/CommandLine.php                    |   18 +++++--
 Console/CommandLine/Renderer/Default.php   |   10 +++-
 package.xml                                |   30 ++++++++---
 tests/console_commandline_addargument.phpt |   20 +++++--
 tests/console_commandline_bug18682.phpt    |   79 ++++++++++++++++++++++++++++
 5 files changed, 142 insertions(+), 15 deletions(-)
 create mode 100644 tests/console_commandline_bug18682.phpt

diff --git a/Console/CommandLine.php b/Console/CommandLine.php
index c1b01e1..0ccfb7c 100644
--- a/Console/CommandLine.php
+++ b/Console/CommandLine.php
@@ -176,6 +176,15 @@ class Console_CommandLine
      */
     public $force_options_defaults = false;
 
+ 
+   /**
+    * Boolean that tells the parser to treat a single - option as an argument
+    * instead of trying to read STDIN.
+    *
+    * @var bool $avoid_reading_stdin Whether to treat - as an argument
+    */
+    public $avoid_reading_stdin = false;
+
     /**
      * An array of Console_CommandLine_Option objects.
      *
@@ -999,7 +1008,8 @@ protected function parseToken($token, $result, &$args, $argc)
         static $stopflag = false;
         $last  = $argc === 0;
         if (!$stopflag && $lastopt) {
-            if (substr($token, 0, 1) == '-') {
+            if (strlen($token) > ($this->avoid_reading_stdin ? 1 : 0) &&
+                substr($token, 0, 1) == '-') {
                 if ($lastopt->argument_optional) {
                     $this->_dispatchAction($lastopt, '', $result);
                     if ($lastopt->action != 'StoreArray') {
@@ -1085,10 +1095,12 @@ protected function parseToken($token, $result, &$args, $argc)
                 $lastopt = $opt;
             }
             $this->_dispatchAction($opt, $value, $result);
-        } else if (!$stopflag && substr($token, 0, 1) == '-') {
+        } else if (!$stopflag &&
+                   strlen($token) > ($this->avoid_reading_stdin ? 1 : 0) &&
+                   substr($token, 0, 1) == '-') {
             // a short option
             $optname = substr($token, 0, 2);
-            if ($optname == '-') {
+            if ($optname == '-' && !$this->avoid_reading_stdin) {
                 // special case of "-": try to read stdin
                 $args[] = file_get_contents('php://stdin');
                 return;
diff --git a/Console/CommandLine/Renderer/Default.php b/Console/CommandLine/Renderer/Default.php
index 59ce33e..8f34b26 100644
--- a/Console/CommandLine/Renderer/Default.php
+++ b/Console/CommandLine/Renderer/Default.php
@@ -409,12 +409,18 @@ protected function columnWrap($text, $cw)
     {
         $tokens = explode("\n", $this->wrap($text));
         $ret    = $tokens[0];
-        $chunks = $this->wrap(trim(substr($text, strlen($ret))), 
-            $this->line_width - $cw);
+        $text   = trim(substr($text, strlen($ret)));
+        if (empty($text)) {
+            return $ret;
+        }
+
+        $chunks = $this->wrap($text, $this->line_width - $cw);
         $tokens = explode("\n", $chunks);
         foreach ($tokens as $token) {
             if (!empty($token)) {
                 $ret .= "\n" . str_repeat(' ', $cw) . $token;
+            } else {
+                $ret .= "\n";
             }
         }
         return $ret;
diff --git a/tests/console_commandline_addargument.phpt b/tests/console_commandline_addargument.phpt
index ca87c65..eb2e3af 100644
--- a/tests/console_commandline_addargument.phpt
+++ b/tests/console_commandline_addargument.phpt
@@ -27,11 +27,14 @@ $parser->addArgument('Some invalid name');
 --EXPECTF--
 array(4) {
   ["arg1"]=>
-  object(Console_CommandLine_Argument)#5 (7) {
+  object(Console_CommandLine_Argument)#5 (8) {
     ["multiple"]=>
     bool(false)
     ["optional"]=>
     bool(false)
+    ["choices"]=>
+    array(0) {
+    }
     ["name"]=>
     string(4) "arg1"
     ["help_name"]=>
@@ -45,11 +48,14 @@ array(4) {
     }
   }
   ["arg2"]=>
-  object(Console_CommandLine_Argument)#6 (7) {
+  object(Console_CommandLine_Argument)#6 (8) {
     ["multiple"]=>
     bool(true)
     ["optional"]=>
     bool(false)
+    ["choices"]=>
+    array(0) {
+    }
     ["name"]=>
     string(4) "arg2"
     ["help_name"]=>
@@ -63,11 +69,14 @@ array(4) {
     }
   }
   ["arg3"]=>
-  object(Console_CommandLine_Argument)#7 (7) {
+  object(Console_CommandLine_Argument)#7 (8) {
     ["multiple"]=>
     bool(true)
     ["optional"]=>
     bool(false)
+    ["choices"]=>
+    array(0) {
+    }
     ["name"]=>
     string(4) "arg3"
     ["help_name"]=>
@@ -81,11 +90,14 @@ array(4) {
     }
   }
   ["arg4"]=>
-  object(Console_CommandLine_Argument)#8 (7) {
+  object(Console_CommandLine_Argument)#8 (8) {
     ["multiple"]=>
     bool(false)
     ["optional"]=>
     bool(true)
+    ["choices"]=>
+    array(0) {
+    }
     ["name"]=>
     string(4) "arg4"
     ["help_name"]=>
diff --git a/tests/console_commandline_bug18682.phpt b/tests/console_commandline_bug18682.phpt
new file mode 100644
index 0000000..aa769c3
--- /dev/null
+++ b/tests/console_commandline_bug18682.phpt
@@ -0,0 +1,79 @@
+--TEST--
+Test for bug #18682: columnWrap() in Default Renderer eats up lines with only a EOL.
+--ARGS--
+cmd1 --help 2>&1
+--FILE--
+<?php
+
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tests.inc.php';
+
+class Renderer extends Console_CommandLine_Renderer_Default {
+  protected function description() {
+    return $this->columnWrap($this->parser->description, 2);
+  }
+}
+
+$parser = new Console_CommandLine();
+$parser->accept(new Renderer);
+$parser->renderer->line_width = 75;
+$parser->addCommand('cmd1', array(
+    'description' => '
+Installs listed packages.
+
+local package.xml example:
+php pyrus.phar install package.xml
+
+local package archive example:
+php pyrus.phar install PackageName-1.2.0.tar
+
+remote package archive example:
+php pyrus.phar install http://www.example.com/PackageName-1.2.0.tgz
+
+Examples of an abstract package:
+php pyrus.phar install PackageName
+  installs PackageName from the default channel with stability preferred_state
+php pyrus.phar pear/PackageName
+  installs PackageName from the pear.php.net channel with stability preferred_state
+php pyrus.phar install channel://doc.php.net/PackageName
+  installs PackageName from the doc.php.net channel with stability preferred_state
+php pyrus.phar install PackageName-beta
+  installs PackageName from the default channel, beta or stable stability
+php pyrus.phar install PackageName-1.2.0
+  installs PackageName from the default channel, version 1.2.0'
+));
+$parser->parse();
+
+?>
+--EXPECTF--
+  Installs listed packages.
+
+  local package.xml example:
+  php pyrus.phar install package.xml
+
+  local package archive example:
+  php pyrus.phar install PackageName-1.2.0.tar
+
+  remote package archive example:
+  php pyrus.phar install http://www.example.com/PackageName-1.2.0.tgz
+
+  Examples of an abstract package:
+  php pyrus.phar install PackageName
+    installs PackageName from the default channel with stability
+  preferred_state
+  php pyrus.phar pear/PackageName
+    installs PackageName from the pear.php.net channel with stability
+  preferred_state
+  php pyrus.phar install channel://doc.php.net/PackageName
+    installs PackageName from the doc.php.net channel with stability
+  preferred_state
+  php pyrus.phar install PackageName-beta
+    installs PackageName from the default channel, beta or stable stability
+  php pyrus.phar install PackageName-1.2.0
+    installs PackageName from the default channel, version 1.2.0
+
+Usage:
+  %sconsole_commandline_bug18682.php
+  [options] cmd1 [options]
+
+Options:
+  -h, --help  show this help message and exit
-- 
1.7.10