<commit_before><?php

/**
 * A command line options parser
 */
class Ergo_Console_Options
{
	private $_args, $_options=array(), $_parsed;

	/**
	 * Constructor
	 * @param array $argv
	 */
	public function __construct($argv)
	{
		$this->_args = $argv;
	}

	/**
	 * Adds options definitions to the parser. Either a string or an array of strings
	 * can be provided, with keys like the following:
	 *
	 * -v
	 * --flag
	 * --flag=withvalue
	 *
	 * Bare parameters without a prefix can be captured as :alias
	 */
	public function define($options)
	{
		if(!is_array($options)) $options = array($options);

		foreach($options as $option)
		{
			$option = $this->_parseOption($option);
			$this->_options[$option->name] = $option;
		}
	}

	/**
	 * Forces a re-parse of specific arguments
	 */
	public function parse($args)
	{
		$needsValue = false;
		$argumentPattern = "/^((?:--?|:)\w+)([*?+])?(=.+?)?$/";

		foreach(array_slice($args,1) as $arg)
		{
			if($needsValue && preg_match('/^(--?|:)/', $arg))
			{
				throw new Exception("Argument {$m[1]} needs a value");
			}
			else if(preg_match('/^(--?\w+)/', $arg, $m))
			{
				if($this->_definition($m[1])->needsValue)
				{
					$needsValue = $m[1];
				}
				else
				{
					$this->_parsed[$arg][] = null;
				}
			}
			else
			{
				if($needsValue)
				{
					$this->_parsed[$needsValue][] = $arg;
					$needsValue = false;
				}
				else if($param = $this->_nextParameter())
				{
					$this->_parsed[$param][] = $arg;
				}
				else
				{
					throw new Exception("Unknown parameter $arg");
				}
			}
		}
	}

	/**
	 * Determines if the specific key has been set
	 * @return bool
	 */
	public function has($key)
	{
		if(!isset($this->_parsed)) $this->parse($this->_args);

		foreach(func_get_args() as $arg)
			if(isset($this->_parsed[$arg])) return true;

		return false;
	}

	/**
	 * Returns a single option value if set, an exception otherwise
	 * @throws Exception
	 * @return mixed
	 */
	public function value($key)
	{
		$values = $this->values($key);
		return $values[0];
	}

	/**
	 * Returns an array of option values
	 * @throws Exception
	 * @return array
	 */
	public function values($key)
	{
		if(!isset($this->_parsed)) $this->parse($this->_args);
<commit_msg>Fixed bug where arguments with dashes broke the Options parser.<commit_after><?php

/**
 * A command line options parser
 */
class Ergo_Console_Options
{
	private $_args, $_options=array(), $_parsed;

	/**
	 * Constructor
	 * @param array $argv
	 * @param array params to pass to define
	 */
	public function __construct($argv, $define=array())
	{
		$this->_args = $argv;
		$this->define($define);
	}

	/**
	 * Adds options definitions to the parser. Either a string or an array of strings
	 * can be provided, with keys like the following:
	 *
	 * -v
	 * --flag
	 * --flag=withvalue
	 *
	 * Bare parameters without a prefix can be captured as :alias
	 */
	public function define($options)
	{
		if(!is_array($options)) $options = array($options);

		foreach($options as $option)
		{
			$option = $this->_parseOption($option);
			$this->_options[$option->name] = $option;
		}
	}

	/**
	 * Forces a re-parse of specific arguments
	 */
	public function parse($args)
	{
		$needsValue = false;
		$argumentPattern = "/^((?:--?|:)\w+)([*?+])?(=.+?)?$/";

		foreach(array_slice($args,1) as $arg)
		{
			if(!$needsValue && preg_match('/^(--?\w+)/', $arg, $m))
			{
				if($this->_definition($m[1])->needsValue)
				{
					$needsValue = $m[1];
				}
				else
				{
					$this->_parsed[$arg][] = null;
				}
			}
			else
			{
				if($needsValue)
				{
					$this->_parsed[$needsValue][] = $arg;
					$needsValue = false;
				}
				else if($param = $this->_nextParameter())
				{
					$this->_parsed[$param][] = $arg;
				}
				else
				{
					throw new Exception("Unknown parameter $arg");
				}
			}
		}
	}

	/**
	 * Determines if the specific key has been set
	 * @return bool
	 */
	public function has($key)
	{
		if(!isset($this->_parsed)) $this->parse($this->_args);

		foreach(func_get_args() as $arg)
			if(isset($this->_parsed[$arg])) return true;

		return false;
	}

	/**
	 * Returns a single option value if set, an exception otherwise
	 * @throws Exception
	 * @return mixed
	 */
	public function value($key)
	{
		$values = $this->values($key);
		return count($values) ? $values[0] : null;
	}

	/**
	 * Returns an array of option values
	 * @throws Exception
	 * @return array
	 */
	public function values($key)
	{
		if(!isset($this->_parsed)) $this->parse($this->_args);

{'fraction_non_alphanumeric': 0.1817170818505338, 'fraction_numerical': 0.002001779359430605, 'mean_word_length': 2.983170947741364, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '949667', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1511, 'n_words': 372}