switchified console entry point
This commit is contained in:
		
							parent
							
								
									274b333649
								
							
						
					
					
						commit
						ad12bcd557
					
				| @ -62,13 +62,20 @@ | ||||
| 	class CliSwitchCollection implements IteratorAggregate { | ||||
| 		 | ||||
| 		private $switches; | ||||
| 		private $switchArg; | ||||
| 		 | ||||
| 		public function __construct() { | ||||
| 			$this->switches = array(); | ||||
| 			$this->switchArg = null; | ||||
| 		} | ||||
| 		 | ||||
| 		public function addSwitch(CliSwitch $switch) { | ||||
| 			if ($switch->longName === null) { | ||||
| 				$this->switchArg = $switch; | ||||
| 			} else { | ||||
| 				$this->switches[] = $switch; | ||||
| 			} | ||||
| 			 | ||||
| 			return $this; | ||||
| 		} | ||||
| 		 | ||||
| @ -98,6 +105,10 @@ | ||||
| 			return $switches; | ||||
| 		} | ||||
| 		 | ||||
| 		public function getSwitchArg() { | ||||
| 			return $this->switchArg; | ||||
| 		} | ||||
| 		 | ||||
| 		public function getIterator() { | ||||
| 			return new ArrayIterator($this->switches); | ||||
| 		} | ||||
| @ -152,8 +163,6 @@ | ||||
| 				//echo $length . "\n";
 | ||||
| 				$this->maxSwitchLength = max($this->maxSwitchLength, $length); | ||||
| 			} | ||||
| 			 | ||||
| 			//echo $this->maxSwitchLength; exit;
 | ||||
| 		} | ||||
| 		 | ||||
| 		public function __toString() { | ||||
| @ -162,8 +171,11 @@ | ||||
| 			$usage .= (!empty($this->copyright)) ? '  ' . $this->copyright . "\n" : ''; | ||||
| 			$usage .= "\n"; | ||||
| 			 | ||||
| 			$usage .= $this->description; | ||||
| 			$usage .= "\n\n"; | ||||
| 			 | ||||
| 			$usage .= "USAGE\n"; | ||||
| 			$usageData = '  php ' . $this->script; | ||||
| 			$usageData = '  ' . $this->script; | ||||
| 			 | ||||
| 			$switchData = "REQUIRED\n"; | ||||
| 			 | ||||
| @ -185,6 +197,14 @@ | ||||
| 				$switchData .= $x; | ||||
| 			} | ||||
| 			 | ||||
| 			$arg = $this->switches->getSwitchArg(); | ||||
| 			if ($arg !== null) { | ||||
| 				$x = '  ' . $arg->value; | ||||
| 				$x .= str_repeat(' ' , $this->maxSwitchLength - strlen($x)); | ||||
| 				$x .= wordwrap($arg->description, self::LINE_LENGTH - strlen($x), "\n" . str_repeat(' ', strlen($x))) . "\n"; | ||||
| 				$switchData .= $x; | ||||
| 			} | ||||
| 			 | ||||
| 			$switchData .= "\nOPTIONAL\n"; | ||||
| 			foreach ($switches['optional'] as $switch) { | ||||
| 				$usageData .= ' [--' . $switch->longName; | ||||
| @ -203,8 +223,11 @@ | ||||
| 				$switchData .= $x; | ||||
| 			} | ||||
| 			 | ||||
| 			$usage .= wordwrap($usageData, self::LINE_LENGTH - 2, "\n  "); | ||||
| 			if ($arg !== null) { | ||||
| 				$usageData .= ' ' . $arg->value; | ||||
| 			} | ||||
| 			 | ||||
| 			$usage .= wordwrap($usageData, self::LINE_LENGTH - 2, "\n  "); | ||||
| 			$usage .= "\n\n" . $switchData . "\n"; | ||||
| 			 | ||||
| 			return $usage; | ||||
|  | ||||
| @ -3,13 +3,47 @@ | ||||
| 	require_once 'cli.php'; | ||||
| 	require_once dirname(dirname(__FILE__)) . '/bootstrap.php'; | ||||
| 	 | ||||
| 	function usage() { | ||||
| 		$usage = new Usage( | ||||
| 			Product::NAME . ' ' . Product::VERSION . ' (' . Product::DATE . ')', | ||||
| 			'tunit', | ||||
| 			'Command line test runner', | ||||
| 			'Tommy Montgomery', | ||||
| 			'2009' | ||||
| 		); | ||||
| 		 | ||||
| 		global $switches; | ||||
| 		$usage->setSwitches($switches); | ||||
| 		 | ||||
| 		echo $usage; | ||||
| 	} | ||||
| 	 | ||||
| 	global $switches; | ||||
| 	$switches = new CliSwitchCollection(); | ||||
| 	$switches->addSwitch(new CliSwitch(null, null, true, '<files>', 'Files and/or directories to parse for test cases')) | ||||
| 	         ->addSwitch(new CliSwitch('help', 'h', false, null, 'Display this help message (--usage is an alias)')) | ||||
| 	         ->addSwitch(new CliSwitch('recursive', null, false, null, 'Recurse into subdirectories')); | ||||
| 
 | ||||
| 	array_shift($argv); | ||||
| 	$args = Cli::parseArgs($argv, $switches); | ||||
| 	 | ||||
| 	if (isset($args['help'])) { | ||||
| 		usage(); | ||||
| 		exit(0); | ||||
| 	} | ||||
| 	 | ||||
| 	if (empty($args['args'])) { | ||||
| 		usage(); | ||||
| 		exit(1); | ||||
| 	} | ||||
| 	 | ||||
| 	//accumulate tests
 | ||||
| 	$tests = TestAccumulator::getTests($args['args'], true); | ||||
| 	$tests = TestAccumulator::getTests($args['args'], isset($args['recursive'])); | ||||
| 	 | ||||
| 	if (empty($tests)) { | ||||
| 		fwrite(STDERR, 'Found no TestCase subclasses in the given files'); | ||||
| 		exit(1); | ||||
| 	} | ||||
| 	 | ||||
| 	$runner = new TestRunner(array(new TestSuite('Main Test Suite', $tests)), array(new ConsoleListener(/*ConsoleListener::VERBOSITY_HIGH*/))); | ||||
| 	$runner->run(); | ||||
|  | ||||
| @ -19,7 +19,7 @@ | ||||
| 	function usage() { | ||||
| 		$usage = new Usage( | ||||
| 			'Manifest Builder 1.0', | ||||
| 			basename(__FIlE__), | ||||
| 			'php ' . basename(__FIlE__), | ||||
| 			'Builds a manifest file suitable for use by Autoloader', | ||||
| 			'Tommy Montgomery', | ||||
| 			'2009' | ||||
|  | ||||
| @ -8,7 +8,7 @@ | ||||
| 	function usage() { | ||||
| 		$usage = new Usage( | ||||
| 			'Productizer 1.0', | ||||
| 			basename(__FIlE__), | ||||
| 			'php ' . basename(__FIlE__), | ||||
| 			'Creates the equivalent of C#.NET\'s "AssemblyInfo.cs"', | ||||
| 			'Tommy Montgomery', | ||||
| 			'2009' | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user