function testUsage() { $parser = new Parser("Hello World", "hello"); $parser->addFlag("foo", array("alias" => "-f", "help" => "Turn on fooness")); $parser->addFlag("bar", array("has_value" => true)); $parser->addArgument("baz", array("required" => true)); $parser->addArgument("boo", array("required" => false)); $parser->addArgument("bab", array("var_arg" => true, "help" => "Bla bla bla")); $this->assertEquals(<<<EOT Usage: hello [--foo|-f] [--bar <bar>] <baz> [<boo>] [<bab> ...] Hello World EOT , $parser->usage()); $this->assertEquals(<<<EOT {$parser->usage()} Arguments: baz (required) boo bab: Bla bla bla Flags: -f, --foo: Turn on fooness --bar <bar> EOT , $parser->longUsage()); }
<?php require __DIR__ . "/../vendor/autoload.php"; use CHH\Optparse; $parser = new Optparse\Parser("Says Hello", "hello"); function usage_and_exit() { global $parser; fwrite(STDERR, "{$parser->longUsage()}\n"); exit(1); } $parser->addFlag("help", array("alias" => "-h", "help" => "Displays this help message"), "usage_and_exit"); $parser->addFlag("shout", array("alias" => "-S", "default" => false, "help" => "Prints in uppercase letters")); $parser->addArgument("name", array("required" => true, "help" => "The subject's name")); try { $parser->parse(); } catch (Optparse\Exception $e) { fwrite(STDERR, "{$e->getMessage()}\n\n"); usage_and_exit(); } $msg = "Hello {$parser["name"]}!"; if ($parser["shout"]) { $msg = strtoupper($msg); } echo "{$msg}\n";
<?php require __DIR__ . "/../vendor/autoload.php"; use CHH\Optparse; $opts = new Optparse\Parser(); $opts->addFlag("help", array("alias" => "-h"), function () use($opts) { echo "{$opts->usage()}\n"; exit(0); }); $opts->addArgument("files", array("var_arg" => true, "required" => true)); try { $opts->parse(); } catch (Optparse\Exception $e) { fwrite(STDERR, "{$e->getMessage()}\n\n"); fwrite(STDERR, "{$opts->usage()}\n"); exit(1); } foreach ($opts["files"] as $file) { readfile($file); echo "\n"; }