diff --git a/classes/options.php b/classes/options.php index 41daca5..36b27f3 100644 --- a/classes/options.php +++ b/classes/options.php @@ -46,16 +46,18 @@ class options { */ const VALUE_REQUIRED = 2; + /** + * Long(Two dashes, multiple letter) Options and * Short(One dash, single letter) Options * * @var array */ - private $validShortOptions = [ + private $validOptions = [ 'f' => [ 'option' => self::OPTION_REQUIRED, 'value' => self::VALUE_REQUIRED, - 'comment' => 'Path to the file or folder to run against.', + 'comment' => '(Required) Path to the file or folder to run against.', 'description' => 'The location of the file or folder to use for generating the report. A fully qualified path is recommended. Relative paths will be based off the php7mar folder.', 'example' => '-f="/path/to/folder"' ], @@ -70,8 +72,10 @@ class options { 'option' => self::OPTION_OPTIONAL, 'value' => self::VALUE_REQUIRED, 'comment' => 'Types of tests to run.', - 'description' => 'By default all tests will run. This option allows tests to be selected using a comma delimited list. Allowable values: critical, nuance, and syntax.', + 'description' => 'By default all tests will run. This option allows tests to be selected using a comma delimited list.', 'example' => '-t="syntax,nuance"', + 'lowercase' => true, + 'comma_delimited' => true, 'allowed' => [ 'critical', 'nuance', @@ -82,18 +86,10 @@ class options { 'option' => self::OPTION_OPTIONAL, 'value' => self::VALUE_REQUIRED, 'comment' => 'File extensions to include when scanning a directory.', - 'description' => 'A comma separated list of file extensions to consider as PHP files. Defaults to "php"', + 'description' => 'A comma separated list of file extensions to consider as PHP files. Defaults to "php"', 'example' => '-x="php,inc"', 'comma_delimited' => true - ] - ]; - - /** - * Long(Two dashes, multiple letter) Options - * - * @var array - */ - private $validLongOptions = [ + ], 'php' => [ 'option' => self::OPTION_OPTIONAL, 'value' => self::VALUE_REQUIRED, @@ -128,22 +124,29 @@ class options { * @access public * @return void */ - public function __construct() { - global $argv; - - //Temporary variable so we do not override the global. - $_argv = $argv; - array_shift($_argv); - $options = $_argv; - - if (empty($options)) { + public function __construct() { + $short = ''; + $long = []; + foreach( $this->validOptions as $o => $opt ){ + if( strlen( $o ) == 1 ){ + $short .= $o .($opt['value'] == self::VALUE_REQUIRED ?':':'').($opt['value'] == self::VALUE_OPTIONAL ?'::':'' ); + }else{ + $long[] = $o .($opt['value'] == self::VALUE_REQUIRED ?':':'').($opt['value'] == self::VALUE_OPTIONAL ?'::':'' ); + } + } + $this->options = getopt( $short, $long ); + if( !$this->options ){ $this->printOptionsAndExit(); } - - foreach ($options as $option) { - $this->parseOption($option); + + try{ + $this->enforceOptions(); + }catch( \Exception $e ){ + echo "\n-------------------------------------------\n"; + echo $e->getMessage(); + echo "\n-------------------------------------------\n"; + $this->printOptionsAndExit(false); } - $this->enforceOptions(); } /** @@ -152,60 +155,34 @@ public function __construct() { * @access private * @return void */ - private function printOptionsAndExit() { + private function printOptionsAndExit( $verbose = true ) { echo "Available Options:\n"; - foreach ($this->validShortOptions as $option => $info) { - echo "-\033[1m{$option}\033[0m\n {$info['comment']}\n {$info['description']}\n Example: {$info['example']}\n\n"; - } - foreach ($this->validLongOptions as $option => $info) { - echo "--\033[1m{$option}\033[0m\n {$info['comment']}\n {$info['description']}\n Example: {$info['example']}\n\n"; - } - exit; - } - - /** - * Parse a raw option - * - * @access private - * @param string Raw option from the command line. - * @return array Option name, value if provided. - */ - private function parseOption($rawOption) { - $regex = "#^(?P