diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-03-14 05:42:27 +0000 |
---|---|---|
committer | <> | 2013-04-03 16:25:08 +0000 |
commit | c4dd7a1a684490673e25aaf4fabec5df138854c4 (patch) | |
tree | 4d57c44caae4480efff02b90b9be86f44bf25409 /scripts/dev/generate-phpt/src/setup/gtCommandLineOptions.php | |
download | php2-master.tar.gz |
Imported from /home/lorry/working-area/delta_php2/php-5.4.13.tar.bz2.HEADphp-5.4.13master
Diffstat (limited to 'scripts/dev/generate-phpt/src/setup/gtCommandLineOptions.php')
-rw-r--r-- | scripts/dev/generate-phpt/src/setup/gtCommandLineOptions.php | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/scripts/dev/generate-phpt/src/setup/gtCommandLineOptions.php b/scripts/dev/generate-phpt/src/setup/gtCommandLineOptions.php new file mode 100644 index 0000000..0e4d878 --- /dev/null +++ b/scripts/dev/generate-phpt/src/setup/gtCommandLineOptions.php @@ -0,0 +1,98 @@ +<?php + +/** + * Parse command line options + * + */ +class gtCommandLineOptions { + + protected $shortOptions = array( + 'b', + 'e', + 'v', + 'h', + ); + + protected $shortOptionsWithArgs = array( + 'c', + 'm', + 'f', + 'i', + 's', + 'x', + 'k', + ); + + protected $options; + + protected function isShortOption($arg) + { + return (substr($arg, 0, 1) == '-') && (substr($arg, 1, 1) != '-'); + } + + public function isValidOptionArg($array, $index) { + if (!isset($array[$index])) + { + return false; + } + return substr($array[$index], 0, 1) != '-'; + } + + + public function parse($argv) + { + if(count($argv) < 2) { + throw new gtMissingOptionsException('Command line options are required'); + } + + for ($i=1; $i<count($argv); $i++) { + + if ($this->isShortOption($argv[$i])) { + $option = substr($argv[$i], 1); + } else { + throw new gtUnknownOptionException('Unrecognised command line option ' . $argv[$i]); + } + + if (!in_array($option, array_merge($this->shortOptions, $this->shortOptionsWithArgs))) + { + throw new gtUnknownOptionException('Unknown option ' . $argv[$i]); + } + + if (in_array($option, $this->shortOptions)) { + $this->options[$option] = true; + continue; + } + + if (!$this->isValidOptionArg($argv, $i + 1)) + { + throw new gtMissingArgumentException('Missing argument for command line option ' . $argv[$i]); + } + + $i++; + $this->options[$option] = $argv[$i]; + } + } + + /** + * + */ + public function getOption($option) + { + if (!isset($this->options[$option])) { + return false; + } + return $this->options[$option]; + } + + + /** + * Check whether an option exists + */ + public function hasOption($option) + { + return isset($this->options[$option]); + } + + +} +?>
\ No newline at end of file |