<commit_before><?php

/**
 * @copyright <NAME>
 * @license   http://www.makoframework.com/license
 */

namespace mako\cli\input\arguments;

use mako\cli\input\arguments\exceptions\ArgumentException;
use mako\cli\input\arguments\exceptions\InvalidArgumentException;
use mako\cli\input\arguments\exceptions\MissingArgumentException;
use mako\cli\input\arguments\exceptions\UnexpectedValueException;
use mako\common\traits\SuggestionTrait;
use RuntimeException;

use function array_keys;
use function array_shift;
use function current;
use function explode;
use function preg_match;
use function strlen;
use function strpos;
use function substr;
use function vsprintf;

/**
 * Argument parser.
 *
 * @author <NAME>
 */
class ArgvParser
{
	use SuggestionTrait;

	/**
	 * Regex that matches integers.
	 *
	 * @var string
	 */
	const INT_REGEX = '/^([+-]?[1-9]\d*|0)$/';

	/**
	 * Regex that matches floats.
	 *
	 * @var string
	 */
	const FLOAT_REGEX = '/^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$/';

	/**
	 * Argv.
	 *
	 * @var array
	 */
	protected $argv;

	/**
	 * Arguments.
	 *
	 * @var array
	 */
	protected $arguments = [];

	/**
	 * Map.
	 *
	 * @var array
	 */
	protected $map = [];

	/**
	 * Positional arguments.
	 *
	 * @var array
	 */
	protected $positionals = [];

	/**
	 * Parsed arguments.
	 *
	 * @var array
	 */
	protected $parsed = [];

	/**
	 * Should unknown arguments be ignored?
	 *
	 * @var bool
	 */
	protected $ignoreUnknownArguments = false;

	/**
	 * Constructor.
	 *
	 * @param array $argv      Argv
	 * @param array $arguments Array of arguments
	 */
	public function __construct(array $argv, array $arguments = [])
	{
		$this->argv = $argv;

	   $this->addArguments($arguments);
	}

	/**
	 * Returns the registered arguments.
	 *
	 * @return array
	 */
	public function getArguments(): array
	{
		return $this->arguments;
	}

	/**
	 * Tries to find a suggestion for the invalid argument name.
	 *
	 * @param  string      $name Invalid argument name
	 * @return string|null
	 */
	protected function findArgumentSuggestion(string $name): ?string
	{
		return $this->suggest($name, array_keys($this->map));
	}

	/**
	 * Returns an argument based on its name.
	 *
	 * @param  string                                  $name Argument name
	 * @return \mako\cli\input\arguments\Argument|null
	 */
	protected function getArgument(string $name): ?Argument
	{
		if(!isset($this->map[$name]))
		{
			if($this->ignoreUnknownArguments === false)
			{
				throw new InvalidArgumentException(vsprintf('Unknown argument [ %s ].', [$name]), $this->findArgumentSuggestion($name));
			}

			return null;
		}

		return $this->arguments[$this->map[$name]];
	}

	/**
	 * Clears the parsed argument cache.
	 *
	 * @return $this
	 */
	public function clearCache()
	{
		$this->parsed = [];

		return $this;
	}

	/**
	 * Add argument.<commit_msg>Cleaning up<commit_after><?php

/**
 * @copyright <NAME>
 * @license   http://www.makoframework.com/license
 */

namespace mako\cli\input\arguments;

use mako\cli\input\arguments\exceptions\ArgumentException;
use mako\cli\input\arguments\exceptions\InvalidArgumentException;
use mako\cli\input\arguments\exceptions\MissingArgumentException;
use mako\cli\input\arguments\exceptions\UnexpectedValueException;
use mako\common\traits\SuggestionTrait;
use RuntimeException;

use function array_keys;
use function array_shift;
use function current;
use function explode;
use function preg_match;
use function strlen;
use function strpos;
use function substr;
use function vsprintf;

/**
 * Argument parser.
 *
 * @author <NAME>
 */
class ArgvParser
{
	use SuggestionTrait;

	/**
	 * Regex that matches integers.
	 *
	 * @var string
	 */
	const INT_REGEX = '/^([+-]?[1-9]\d*|0)$/';

	/**
	 * Regex that matches floats.
	 *
	 * @var string
	 */
	const FLOAT_REGEX = '/^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$/';

	/**
	 * Argv.
	 *
	 * @var array
	 */
	protected $argv;

	/**
	 * Arguments.
	 *
	 * @var array
	 */
	protected $arguments = [];

	/**
	 * Map.
	 *
	 * @var array
	 */
	protected $map = [];

	/**
	 * Positional arguments.
	 *
	 * @var array
	 */
	protected $positionals = [];

	/**
	 * Parsed arguments.
	 *
	 * @var array
	 */
	protected $parsed = [];

	/**
	 * Should unknown arguments be ignored?
	 *
	 * @var bool
	 */
	protected $ignoreUnknownArguments = false;

	/**
	 * Constructor.
	 *
	 * @param array $argv      Argv
	 * @param array $arguments Array of arguments
	 */
	public function __construct(array $argv, array $arguments = [])
	{
		$this->argv = $argv;

	   $this->addArguments($arguments);
	}

	/**
	 * Returns the registered arguments.
	 *
	 * @return array
	 */
	public function getArguments(): array
	{
		return $this->arguments;
	}

	/**
	 * Tries to find a suggestion for the invalid argument name.
	 *
	 * @param  string      $name Invalid argument name
	 * @return string|null
	 */
	protected function findArgumentSuggestion(string $name): ?string
	{
		return $this->suggest($name, array_keys($this->map));
	}

	/**
	 * Returns an argument based on its name.
	 *
	 * @param  string                                  $name Argument name
	 * @return \mako\cli\input\arguments\Argument|null
	 */
	protected function getArgument(string $name): ?Argument
	{
		if(!isset($this->map[$name]))
		{
			if($this->ignoreUnknownArguments === false)
			{
				throw new InvalidArgumentException(vsprintf('Unknown argument [ %s ].', [$name]), $this->findArgumentSuggestion($name));
			}

			return null;
		}

		return $this->arguments[$this->map[$name]];
	}

	/**
	 * Clears the parsed argument cache.
	 *
	 * @return \mako\cli\input\arguments\ArgvParser
	 */
	public function clearCache(): ArgvParser
	{
		$this->parsed = [];

		return $this;
	}

	/**
	 * Add argument.
{'fraction_non_alphanumeric': 0.15609159237895473, 'fraction_numerical': 0.003146303093864709, 'mean_word_length': 3.0754985754985755, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '664076', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1797, 'n_words': 431}