From 227b28eadce232c2b5ede873a38a6342963bb467 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Wed, 17 Jul 2013 17:25:56 +0000 Subject: allow defaults in it::getopt --- it.class | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'it.class') diff --git a/it.class b/it.class index 18f9ffc..1fb95cc 100644 --- a/it.class +++ b/it.class @@ -655,7 +655,7 @@ static function imageconvert($p) * -x EXTENSION Ignore EXTENSION * Mandatory arguments from the Usage: line are returned under their (lowercased!) name. * All non-option arguments are returned in 'args' - * Option text must be indented; if long and short option present, value is stored in long option + * Option text must be indented; if long and short option present, value is stored in long one. Defaults in [ ] at end of line * Options without arguments store true or false under their key * Options -h and --help will be handled internally by printing usage and exiting * When printing, the usage will be de-indented so the first line starts in the first column @@ -690,6 +690,9 @@ static function getopt($helplines) list($shortoptname, $shortoptarg) = $matches; } + if (($longoptarg || $shortoptarg) && ($default = it::match('\[(.*)\]\s*$', $helpline))) + $defaults[$longoptname ? $longoptname : $shortoptname] = it::replace(array('^default:?\s*' => ""), trim($default)); + if ($longoptname || $shortoptname) { if ($longoptname && ($shortoptname || $shortoptname === "0")) @@ -767,7 +770,7 @@ static function getopt($helplines) if ($result['verbose']) $GLOBALS['debug_verbose']++; - return $result; + return $result + (array)$defaults; } static function _stdin_next() -- cgit v1.2.3