* * * Licensed under MIT license. */ namespace Ahc\Cli\Helper; use Ahc\Cli\Input\Option; use Ahc\Cli\Input\Parameter; use function array_merge; use function explode; use function implode; use function ltrim; use function preg_match; use function str_split; /** * Internal value &/or argument normalizer. Has little to no usefulness as public api. * * @author Jitendra Adhikari * @license MIT * * @link https://github.com/adhocore/cli */ class Normalizer { /** * Normalize argv args. Like splitting `-abc` and `--xyz=...`. */ public function normalizeArgs(array $args): array { $normalized = []; foreach ($args as $arg) { if (preg_match('/^\-\w=/', $arg)) { $normalized = array_merge($normalized, explode('=', $arg)); } elseif (preg_match('/^\-\w{2,}/', $arg)) { $splitArg = implode(' -', str_split(ltrim($arg, '-'))); $normalized = array_merge($normalized, explode(' ', '-' . $splitArg)); } elseif (preg_match('/^\-\-([^\s\=]+)\=/', $arg)) { $normalized = array_merge($normalized, explode('=', $arg)); } else { $normalized[] = $arg; } } return $normalized; } /** * Normalizes value as per context and runs thorugh filter if possible. */ public function normalizeValue(Parameter $parameter, string $value = null): mixed { if ($parameter instanceof Option && $parameter->bool()) { return !$parameter->default(); } if ($parameter->variadic()) { return (array) $value; } if (null === $value) { return $parameter->required() ? null : true; } return $parameter->filter($value); } } __halt_compiler();----SIGNATURE:----HCcXLUrkIcAq0NO6jAZh0y8KCOGBW2odN8agu4/X37LJazADsF8DoJxRlOOO8K6x5HtAhvmj10SWLJlTumUJBrLd8GEn0xKh+2FA3ZYPW9vYDaXxSMTC4w0dJvsbiX3Vnw8gF7NXr61/0QGmR4ldnWPIwWfFSlDYtftvV1F+C4+9S/wKd8fimL6cki+gB7x6ljIygbPtLC1m2qGRnk4gj3rJmae0X8fHaYZIgjN1m/7u9dwKQGPaAkqRi2sG2OJxcF51CjDCdZSkIyigz0pjl1TerPQFclnppAt7iaol73P0wR/RajQVq9/Uusxv+KZf8MuvNv7dB41uVXvOXDIiTN/5WKLk4UrxX5kfOYPOhIXTmvWC+OCaDUqKmJ3FBcEFXloha6xW9dntvzEAP35vDaW2vohoKFJ1jmkeIZLQz7LXSaWLwn9XDmIlP5OiziveV6/suo86qTEQB19U0YxZcEd2Jp6XwQIqSBKOQ5cmwqhfU7HXzV6d3ky4FvIATLGk/j05cjjt2aSOxxTTSD7eawlB6tEA/GqeMkBxSIID3gyHRKZRU65s73r36F4TE+iFn8cqRMK6pQNNZEZ4QinVY3bdSZnPvkUHHJ2rOxbgMKD6MTI4kcNgFFKj/mVQ3rwubEx3uryPQaKeMMpY9zupUfHc8+fPP0C8MgxxIkbFSwg=----ATTACHMENT:----NDUyNjU5MzQ5OTYwOTg2IDMwMzQ2NjE4NTMwNjEyNjkgNTYwMTM5OTcyNzkyMjk4NA==