Permalink
Browse files

Adds a plain and silent option to Command.

If `--plain`, then no styles are displayed.
If `--silent`, then only error is output.
  • Loading branch information...
1 parent 7024e5a commit 86ec5049182650c43864f08e24d7a1ecd439b628 @gwoo gwoo committed Jul 10, 2012
Showing with 64 additions and 9 deletions.
  1. +32 −8 console/Command.php
  2. +32 −1 tests/cases/console/CommandTest.php
View
@@ -50,6 +50,20 @@ class Command extends \lithium\core\Object {
public $response;
/**
+ * Only shows only text output without styles.
+ *
+ * @var boolean
+ */
+ public $plain = false;
+
+ /**
+ * Only shows error output.
+ *
+ * @var boolean
+ */
+ public $silent = false;
+
+ /**
* Dynamic dependencies.
*
* @var array
@@ -88,12 +102,15 @@ public function __construct(array $config = array()) {
protected function _init() {
parent::_init();
$this->request = $this->_config['request'];
- $resp = $this->_config['response'];
- $this->response = is_object($resp) ? $resp : $this->_instance('response', $resp);
if (!is_object($this->request) || !$this->request->params) {
return;
}
+ $this->response = $this->_config['response'];
+
+ if (!is_object($this->response)) {
+ $this->response = $this->_instance('response', $this->response);
+ }
$default = array('command' => null, 'action' => null, 'args' => null);
$params = array_diff_key((array) $this->request->params, $default);
@@ -160,6 +177,9 @@ protected function _help() {
* @return integer
*/
public function out($output = null, $options = array('nl' => 1)) {
+ if ($this->silent) {
+ return;
+ }
return $this->_response('output', $output, $options);
}
@@ -285,8 +305,8 @@ public function header($text, $line = null) {
* @return void
*/
public function columns($rows, $options = array()) {
- $defaults = array('separator' => "\t");
- $config = $options + $defaults;
+ $defaults = array('separator' => "\t", "error" => false);
+ $options += $defaults;
$lengths = array_reduce($rows, function($columns, $row) {
foreach ((array) $row as $key => $val) {
if (!isset($columns[$key]) || strlen($val) > $columns[$key]) {
@@ -295,15 +315,19 @@ public function columns($rows, $options = array()) {
}
return $columns;
});
- $rows = array_reduce($rows, function($rows, $row) use ($lengths, $config) {
+ $rows = array_reduce($rows, function($rows, $row) use ($lengths, $options) {
$text = '';
foreach ((array) $row as $key => $val) {
- $text = $text . str_pad($val, $lengths[$key]) . $config['separator'];
+ $text = $text . str_pad($val, $lengths[$key]) . $options['separator'];
}
$rows[] = $text;
return $rows;
});
- $this->out($rows, $config);
+ if ($options['error']) {
+ $this->error($rows, $options);
+ return;
+ }
+ $this->out($rows, $options);
}
/**
@@ -386,7 +410,7 @@ protected function _response($type, $string, $options) {
}
extract($options);
- if ($style !== null) {
+ if ($style !== null && !$this->plain) {
$string = "{:{$style}}{$string}{:end}";
}
if ($nl) {
@@ -152,7 +152,7 @@ public function testHelp() {
$command = new MockCommand(array('request' => $this->request));
$return = $command->__invoke('_help');
- $this->assertTrue($return);
+ $this->assertTrue($return instanceOf \lithium\tests\mocks\console\MockResponse);
$expected = "DESCRIPTION.*This is the Mock Command";
$result = $command->response->output;
@@ -235,6 +235,37 @@ public function testInWithBadInput(){
$result = $command->in('Everything Cool?', array('choices' => array('y', 'n')));
$this->assertEqual($expected, $result);
}
+
+ public function testOutWithStyles() {
+ $command = new MockCommand(array('request' => $this->request));
+ $expected = "{:some-style}ok\n";
+ $result = $command->out('ok', 'some-style');
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testOutWithoutStyles() {
+ $command = new MockCommand(array('request' => $this->request));
+ $command->plain = true;
+ $expected = "ok\n";
+ $result = $command->out('ok', 'some-style');
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testOutWithSilent() {
+ $command = new MockCommand(array('request' => $this->request));
+ $command->silent = true;
+ $expected = "";
+ $result = $command->out('ok', 'some-style');
+ $this->assertEqual($expected, $result);
+ }
+
+ public function testColumnsOnErrorOutput() {
+ $command = new MockCommand(array('request' => $this->request));
+ $expected = "data1\t\ndata2\t\n";
+ $command->columns(array('col1' => 'data1', 'col2' => 'data2'), array('error' => true));
+ $result = $command->response->error;
+ $this->assertEqual($expected, $result);
+ }
}
?>

0 comments on commit 86ec504

Please sign in to comment.