From cc1eb8b0d002ec2753668d96c2a42db5840eb583 Mon Sep 17 00:00:00 2001 From: Hassan Khan Date: Sun, 26 Jan 2014 23:43:31 +0000 Subject: [PATCH] ``Zepto\Console`` now uses double-quotes everywhere. Also updated test class accordingly. --- library/Zepto/Console.php | 106 ++++++++++++------------ tests/Zepto/ConsoleTest.php | 160 ++++++++++++++++++------------------ 2 files changed, 134 insertions(+), 132 deletions(-) diff --git a/library/Zepto/Console.php b/library/Zepto/Console.php index e7619ab..d4a0261 100644 --- a/library/Zepto/Console.php +++ b/library/Zepto/Console.php @@ -19,6 +19,8 @@ * Parses command line inputs, and provides an easy, OOP approach to complex-ish * CLI scripts. * + * All output is wrapped with ``PHP_EOL``, and this cannot be changed. + * * * use Zepto\Console as Console; * @@ -60,10 +62,10 @@ public function __construct($inputs = null) * Add option * * Example: - * $cli->option('-p, --peppers', 'Add peppers'); - * $cli->option('-c, --cheese [type]', 'Add a cheese', true); - * $cli->get('-p'); // true/false - * $cli->get('-c'); // cheese type + * $zep->option('-p, --peppers', 'Add peppers'); + * $zep->option('-c, --cheese [type]', 'Add a cheese', true); + * $zep->get('-p'); // true/false + * $zep->get('-c'); // cheese type * * @param string $flags * @param string $help_text @@ -73,13 +75,13 @@ public function option($flags, $help_text, $required = false) { $options = $this->parseOption($flags); - $options['help'] = $flags . ' ' . $help_text; + $options["help"] = $flags . " " . $help_text; if ($required) { - $options['required'] = true; + $options["required"] = true; } - $this->setOption($options['short'], $options); + $this->setOption($options["short"], $options); } @@ -87,8 +89,8 @@ public function option($flags, $help_text, $required = false) * Add param * * Example: - * $cli->param('client', 'Name of client', true); - * $cli->get('client'); + * $zep->param('client', 'Name of client', true); + * $zep->get('client'); * * @param string $param * @param string $help_text @@ -98,13 +100,13 @@ public function param($param, $help_text, $required = false) { $options = array(); - $options['name'] = $param; + $options["name"] = $param; if ($required) { - $options['help'] = '<' . $param . '> ' . $help_text; - $options['required'] = true; + $options["help"] = "<" . $param . "> " . $help_text; + $options["required"] = true; } else { - $options['help'] = '[' . $param . '] ' . $help_text; - $options['required'] = false; + $options["help"] = "[" . $param . "] " . $help_text; + $options["required"] = false; } $this->setParam($options); @@ -119,17 +121,17 @@ public function param($param, $help_text, $required = false) private function parseOption($string) { $output = array(); - $exploded = explode(',', $string); + $exploded = explode(",", $string); - $output['short'] = $exploded[0]; // short flag + $output["short"] = $exploded[0]; // short flag - $regex = '/\[(.*)\]/'; - $output['long'] = $exploded[1]; + $regex = "/\[(.*)\]/"; + $output["long"] = $exploded[1]; if (preg_match($regex, $exploded[1])) { // check for input - $output['long'] = preg_replace($regex, '', $exploded[1]); // replace input from string - $output['input'] = true; // set input as true + $output["long"] = preg_replace($regex, "", $exploded[1]); // replace input from string + $output["input"] = true; // set input as true } - $output['long'] = trim($output['long']); + $output["long"] = trim($output["long"]); return $output; } @@ -146,9 +148,9 @@ public function parse() { // check if a help flag is set try { - $key = $this->checkInputs('-h', '--help'); + $key = $this->checkInputs("-h", "--help"); if ($key !== false) { - throw new \Exception('Help flag is set'); + throw new \Exception("Help flag is set"); } } catch (\Exception $e) { $this->outputHelp(); @@ -158,20 +160,20 @@ public function parse() // loop through options and see if they are in the inputs foreach ($this->options as $option => $info) { // if option is in inputs - $key = $this->checkInputs($info['short'], $info['long']); + $key = $this->checkInputs($info["short"], $info["long"]); if ($key === false) { - $this->pinputs[$info['short']] = false; - $this->pinputs[$info['long']] = false; + $this->pinputs[$info["short"]] = false; + $this->pinputs[$info["long"]] = false; } else { // check if next input should be in input - if (array_key_exists('input', $info) && $info['input'] == true) { - $this->pinputs[$info['short']] = $this->inputs[$key + 1]; - $this->pinputs[$info['long']] = $this->inputs[$key + 1]; + if (array_key_exists("input", $info) && $info["input"] == true) { + $this->pinputs[$info["short"]] = $this->inputs[$key + 1]; + $this->pinputs[$info["long"]] = $this->inputs[$key + 1]; unset($this->inputs[$key]); // remove flag from inputs array unset($this->inputs[$key + 1]); } else { - $this->pinputs[$info['short']] = true; - $this->pinputs[$info['long']] = true; + $this->pinputs[$info["short"]] = true; + $this->pinputs[$info["long"]] = true; unset($this->inputs[$key]); } } @@ -183,7 +185,7 @@ public function parse() foreach ($this->inputs as $key => $input) { // If parameter for input exists if (array_key_exists($count, $this->params)) { - $this->pinputs[$this->params[$count]['name']] = $input; + $this->pinputs[$this->params[$count]["name"]] = $input; unset($this->inputs[$key]); // remove input from inputs } $count++; @@ -239,9 +241,9 @@ private function checkRequired() { // Loop through all params foreach ($this->params as $param) { - if (array_key_exists('required', $param) && $param['required'] == true) { - if (!array_key_exists($param['name'], $this->pinputs)) { - throw new \Exception('Parameter "' . $param['name'] . '" is required'); + if (array_key_exists("required", $param) && $param["required"] == true) { + if (!array_key_exists($param["name"], $this->pinputs)) { + throw new \Exception("Parameter '{$param["name"]}' is required"); } } } @@ -249,10 +251,10 @@ private function checkRequired() // Loop through all options foreach ($this->options as $key => $option) { // if option is required - if (array_key_exists('required', $option) && $option['required'] == true) { + if (array_key_exists("required", $option) && $option["required"] == true) { // check that it is defined in pinputs - if ($this->pinputs[$option['short']] == false) { - throw new \Exception('Option "' . $option['help'] . '" is required'); + if ($this->pinputs[$option["short"]] == false) { + throw new \Exception("Option '{$option["help"]}' is required"); } } } @@ -330,7 +332,7 @@ public function getName() public function get($flag) { if (!array_key_exists($flag, $this->pinputs)) { - throw new \Exception('Input ' . $flag . ' cannot be found'); + throw new \Exception("Input $flag cannot be found"); } else { return $this->pinputs[$flag]; } @@ -351,16 +353,16 @@ public function get($flag) public static function prompt($msg, $password = null) { // output message - echo $msg . ' '; + echo PHP_EOL . "$msg "; // if password then disable text output if ($password != null) { - system('stty -echo'); + system("stty -echo"); } $input = trim(fgets(STDIN)); if ($password != null) { - system('stty echo'); + system("stty echo"); echo PHP_EOL; // output end of line because the user's CR won't have been outputted } return $input; @@ -376,10 +378,10 @@ public static function prompt($msg, $password = null) */ public static function confirm($msg) { - echo $msg . ' '; + echo PHP_EOL . "$msg "; $input = trim(fgets(STDIN)); - if (strtolower($input) == 'y' || strtolower($input) == 'yes') { + if (strtolower($input) == "y" || strtolower($input) == "yes") { return true; } else { return false; @@ -414,13 +416,13 @@ public static function out($msg) */ public function outputHelp($short = false) { - echo PHP_EOL . "Usage: " . $this->getName() . " "; + echo PHP_EOL . "Usage: {$this->getName()} "; if (!empty($this->params)) { foreach ($this->params as $param) { - if ($param['required'] == true) { - echo '<' . $param['name'] . '> '; + if ($param["required"] === true) { + echo "<{$param["name"]}> "; } else { - echo '[' . $param['name'] . '] '; + echo "[{$param["name"]}] "; } } } @@ -432,21 +434,21 @@ public function outputHelp($short = false) if (!empty($this->params)) { echo "Parameters:" . PHP_EOL; foreach ($this->params as $param) { - echo "\t" . $param['help'] . PHP_EOL; + echo "\t{$param["help"]}" . PHP_EOL; } echo PHP_EOL; } echo "Options:" . PHP_EOL; foreach ($this->options as $option) { - $output = "\t" . $option['help']; - if (array_key_exists('required', $option) && $option['required'] == true) { + $output = "\t{$option["help"]}"; + if (array_key_exists("required", $option) && $option["required"] === true) { $output .= " [required]"; } $output .= PHP_EOL; echo $output; } - echo "\t" . $this->helpOption . PHP_EOL; + echo "\t{$this->helpOption}" . PHP_EOL; } else { echo PHP_EOL; } diff --git a/tests/Zepto/ConsoleTest.php b/tests/Zepto/ConsoleTest.php index 26c044d..a002245 100644 --- a/tests/Zepto/ConsoleTest.php +++ b/tests/Zepto/ConsoleTest.php @@ -12,15 +12,15 @@ class ConsoleTest extends \PHPUnit_Framework_TestCase * @covers Zepto\Console::getInputs() */ public function testGetName() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '-p' )); - $cli->parse(); - $inputs = $cli->getInputs(); + $zep->parse(); + $inputs = $zep->getInputs(); - $this->assertEquals('cli.php', $cli->getName()); + $this->assertEquals('zep', $zep->getName()); $this->assertEquals('-p', $inputs[0]); } @@ -29,11 +29,11 @@ public function testGetName() { * @covers Zepto\Console::getOptions */ public function testOption() { - $cli = new Console(array('cli.php')); - $cli->option('-p, --peppers', 'Add peppers'); - $cli->option('-c, --cheese [type]', 'Add a cheese'); + $zep = new Console(array('zep')); + $zep->option('-p, --peppers', 'Add peppers'); + $zep->option('-c, --cheese [type]', 'Add a cheese'); - $options = $cli->getOptions(); + $options = $zep->getOptions(); $this->assertCount(2, $options); $this->assertArrayHasKey('-p', $options); @@ -46,21 +46,21 @@ public function testOption() { * @covers Zepto\Console::parse */ public function testRequiredOption() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '-p' )); - $cli->option('-h, --ham', 'Add ham'); - $cli->option('-b, --bread [type]', 'Type of bread', true); + $zep->option('-h, --ham', 'Add ham'); + $zep->option('-b, --bread [type]', 'Type of bread', true); - $cli->parse(); + $zep->parse(); // expect parse to throw an exception that input is not defined $this->expectOutputString( - "Option \"-b, --bread [type] Type of bread\" is required" + "Option '-b, --bread [type] Type of bread' is required" . PHP_EOL . PHP_EOL . PHP_EOL - . "Usage: cli.php [options]" . PHP_EOL + . "Usage: zep [options]" . PHP_EOL ); } @@ -68,37 +68,37 @@ public function testRequiredOption() { * Test params */ public function testParams() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', 'test', 'uk' )); - $cli->param('client', 'Name of client', true); - $cli->param('locale', 'Client locale'); - $cli->parse(); + $zep->param('client', 'Name of client', true); + $zep->param('locale', 'Client locale'); + $zep->parse(); // expect parse to throw an exception that input is not defined - $this->assertEquals("test", $cli->get('client')); - $this->assertEquals("uk", $cli->get('locale')); + $this->assertEquals("test", $zep->get('client')); + $this->assertEquals("uk", $zep->get('locale')); } /** * Test required */ public function testRequiredParam() { - $cli = new Console(array( - 'cli.php' + $zep = new Console(array( + 'zep' )); - $cli->param('client', 'Specify client', true); + $zep->param('client', 'Specify client', true); - $cli->parse(); + $zep->parse(); // expect parse to throw an exception that input is not defined $this->expectOutputString( - "Parameter \"client\" is required" + "Parameter 'client' is required" . PHP_EOL . PHP_EOL . PHP_EOL - . "Usage: cli.php [options]" . PHP_EOL + . "Usage: zep [options]" . PHP_EOL ); } @@ -107,34 +107,34 @@ public function testRequiredParam() { * @covers Zepto\Console::parse */ public function testParse() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '-p', '--cheese', 'cheddar' )); - $cli->option('-p, --peppers', 'Add peppers'); - $cli->option('-c, --cheese [type]', 'Add a cheese'); - $cli->option('-m, --mayo', 'Add mayonaise'); + $zep->option('-p, --peppers', 'Add peppers'); + $zep->option('-c, --cheese [type]', 'Add a cheese'); + $zep->option('-m, --mayo', 'Add mayonaise'); - $cli->parse(); + $zep->parse(); - $this->assertTrue($cli->get('-p')); - $this->assertTrue($cli->get('--peppers')); + $this->assertTrue($zep->get('-p')); + $this->assertTrue($zep->get('--peppers')); - $this->assertEquals('cheddar', $cli->get('-c')); - $this->assertEquals('cheddar', $cli->get('--cheese')); + $this->assertEquals('cheddar', $zep->get('-c')); + $this->assertEquals('cheddar', $zep->get('--cheese')); - $this->assertFalse($cli->get('-m')); - $this->assertFalse($cli->get('--mayo')); + $this->assertFalse($zep->get('-m')); + $this->assertFalse($zep->get('--mayo')); } /** * @covers Zepto\Console::parse() */ public function testParsingNonOptions() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '-p', '--cheese', 'cheddar', @@ -142,20 +142,20 @@ public function testParsingNonOptions() { '-b', 'info' )); - $cli->option('-p, --peppers', 'Add peppers'); - $cli->option('-c, --cheese [type]', 'Add a cheese'); + $zep->option('-p, --peppers', 'Add peppers'); + $zep->option('-c, --cheese [type]', 'Add a cheese'); - $cli->parse(); + $zep->parse(); - $this->assertTrue($cli->get('-p')); - $this->assertTrue($cli->get('--peppers')); + $this->assertTrue($zep->get('-p')); + $this->assertTrue($zep->get('--peppers')); - $this->assertEquals('cheddar', $cli->get('-c')); - $this->assertEquals('cheddar', $cli->get('--cheese')); + $this->assertEquals('cheddar', $zep->get('-c')); + $this->assertEquals('cheddar', $zep->get('--cheese')); - $this->assertEquals('extra', $cli->get(0)); - $this->assertEquals('-b', $cli->get(1)); - $this->assertEquals('info', $cli->get(2)); + $this->assertEquals('extra', $zep->get(0)); + $this->assertEquals('-b', $zep->get(1)); + $this->assertEquals('info', $zep->get(2)); } /** @@ -163,14 +163,14 @@ public function testParsingNonOptions() { */ public function testGetParams() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '-t', '-p', )); - $cli->param('option', 'Name of option'); - $cli->param('option2', 'Name of option 2', true); + $zep->param('option', 'Name of option'); + $zep->param('option2', 'Name of option 2', true); $expected = array( 0 => array( @@ -191,19 +191,19 @@ public function testGetParams() */ public function testGet() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '-t', '-p', )); - $cli->option('-t, --test', 'Test'); - $cli->option('-p, --pest', 'Pest'); + $zep->option('-t, --test', 'Test'); + $zep->option('-p, --pest', 'Pest'); - $cli->parse(); + $zep->parse(); - $this->assertTrue($cli->get('-t')); - $this->assertTrue($cli->get('-p')); + $this->assertTrue($zep->get('-t')); + $this->assertTrue($zep->get('-p')); } /** @@ -212,12 +212,12 @@ public function testGet() */ public function testGetNonexistentOption() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '--test' )); - $cli->get('unreal'); + $zep->get('unreal'); } /** @@ -225,12 +225,12 @@ public function testGetNonexistentOption() */ public function testOut() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '-test' )); - $cli->out('HAHA'); + $zep->out('HAHA'); $this->expectOutputString(PHP_EOL . 'HAHA' . PHP_EOL); } @@ -247,23 +247,23 @@ public function testOutAsStaticMethod() * Test help text */ public function testHelp() { - $cli = new Console(array( - 'cli.php', + $zep = new Console(array( + 'zep', '-p', '--help' )); - $cli->option('-p, --peppers', 'Add peppers'); - $cli->option('-c, --cheese [type]', 'Add a cheese'); - $cli->option('-m, --mayo', 'Add mayonaise'); - $cli->option('-b, --bread [type]', 'Type of bread', true); + $zep->option('-p, --peppers', 'Add peppers'); + $zep->option('-c, --cheese [type]', 'Add a cheese'); + $zep->option('-m, --mayo', 'Add mayonaise'); + $zep->option('-b, --bread [type]', 'Type of bread', true); - $cli->param('client', 'Name of client', true); - $cli->param('locale', 'Client locale'); + $zep->param('client', 'Name of client', true); + $zep->param('locale', 'Client locale'); - $cli->parse(); + $zep->parse(); - $this->expectOutputString(PHP_EOL . "Usage: cli.php [locale] [options]\n\nParameters:\n\t Name of client\n\t[locale] Client locale\n\nOptions:\n\t-p, --peppers Add peppers\n\t-c, --cheese [type] Add a cheese\n\t-m, --mayo Add mayonaise\n\t-b, --bread [type] Type of bread [required]\n\t-h, --help Output usage information\n"); + $this->expectOutputString(PHP_EOL . "Usage: zep [locale] [options]\n\nParameters:\n\t Name of client\n\t[locale] Client locale\n\nOptions:\n\t-p, --peppers Add peppers\n\t-c, --cheese [type] Add a cheese\n\t-m, --mayo Add mayonaise\n\t-b, --bread [type] Type of bread [required]\n\t-h, --help Output usage information\n"); } }