Skip to content

Commit

Permalink
Add help command
Browse files Browse the repository at this point in the history
  • Loading branch information
FO-nTTaX committed May 13, 2022
1 parent 212875d commit 41c07f3
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ composer sqllint
### Parameter
| Name | Values |
|----------|--------------------------|
| --help | |
| --report | "cli" (default), "junit" |

[1]:https://github.com/phpmyadmin/sql-parser
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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" ],
Expand Down
125 changes: 125 additions & 0 deletions src/Parameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare( strict_types=1 );

namespace Liquipedia\SqlLint;

class Parameters {

private const PARAMETERS = [
'help' => [
'description' => 'Displays this help command',
],
'report' => [
'values' => [
'cli',
'junit',
],
'description' => 'Defines the output formatter',
],
];

/**
* @var array<string, string>|null
*/
private static $stringParameters = null;

/**
* @var array<string, bool>|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 ];
}
}
}
}
}

}
26 changes: 9 additions & 17 deletions src/SqlLint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}

0 comments on commit 41c07f3

Please sign in to comment.