svn: /pear/peardoc/trunk/en/package/console/ console-getopt/examples/associative.php console-getopt.xml

[email protected] (Christian Weiske)
Newsgroups php.pear.doc
Message-ID <[email protected]>
cweiske                                  Thu, 15 Oct 2009 07:16:31 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=289658

Log:
fix bug #13902: User note that is a documentation problem

Bug: http://pear.php.net/bugs/13902 (unknown) 
      
Changed paths:
    A   pear/peardoc/trunk/en/package/console/console-getopt/examples/
    A   pear/peardoc/trunk/en/package/console/console-getopt/examples/associative.php
    U   pear/peardoc/trunk/en/package/console/console-getopt.xml

Added: pear/peardoc/trunk/en/package/console/console-getopt/examples/associative.php
===================================================================
--- pear/peardoc/trunk/en/package/console/console-getopt/examples/associative.php	                        (rev 0)
+++ pear/peardoc/trunk/en/package/console/console-getopt/examples/associative.php	2009-10-15 07:16:31 UTC (rev 289658)
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Example how to get a key-value pair array
+ * from command line parameters with Console_Getopt.
+ *
+ * @link http://pear.php.net/bugs/bug.php?id=13902
+ */
+require_once 'Console/Getopt.php';
+
+/**
+ * Make a key-value array.
+ * Since Console_Getopt does not provide such a method,
+ * we implement it ourselves.
+ *
+ * @params array $params Array of parameters from Console_Getopt::getopt2()
+ *
+ * @return array key-value pair array
+ */
+function &condense_arguments($params)
+{
+    $new_params = array();
+    foreach ($params[0] as $param) {
+        $new_params[$param[0]] = $param[1];
+    }
+    return $new_params;
+}
+
+
+$cg = new Console_Getopt();
+$args = $cg->readPHPArgv();
+array_shift($args);
+
+$shortOpts = 'u:g:';
+$longOpts  = array('user=', 'group=');
+
+$params = $cg->getopt2($args, $shortOpts, $longOpts);
+if (PEAR::isError($params)) {
+    echo 'Error: ' . $params->getMessage() . "\n";
+    exit(1);
+}
+
+var_dump(condense_arguments($params));
+/*
+When called as follows:
+ associative.php -u jason -g argonauts
+you will get this output:
+ array(2) {
+   ["u"]=>
+   string(5) "jason"
+   ["g"]=>
+   string(9) "argonauts"
+ }
+ */
+?>
\ No newline at end of file

Modified: pear/peardoc/trunk/en/package/console/console-getopt.xml
===================================================================
--- pear/peardoc/trunk/en/package/console/console-getopt.xml	2009-10-15 07:15:56 UTC (rev 289657)
+++ pear/peardoc/trunk/en/package/console/console-getopt.xml	2009-10-15 07:16:31 UTC (rev 289658)
@@ -11,9 +11,9 @@
  </info>

  <chapter>
- &package.console.console-getopt.intro-options;
- &package.console.console-getopt.getopt;
- &package.console.console-getopt.readphpargv;
-
-  </chapter>
+  &package.console.console-getopt.intro-options;
+  &package.console.console-getopt.getopt;
+  &package.console.console-getopt.readphpargv;
+ </chapter>
+ &package.console.console-getopt.examples;
 </book>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.