diff --git a/README.md b/README.md index d41773a..e2b0a3c 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ composer sqllint ### Parameter | Name | Values | |----------|--------------------------| +| --help | | | --report | "cli" (default), "junit" | [1]:https://github.com/phpmyadmin/sql-parser diff --git a/composer.json b/composer.json index f37268d..8d890cf 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "liquipedia/sqllint", "description": "A thin wrapper around the SqlParser from the phpMyAdmin project which can be used to lint any amount of sql files from the command line.", - "version": "1.1.2", + "version": "1.1.3", "type": "library", "license": "GPL-2.0-or-later", "keywords": [ "sql", "lint", "junit" ], diff --git a/src/Parameters.php b/src/Parameters.php new file mode 100644 index 0000000..121a50d --- /dev/null +++ b/src/Parameters.php @@ -0,0 +1,125 @@ + [ + 'description' => 'Displays this help command', + ], + 'report' => [ + 'values' => [ + 'cli', + 'junit', + ], + 'description' => 'Defines the output formatter', + ], + ]; + + /** + * @var array|null + */ + private static $stringParameters = null; + + /** + * @var array|null + */ + private static $boolParameters = null; + + /** + * @param string $parameter + * @param string $default + * @return string + */ + public static function get( string $parameter, string $default ): string { + self::initialiseCliParamsMaybe(); + + if ( self::$stringParameters !== null && array_key_exists( $parameter, self::$stringParameters ) ) { + if ( + array_key_exists( 'values', self::PARAMETERS[ $parameter ] ) + && in_array( self::$stringParameters[ $parameter ], self::PARAMETERS[ $parameter ][ 'values' ] ) + ) { + return self::$stringParameters[ $parameter ]; + } else { + $message = + PHP_EOL . 'ERROR: Unknown parameter "' . $parameter . '"'; + if ( array_key_exists( 'values', self::PARAMETERS[ $parameter ] ) ) { + $message .= ', should be one of' + . ' "' . implode( '", "', self::PARAMETERS[ $parameter ][ 'values' ] ) . '"'; + } + $message .= PHP_EOL . PHP_EOL; + die( $message ); + } + } elseif ( array_key_exists( $parameter, self::PARAMETERS ) ) { + return $default; + } else { + die( + PHP_EOL . 'ERROR: Unknown parameter "' . $parameter . '"' . PHP_EOL . PHP_EOL + ); + } + } + + /** + * @param string $parameter + * @param bool $default + * @return bool + */ + public static function getBool( string $parameter, bool $default ): bool { + self::initialiseCliParamsMaybe(); + + if ( self::$boolParameters !== null && array_key_exists( $parameter, self::$boolParameters ) ) { + return true; + } elseif ( array_key_exists( $parameter, self::PARAMETERS ) ) { + return $default; + } else { + return false; + } + } + + public static function displayHelp(): void { + $parameterMaxLength = max( array_map( 'strlen', array_keys( self::PARAMETERS ) ) ); + $parameterSpacer = $parameterMaxLength + 6; + $help = 'Available parameters:' . PHP_EOL . PHP_EOL; + + foreach ( self::PARAMETERS as $key => $value ) { + $help .= ' --' . $key . str_repeat( ' ', $parameterMaxLength - strlen( $key ) + 3 ) + . $value[ 'description' ] . PHP_EOL; + if ( array_key_exists( 'values', $value ) ) { + $help .= str_repeat( ' ', $parameterSpacer ) + . 'Values: One of "' . implode( '", "', $value[ 'values' ] ) . '"' . PHP_EOL + . str_repeat( ' ', $parameterSpacer ) + . 'Default: "' . $value[ 'values' ][ 0 ] . '"' . PHP_EOL; + } + } + echo $help; + } + + private static function initialiseCliParamsMaybe(): void { + if ( self::$stringParameters === null ) { + $params = []; + foreach ( self::PARAMETERS as $key => $value ) { + $paramType = $key; + if ( array_key_exists( 'values', $value ) ) { + $paramType .= '::'; + } + $params[] = $paramType; + } + $opts = getopt( '', $params ); + foreach ( self::PARAMETERS as $key => $value ) { + if ( array_key_exists( $key, $opts ) ) { + if ( is_array( $opts[ $key ] ) ) { + die( PHP_EOL . 'ERROR: More than one value for "' . $key . '"' . PHP_EOL . PHP_EOL ); + } elseif ( is_string( $opts[ $key ] ) ) { + self::$stringParameters[ $key ] = $opts[ $key ]; + } elseif ( is_bool( $opts[ $key ] ) ) { + self::$boolParameters[ $key ] = $opts[ $key ]; + } + } + } + } + } + +} diff --git a/src/SqlLint.php b/src/SqlLint.php index b08f4ea..2573717 100644 --- a/src/SqlLint.php +++ b/src/SqlLint.php @@ -12,24 +12,16 @@ class SqlLint { * @return int */ public static function lint(): int { - $args = getopt( '', [ 'report::' ] ); - $reportType = 'cli'; - if ( array_key_exists( 'report', $args ) ) { - if ( array_key_exists( strval( $args[ 'report' ] ), IReport::REPORT_TYPES ) ) { - $reportType = strval( $args[ 'report' ] ); - } else { - die( - PHP_EOL . 'ERROR: Unknown report type "' . strval( $args[ 'report' ] ) . '",' - . ' should be one of "' . implode( '", "', array_keys( IReport::REPORT_TYPES ) ) . '"' - . PHP_EOL . PHP_EOL - ); - } - } - $reportClass = IReport::REPORT_TYPES[ $reportType ]; + if ( Parameters::getBool( 'help', false ) ) { + Parameters::displayHelp(); + return 0; + } else { + $reportClass = IReport::REPORT_TYPES[ Parameters::get( 'report', 'cli' ) ]; - $report = new $reportClass; - $runner = new Runner( $report ); - return $runner->run(); + $report = new $reportClass; + $runner = new Runner( $report ); + return $runner->run(); + } } }