Permalink
Browse files

Argument parsing engine.

  • Loading branch information...
1 parent 49c59ae commit ed8b0fbcb02f6af64a7be3b26dee5e4ae2116e7f @jlogsdon committed Apr 21, 2010
Showing with 831 additions and 0 deletions.
  1. +9 −0 README.md
  2. +36 −0 example_args.php
  3. +453 −0 lib/cli/Arguments.php
  4. +135 −0 lib/cli/arguments/Argument.php
  5. +43 −0 lib/cli/arguments/InvalidArguments.php
  6. +121 −0 lib/cli/arguments/Lexer.php
  7. +34 −0 test.php
View
@@ -43,6 +43,15 @@ table rendered for visual display.
You can also explicitly set the renderer used by calling `\cli\Table::setRenderer()` and giving it an instance of one
of the concrete `\cli\table\Renderer` classes.
+Argument Parser
+---------------
+
+Argument parsing uses a simple framework for taking a list of command line arguments,
+usually straight from `$_SERVER['argv']`, and parses the input against a set of
+defined rules.
+
+Check `example_args.php` for an example.
+
Usage
-----
View
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Sample invocations:
+ *
+ * # php example_args.php -vC ./ --version
+ * {"verbose":true,"cache":".\/","version":true}
+ * # php example_args.php -vC --version
+ * PHP Warning: [cli\Arguments] no value given for -C
+ * # php example_args.php -vC multi word --version
+ * {"verbose":true,"cache":"multi word","version":true}
+ *
+ */
+
+if (php_sapi_name() != 'cli') {
+ die('Must run from command line');
+}
+
+require 'lib/cli/cli.php';
+\cli\register_autoload();
+
+$strict = in_array('--strict', $_SERVER['argv']);
+$arguments = new \cli\Arguments(compact('strict'));
+
+$arguments->addFlag(array('verbose', 'v'), 'Turn on verbose output');
+$arguments->addFlag('version', 'Turn on verbose output');
+$arguments->addFlag(array('quiet', 'q'), 'Disable all output');
+
+$arguments->addOption(array('cache', 'C'), array(
+ 'default' => __DIR__,
+ 'description' => 'Set the cache directory. Defaults to the current directory'));
+$arguments->addOption(array('name', 'n'), array(
+ 'default' => null,
+ 'description' => 'Set a name.'));
+
+$arguments->parse();
+echo $arguments->asJSON() . "\n";
Oops, something went wrong.

0 comments on commit ed8b0fb

Please sign in to comment.