From 2f8c656e1df10984527fe445bd57783f5fdcafdc Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 28 Jun 2009 16:12:45 -0400 Subject: command line arg handling a little more flexible --- scripts/commandline.inc | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'scripts/commandline.inc') diff --git a/scripts/commandline.inc b/scripts/commandline.inc index 4a7757fb9..8c5c56d5e 100644 --- a/scripts/commandline.inc +++ b/scripts/commandline.inc @@ -115,24 +115,60 @@ require_once INSTALLDIR . '/lib/common.php'; set_error_handler('common_error_handler'); -function have_option($str) +function have_option($opt, $alt=null) { global $options; + + $matches = array($opt); + + if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) { + $matches[] = '--'.$opt; + } else { + $matches[] = $opt; + } + + if (!empty($alt)) { + if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) { + $matches[] = '--'.$alt; + } else { + $matches[] = $alt; + } + } + foreach ($options as $option) { - if ($option[0] == $str) { + if (in_array($option[0], $matches)) { return true; } } + return false; } -function get_option_value($str) +function get_option_value($str, $alt=null) { global $options; + + $matches = array(); + + if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) { + $matches[] = '--'.$opt; + } else { + $matches[] = $opt; + } + + if (!empty($alt)) { + if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) { + $matches[] = '--'.$alt; + } else { + $matches[] = $alt; + } + } + foreach ($options as $option) { - if ($option[0] == $str) { + if (in_array($option[0], $matches)) { return $option[1]; } } + return null; } \ No newline at end of file -- cgit v1.2.3-54-g00ecf From c755970141810a98e08f47c52568f635167e34f1 Mon Sep 17 00:00:00 2001 From: Super-User Date: Sun, 28 Jun 2009 20:15:17 +0000 Subject: commandline processing handles errors better --- scripts/commandline.inc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'scripts/commandline.inc') diff --git a/scripts/commandline.inc b/scripts/commandline.inc index 4a7757fb9..53b9a490b 100644 --- a/scripts/commandline.inc +++ b/scripts/commandline.inc @@ -63,7 +63,14 @@ if (isset($longoptions)) { $parser = new Console_Getopt(); -list($options, $args) = $parser->getopt($argv, $shortoptions, $longoptions); +$result = $parser->getopt($argv, $shortoptions, $longoptions); + +if (PEAR::isError($result)) { + print $result->getMessage()."\n"; + exit(1); +} else { + list($options, $args) = $result; +} function show_help() { -- cgit v1.2.3-54-g00ecf From 7a0d33ab5fa29993cc7e05e41a26a26ca3ffd1e8 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 28 Jun 2009 16:55:19 -0400 Subject: reformat commandline.inc --- scripts/commandline.inc | 120 ++++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 60 deletions(-) (limited to 'scripts/commandline.inc') diff --git a/scripts/commandline.inc b/scripts/commandline.inc index 9c6787cb7..bca09216d 100644 --- a/scripts/commandline.inc +++ b/scripts/commandline.inc @@ -66,10 +66,10 @@ $parser = new Console_Getopt(); $result = $parser->getopt($argv, $shortoptions, $longoptions); if (PEAR::isError($result)) { - print $result->getMessage()."\n"; - exit(1); + print $result->getMessage()."\n"; + exit(1); } else { - list($options, $args) = $result; + list($options, $args) = $result; } function show_help() @@ -77,7 +77,7 @@ function show_help() global $helptext; $_default_help_text = << 1 && 0 != strncmp($opt, '--', 2)) { - $matches[] = '--'.$opt; - } else { - $matches[] = $opt; - } - - if (!empty($alt)) { - if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) { - $matches[] = '--'.$alt; - } else { - $matches[] = $alt; - } - } - - foreach ($options as $option) { - if (in_array($option[0], $matches)) { - return true; - } - } - - return false; + global $options; + + $matches = array(); + + if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) { + $matches[] = '--'.$opt; + } else { + $matches[] = $opt; + } + + if (!empty($alt)) { + if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) { + $matches[] = '--'.$alt; + } else { + $matches[] = $alt; + } + } + + foreach ($options as $option) { + if (in_array($option[0], $matches)) { + return true; + } + } + + return false; } function get_option_value($str, $alt=null) { - global $options; - - $matches = array(); - - if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) { - $matches[] = '--'.$opt; - } else { - $matches[] = $opt; - } - - if (!empty($alt)) { - if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) { - $matches[] = '--'.$alt; - } else { - $matches[] = $alt; - } - } - - foreach ($options as $option) { - if (in_array($option[0], $matches)) { - return $option[1]; - } - } - - return null; -} \ No newline at end of file + global $options; + + $matches = array(); + + if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) { + $matches[] = '--'.$opt; + } else { + $matches[] = $opt; + } + + if (!empty($alt)) { + if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) { + $matches[] = '--'.$alt; + } else { + $matches[] = $alt; + } + } + + foreach ($options as $option) { + if (in_array($option[0], $matches)) { + return $option[1]; + } + } + + return null; +} -- cgit v1.2.3-54-g00ecf From 3ac6b7d120dff2d2b1c35e65559aa1613f5b02dd Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 28 Jun 2009 17:02:31 -0400 Subject: error in get_option_value wasn't returning a value --- scripts/commandline.inc | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'scripts/commandline.inc') diff --git a/scripts/commandline.inc b/scripts/commandline.inc index bca09216d..3b6ef6098 100644 --- a/scripts/commandline.inc +++ b/scripts/commandline.inc @@ -122,10 +122,8 @@ require_once INSTALLDIR . '/lib/common.php'; set_error_handler('common_error_handler'); -function have_option($opt, $alt=null) +function _make_matches($opt, $alt) { - global $options; - $matches = array(); if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) { @@ -142,6 +140,15 @@ function have_option($opt, $alt=null) } } + return $matches; +} + +function have_option($opt, $alt=null) +{ + global $options; + + $matches = _make_matches($opt, $alt); + foreach ($options as $option) { if (in_array($option[0], $matches)) { return true; @@ -151,25 +158,11 @@ function have_option($opt, $alt=null) return false; } -function get_option_value($str, $alt=null) +function get_option_value($opt, $alt=null) { global $options; - $matches = array(); - - if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) { - $matches[] = '--'.$opt; - } else { - $matches[] = $opt; - } - - if (!empty($alt)) { - if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) { - $matches[] = '--'.$alt; - } else { - $matches[] = $alt; - } - } + $matches = _make_matches($opt, $alt); foreach ($options as $option) { if (in_array($option[0], $matches)) { -- cgit v1.2.3-54-g00ecf