Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Updated Zepto\Console and changed all method signatures to use sn…
Browse files Browse the repository at this point in the history
…ake case, fitting in more with my liking
  • Loading branch information
hassankhan committed Feb 16, 2014
1 parent 1edc028 commit fedf730
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
38 changes: 19 additions & 19 deletions library/Zepto/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function __construct($inputs = null)
*/
public function option($flags, $help_text, $required = FALSE)
{
$options = $this->parseOption($flags);
$options = $this->parse_option($flags);

$options["help"] = $flags . ' ' . $help_text;

if ($required) {
$options["required"] = TRUE;
}

$this->setOption($options["short"], $options);
$this->set_option($options["short"], $options);
}


Expand Down Expand Up @@ -107,7 +107,7 @@ public function param($param, $help_text, $required = FALSE)
$options["required"] = FALSE;
}

$this->setParam($options);
$this->set_param($options);
}

/**
Expand All @@ -116,7 +116,7 @@ public function param($param, $help_text, $required = FALSE)
* @param string $string
* @return array
*/
private function parseOption($string)
private function parse_option($string)
{
$output = array();
$exploded = explode(",", $string);
Expand Down Expand Up @@ -146,20 +146,20 @@ public function parse()
{
// check if a help flag is set
try {
$key = $this->checkInputs("-h", "--help");
$key = $this->check_inputs("-h", "--help");
if ($key !== FALSE) {
throw new \Exception("Help flag is set");
}
}
catch (\Exception $e) {
$this->outputHelp();
$this->output_help();
return FALSE;
}

// 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->check_inputs($info["short"], $info["long"]);
if ($key === FALSE) {
$this->processed_inputs[$info["short"]] = FALSE;
$this->processed_inputs[$info["long"]] = FALSE;
Expand Down Expand Up @@ -200,10 +200,10 @@ public function parse()

// check required inputs
try {
$this->checkRequired();
$this->check_required();
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL . PHP_EOL;
$this->outputHelp(TRUE);
$this->output_help(TRUE);
return FALSE;
}

Expand All @@ -217,7 +217,7 @@ public function parse()
* @param string $long
* @return bool|mixed
*/
private function checkInputs($short, $long)
private function check_inputs($short, $long)
{
$key = array_search($short, $this->inputs);
if ($key === FALSE) {
Expand All @@ -237,7 +237,7 @@ private function checkInputs($short, $long)
*
* @throws \Exception if required param or option is not present
*/
private function checkRequired()
private function check_required()
{
// Loop through all params
foreach ($this->params as $param) {
Expand Down Expand Up @@ -267,7 +267,7 @@ private function checkRequired()
* @param string $name
* @param $options
*/
public function setOption($name, $options)
public function set_option($name, $options)
{
$this->options[$name] = $options;
}
Expand All @@ -277,7 +277,7 @@ public function setOption($name, $options)
*
* @param $options
*/
public function setParam($options)
public function set_param($options)
{
$this->params[] = $options;
}
Expand All @@ -287,7 +287,7 @@ public function setParam($options)
*
* @return array
*/
public function getOptions()
public function get_options()
{
return $this->options;
}
Expand All @@ -297,7 +297,7 @@ public function getOptions()
*
* @return array of params
*/
public function getParams()
public function get_params()
{
return $this->params;
}
Expand All @@ -307,7 +307,7 @@ public function getParams()
*
* @return array
*/
public function getInputs()
public function get_inputs()
{
return $this->processed_inputs;
}
Expand All @@ -317,7 +317,7 @@ public function getInputs()
*
* @return mixed
*/
public function getName()
public function get_name()
{
return $this->name;
}
Expand Down Expand Up @@ -414,9 +414,9 @@ public static function out($msg)
*
* @param bool $short
*/
public function outputHelp($short = FALSE)
public function output_help($short = FALSE)
{
echo PHP_EOL . "Usage: {$this->getName()} ";
echo PHP_EOL . "Usage: {$this->get_name()} ";
if (!empty($this->params)) {
foreach ($this->params as $param) {
if ($param["required"] === TRUE) {
Expand Down
80 changes: 40 additions & 40 deletions tests/Zepto/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ConsoleTest extends \PHPUnit_Framework_TestCase

/**
* @covers Zepto\Console::__construct()
* @covers Zepto\Console::getName()
* @covers Zepto\Console::get_name()
*/
public function testGetName() {
$zep = new Console(array(
Expand All @@ -19,12 +19,12 @@ public function testGetName() {

$zep->parse();

$this->assertEquals('zep', $zep->getName());
$this->assertEquals('zep', $zep->get_name());
}

/**
* @covers Zepto\Console::__construct()
* @covers Zepto\Console::getInputs()
* @covers Zepto\Console::get_inputs()
*/
public function testGetInputs() {
$zep = new Console(array(
Expand All @@ -39,14 +39,14 @@ public function testGetInputs() {
);

$zep->parse();
$this->assertEquals($expected, $zep->getInputs());
$this->assertEquals($expected, $zep->get_inputs());
}

/**
* @covers Zepto\Console::option()
* @covers Zepto\Console::checkInputs()
* @covers Zepto\Console::check_inputs()
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkRequired()
* @covers Zepto\Console::check_required()
*/
public function testParse() {
$zep = new Console(array(
Expand All @@ -73,9 +73,9 @@ public function testParse() {

/**
* @covers Zepto\Console::param()
* @covers Zepto\Console::setParam()
* @covers Zepto\Console::set_param()
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkRequired()
* @covers Zepto\Console::check_required()
*/
public function testParseWithParam() {
$zep = new Console(array(
Expand All @@ -94,9 +94,9 @@ public function testParseWithParam() {

/**
* @covers Zepto\Console::param()
* @covers Zepto\Console::setParam()
* @covers Zepto\Console::set_param()
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkRequired()
* @covers Zepto\Console::check_required()
*/
public function testParseWithRequiredParam() {
$zep = new Console(array(
Expand All @@ -113,9 +113,9 @@ public function testParseWithRequiredParam() {

/**
* @covers Zepto\Console::param()
* @covers Zepto\Console::setParam()
* @covers Zepto\Console::set_param()
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkRequired()
* @covers Zepto\Console::check_required()
*/
public function testParseWithRequiredParamWhenNotGiven() {
$zep = new Console(array(
Expand All @@ -134,18 +134,18 @@ public function testParseWithRequiredParamWhenNotGiven() {

/**
* @covers Zepto\Console::option()
* @covers Zepto\Console::parseOption()
* @covers Zepto\Console::setOption()
* @covers Zepto\Console::parse_option()
* @covers Zepto\Console::set_option()
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkRequired()
* @covers Zepto\Console::check_required()
*/
public function testParseWithOption() {
$zep = new Console(array('zep'));
$zep->option('-p, --peppers', 'Add peppers');
$zep->option('-c, --cheese [type]', 'Add a cheese');
$zep->parse();

$options = $zep->getOptions();
$options = $zep->get_options();

$this->assertCount(2, $options);
$this->assertArrayHasKey('-p', $options);
Expand All @@ -155,9 +155,9 @@ public function testParseWithOption() {

/**
* @covers Zepto\Console::option()
* @covers Zepto\Console::checkInputs()
* @covers Zepto\Console::check_inputs()
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkRequired()
* @covers Zepto\Console::check_required()
*/
public function testParseWithNonOptions() {
$zep = new Console(array(
Expand Down Expand Up @@ -187,10 +187,10 @@ public function testParseWithNonOptions() {

/**
* @covers Zepto\Console::option()
* @covers Zepto\Console::parseOption()
* @covers Zepto\Console::setOption()
* @covers Zepto\Console::parse_option()
* @covers Zepto\Console::set_option()
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkRequired()
* @covers Zepto\Console::check_required()
*/
public function testParseWithRequiredOption() {
$zep = new Console(array(
Expand All @@ -214,10 +214,10 @@ public function testParseWithRequiredOption() {

/**
* @covers Zepto\Console::option()
* @covers Zepto\Console::parseOption()
* @covers Zepto\Console::setOption()
* @covers Zepto\Console::parse_option()
* @covers Zepto\Console::set_option()
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkRequired()
* @covers Zepto\Console::check_required()
*/
public function testParseWithRequiredOptionNotGiven() {
$zep = new Console(array(
Expand All @@ -240,8 +240,8 @@ public function testParseWithRequiredOptionNotGiven() {

/**
* @covers Zepto\Console::parse()
* @covers Zepto\Console::checkInputs()
* @covers Zepto\Console::outputHelp()
* @covers Zepto\Console::check_inputs()
* @covers Zepto\Console::output_help()
*/
public function testParseWithHelpFlagSet() {
$zep = new Console(array(
Expand All @@ -255,7 +255,7 @@ public function testParseWithHelpFlagSet() {
}

/**
* @covers Zepto\Console::getParams()
* @covers Zepto\Console::get_params()
*/
public function testGetParams()
{
Expand All @@ -281,11 +281,11 @@ public function testGetParams()
),
);

$this->assertEquals($expected, $zep->getParams());
$this->assertEquals($expected, $zep->get_params());
}

/**
* @covers Zepto\Console::getOptions()
* @covers Zepto\Console::get_options()
*/
public function testGetOptions()
{
Expand All @@ -311,7 +311,7 @@ public function testGetOptions()
)
);

$this->assertEquals($expected, $zep->getOptions());
$this->assertEquals($expected, $zep->get_options());
}

/**
Expand Down Expand Up @@ -373,11 +373,11 @@ public function testOutAsStaticMethod()

/**
* @covers Zepto\Console::option()
* @covers Zepto\Console::parseOption()
* @covers Zepto\Console::setOption()
* @covers Zepto\Console::parse_option()
* @covers Zepto\Console::set_option()
* @covers Zepto\Console::param()
* @covers Zepto\Console::setParam()
* @covers Zepto\Console::outputHelp()
* @covers Zepto\Console::set_param()
* @covers Zepto\Console::output_help()
*/
public function testOutputHelp() {
$zep = new Console(array(
Expand All @@ -394,18 +394,18 @@ public function testOutputHelp() {
$zep->param('client', 'Name of client', true);
$zep->param('locale', 'Client locale');

$zep->outputHelp();
$zep->output_help();

$this->expectOutputString(PHP_EOL . "Usage: zep <client> [locale] [options]\n\nParameters:\n\t<client> 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");
}

/**
* @covers Zepto\Console::option()
* @covers Zepto\Console::parseOption()
* @covers Zepto\Console::setOption()
* @covers Zepto\Console::parse_option()
* @covers Zepto\Console::set_option()
* @covers Zepto\Console::param()
* @covers Zepto\Console::setParam()
* @covers Zepto\Console::outputHelp()
* @covers Zepto\Console::set_param()
* @covers Zepto\Console::output_help()
*/
public function testOutputHelpShortened() {
$zep = new Console(array(
Expand All @@ -422,7 +422,7 @@ public function testOutputHelpShortened() {
$zep->param('client', 'Name of client', true);
$zep->param('locale', 'Client locale');

$zep->outputHelp(TRUE);
$zep->output_help(TRUE);

$this->expectOutputString("\nUsage: zep <client> [locale] [options]\n");
}
Expand Down

0 comments on commit fedf730

Please sign in to comment.