mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-11-21 17:39:09 +01:00
Improve test_form_rule command to accept JSON formatted parameters
Example: ldapsaisie \ -L LSformRule test_form_rule zxcvbn \ -p customDictionaries="$( jo dict1=/path/to/dict1.json dict2=/path/to/dict2.json )" \ -p minScore=3 \ S3cRet2TeST
This commit is contained in:
parent
5dad4e3648
commit
fee03668f5
2 changed files with 63 additions and 2 deletions
|
@ -43,6 +43,16 @@ class LScli extends LSlog_staticLoggerClass {
|
|||
*/
|
||||
private static $current_command = null;
|
||||
|
||||
/**
|
||||
* Array of custom CLI argument value with them CLI value as key and PHP value as value
|
||||
* @var array<string,mixed>
|
||||
*/
|
||||
public static $custom_cli_arg_values = [
|
||||
"null" => null,
|
||||
"true" => true,
|
||||
"false" => false,
|
||||
];
|
||||
|
||||
/**
|
||||
* Add a CLI command
|
||||
*
|
||||
|
@ -843,6 +853,57 @@ class LScli extends LSlog_staticLoggerClass {
|
|||
return $quote_char . str_replace($quote_char, "\\$quote_char", $word) . $quote_char;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse CLI argument value
|
||||
* @param string $value
|
||||
* @param array<string,mixed>|null $custom_values Extra custom CLI argument value
|
||||
* (optional, will be merged with self::$custom_cli_arg_values,
|
||||
* CLI value (=key), must be passed in lowercase)
|
||||
* @return mixed
|
||||
*/
|
||||
public static function parse_arg_value($value, $custom_values=null) {
|
||||
$custom_values = array_merge(
|
||||
self :: $custom_cli_arg_values,
|
||||
ensureIsArray($custom_values)
|
||||
);
|
||||
if ($value && in_array($value[0], ["{", "["])) {
|
||||
// With explicit type specified (format: "[type]value")
|
||||
if (preg_match("/^\[(string|str|bool|boolean|int|integer|float|array)\](.+)$/i", $value, $m)) {
|
||||
switch(strtolower($m[1])) {
|
||||
case "string":
|
||||
case "str":
|
||||
return strval($m[2]);
|
||||
case "boolean":
|
||||
case "bool":
|
||||
return boolval($m[2]);
|
||||
case "integer":
|
||||
case "int":
|
||||
return intval($m[2]);
|
||||
case "float":
|
||||
return floatval($m[2]);
|
||||
case "array":
|
||||
return ensureIsArray(
|
||||
self :: parse_arg_value($m[2])
|
||||
);
|
||||
}
|
||||
}
|
||||
// Otherwise, consider as JSON encoded value
|
||||
try {
|
||||
$value = json_decode($value, true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
catch (\JsonException $ex) {
|
||||
self :: usage("Fail to decode JSON argument '$value': ".$ex->getMessage());
|
||||
}
|
||||
}
|
||||
else if (array_key_exists(strtolower($value), $custom_values)) {
|
||||
$value = $custom_values[strtolower($value)];
|
||||
}
|
||||
else if (is_numeric($value)) {
|
||||
$value = floatval($value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -126,12 +126,12 @@ class LSformRule extends LSlog_staticLoggerClass {
|
|||
if (in_array($command_args[$i], array('-p', '--param'))) {
|
||||
$i++;
|
||||
LScli :: unquote_word($command_args[$i]);
|
||||
$param_parts = explode('=', $command_args[$i]);
|
||||
$param_parts = explode('=', $command_args[$i], 2);
|
||||
if (count($param_parts) != 2)
|
||||
LScli :: usage('Invalid parameter string ('.$command_args[$i].').');
|
||||
if (array_key_exists($param_parts[0], $params))
|
||||
LScli :: usage('Parameter "'.$param_parts[0].'" already specified.');
|
||||
$params[$param_parts[0]] = $param_parts[1];
|
||||
$params[$param_parts[0]] = LScli :: parse_arg_value($param_parts[1]);
|
||||
}
|
||||
else if (is_null($rule_name)) {
|
||||
$rule_name = $command_args[$i];
|
||||
|
|
Loading…
Reference in a new issue