Browse files

Argument support (closes #1)

  • Loading branch information...
1 parent 855196e commit 8f8d2374fd7688ec6890ec081fea978d5fa1c59f @jaz303 committed Jan 29, 2011
Showing with 68 additions and 3 deletions.
  1. +6 −0 Phakefile
  2. +17 −0 README.markdown
  3. +30 −1 lib/phake.php
  4. +15 −2 phake
View
6 Phakefile
@@ -1,4 +1,10 @@
<?php
+desc('Dump all args');
+task('args', function($app) {
+ echo "Arguments:\n";
+ foreach ($app as $k => $v) echo "$k = $v\n";
+});
+
desc('Load the application environment');
task('environment', function() {
echo "I am the outer environment. I should run first.\n";
View
17 README.markdown
@@ -73,7 +73,24 @@ Sometimes you may want to specify that some code should run before or after a ta
before('foo', function() { ... });
after('baz:bar', function() { ... });
+
+Task Arguments
+--------------
+Phake allows arguments to specified on the command line:
+
+ # Execute task `quux` with the given args
+ ./phake quux name=Jason city=Glasgow
+
+This format must be matched exactly; do not put spaces between `=` and the argument name/value. If you need to put spaces in the argument value, place the entire assignment in quotes.
+
+Arguments are made available to tasks by the application object's `ArrayAccess` implementation:
+
+ task('task_with_args', function($app) {
+ $name = $app['name'];
+ $city = $app['city'];
+ // do some stuff...
+ });
A Somewhat More Complete Example
--------------------------------
View
31 lib/phake.php
@@ -4,12 +4,14 @@
class TaskNotFoundException extends \Exception {};
class TaskCollisionException extends \Exception {};
-class Application
+class Application implements \ArrayAccess, \IteratorAggregate
{
private $root;
+ private $args;
public function __construct() {
$this->root = new Node(null, '');
+ $this->args = array();
}
public function root() {
@@ -47,6 +49,33 @@ public function get_task_list() {
public function __toString() {
return '<' . get_class($this) . '>';
}
+
+ //
+ // ArrayAccess/IteratorAggregate - for argument support
+
+ public function set_args(array $args) {
+ $this->args = $args;
+ }
+
+ public function offsetExists($k) {
+ return array_key_exists($k, $this->args);
+ }
+
+ public function offsetGet($k) {
+ return $this->args[$k];
+ }
+
+ public function offsetSet($k, $v) {
+ $this->args[$k] = $v;
+ }
+
+ public function offsetUnset($k) {
+ unset($this->args[$k]);
+ }
+
+ public function getIterator() {
+ return new \ArrayIterator($this->args);
+ }
}
class Node
View
17 phake
@@ -35,8 +35,20 @@ try {
}
}
- $non_options = $parser->get_non_options();
- if (count($non_options)) $task_names = $non_options;
+ $cli_args = array();
+ $cli_task_names = array();
+ foreach ($parser->get_non_options() as $option) {
+ if (strpos($option, '=') > 0) {
+ list($k, $v) = explode('=', $option);
+ $cli_args[$k] = $v;
+ } else {
+ $cli_task_names[] = $option;
+ }
+ }
+
+ if (count($cli_task_names)) {
+ $task_names = $cli_task_names;
+ }
//
// Locate runfile
@@ -56,6 +68,7 @@ try {
// Go, go, go
$application = phake\Builder::$global->get_application();
+ $application->set_args($cli_args);
$application->reset();
switch ($action) {

0 comments on commit 8f8d237

Please sign in to comment.