Skip to content
Browse files

Adds file support

specify that an option must be a valid file or file glob pattern
returns a fully quantified file path or array of file paths the the case of globbing
added test coverage
  • Loading branch information...
1 parent ee87b4c commit a139bac073dc5f5e7f4bc8d0a7f15b83d200faa2 @nategood committed Oct 7, 2012
View
1 examples/assets/example.txt
@@ -0,0 +1 @@
+Example Input File
View
12 examples/file.php
@@ -0,0 +1,12 @@
+<?php
+
+require dirname(__DIR__) . '/vendor/autoload.php';
+
+// php file.php ./assets/example.txt
+
+$cmd = new Commando\Command();
+$cmd
+ ->argument()
+ ->expectsFile();
+
+var_dump($cmd->getArgumentValues());
View
14 src/Commando/Command.php
@@ -41,6 +41,7 @@ class Command implements \ArrayAccess, \Iterator
'boolean' => 'boolean',
'bool' => 'boolean',
'b' => 'boolean',
+ // mustBeBoolean
'require' => 'require',
'required' => 'require',
@@ -66,6 +67,14 @@ class Command implements \ArrayAccess, \Iterator
'castWith' => 'map',
'must' => 'must',
+ // mustBeNumeric
+ // mustBeInt
+ // mustBeFloat
+
+ 'file' => 'file',
+ 'expectsFile' => 'file',
+ // 'expectsFileGlob' => 'file',
+ // 'mustBeAFile' => 'file',
);
public function __construct($tokens = null)
@@ -237,6 +246,11 @@ private function _map(Option $option, \Closure $callback)
return $option->setMap($callback);
}
+ private function _file(Option $option, $require_exists = true, $allow_globbing = false)
+ {
+ return $option->setFileRequirements($require_exists, $allow_globbing);
+ }
+
public function useDefaultHelp($help = true)
{
View
50 src/Commando/Option.php
@@ -87,7 +87,7 @@ public function setBoolean($bool = true)
* @param bool $allow_globbing
* @throws \Exception if the file does not exists
*/
- public function setFile($require_exists = true, $allow_globbing = true)
+ public function setFileRequirements($require_exists = true, $allow_globbing = true)
{
$this->file = true;
$this->file_require_exists = $require_exists;
@@ -166,21 +166,23 @@ public function validate($value)
}
/**
- * @param string $filepath
+ * @param string $path
* @return string|array full file path or an array of file paths in the
- * case of "globbing" if supported
+ * case where "globbing" is supported
*/
- public function parseFile($filepath)
+ public function parseFilePath($file_path)
{
- $pwd = dirname($_SERVER['SCRIPT_NAME']);
- $path = realpath($pwd . $path);
- if ($this->file_require_exists && $path === false) {
- throw new \Exception('Argument must be a valid file'); // todo include argument/flag name
- }
- if ($this->file_support_globbing) {
- // glob();
- // todo
+ $path = realpath($file_path);
+ if ($this->file_allow_globbing) {
+ $files = glob($file_path);
+ if (empty($files)) {
+ return $files;
+ }
+ return array_map(function($file) {
+ return realpath($file);
+ }, $files);
}
+
return $path;
}
@@ -221,11 +223,19 @@ public function getAliases()
*/
public function isBoolean()
{
- $this->value = false;
+ // $this->value = false; // ?
return $this->boolean;
}
/**
+ * @return bool is this option a boolean
+ */
+ public function isFile()
+ {
+ return $this->file;
+ }
+
+ /**
* @return bool is this option required?
*/
public function isRequired()
@@ -238,15 +248,21 @@ public function isRequired()
*/
public function setValue($value)
{
- // boolean check
if ($this->isBoolean() && !is_bool($value)) {
throw new \Exception(sprintf('Boolean option expected for option -%s, received %s value instead', $this->name, $value));
}
- if (!$this->validate($value)){
+ if (!$this->validate($value)) {
throw new \Exception(sprintf('Invalid value, %s, for option -%s', $value, $this->name));
}
- if ($this->requireFile()) {
- $this->value = $this->parseFileString($value);
+ if ($this->isFile()) {
+ $file_path = $this->parseFilePath($value);
+ if (empty($file_path)) {
+ if ($this->file_require_exists) {
+ throw new \Exception(sprintf('Expected %s to be a valid file', $value, $this->name));
+ }
+ } else {
+ $value = $file_path;
+ }
}
$this->value = $this->map($value);
}
View
29 tests/Commando/OptionTest.php
@@ -49,7 +49,6 @@ public function testAddAlias()
*/
public function testSetValue($val)
{
- // $val = 'abc';
$option = new Option('f');
$option->setValue($val);
$this->assertEquals($val, $option->getValue());
@@ -94,6 +93,34 @@ public function testRule()
$this->assertTrue($caught);
}
+ public function testFile()
+ {
+ $file = dirname(__FILE__) . '/assets/example.txt';
+ $option = new Option(0);
+ $option->setFileRequirements(true, false);
+ $option->setValue($file);
+
+ $this->assertTrue($option->isFile());
+ $this->assertEquals($file, $option->getValue());
+ }
+
+ public function testFileGlob()
+ {
+ $file = dirname(__FILE__) . '/assets/*.txt';
+ $option = new Option(0);
+ $option->setFileRequirements(true, true);
+ $option->setValue($file);
+
+ $file1 = dirname(__FILE__) . '/assets/example.txt';
+ $file2 = dirname(__FILE__) . '/assets/another.txt';
+
+ $values = $option->getValue();
+ $this->assertTrue($option->isFile());
+ $this->assertCount(2, $values);
+ $this->assertTrue(in_array($file1, $values));
+ $this->assertTrue(in_array($file2, $values));
+ }
+
// Providers
public function values()
View
1 tests/Commando/assets/another.txt
@@ -0,0 +1 @@
+Another Example
View
1 tests/Commando/assets/example.txt
@@ -0,0 +1 @@
+Example Input File
View
3 tests/Commando/assets/markdown.md
@@ -0,0 +1,3 @@
+# Not a txt extension
+
+ - Markdown

0 comments on commit a139bac

Please sign in to comment.
Something went wrong with that request. Please try again.