My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes  
Changes to /trunk/class.args.php
r11 vs. r12 Compare: vs.  Format:
Revision r12
Go to: 
Project members, sign in to write a code review
/trunk/class.args.php   r11 /trunk/class.args.php   r12
1 <?PHP 1 <?PHP
2 // A quick and dirty command line argument parser. Written in about 2 // A quick and dirty command line argument parser. Written in about
3 // an hour, so you might want to take this with a grain of salt or two. 3 // an hour, so you might want to take this with a grain of salt or two.
4 // 4 //
5 // Single letter options should be prefixed with a single 5 // Single letter options should be prefixed with a single
6 // dash and can be grouped together. Examples: 6 // dash and can be grouped together. Examples:
7 // 7 //
8 // cmd -a 8 // cmd -a
9 // cmd -ab 9 // cmd -ab
10 // 10 //
11 // Values can be assigned to single letter options like so: 11 // Values can be assigned to single letter options like so:
12 // 12 //
13 // cmd -a foo (a will be set to foo.) 13 // cmd -a foo (a will be set to foo.)
14 // 14 //
15 // cmd -a foo -b (a will be set to foo.) 15 // cmd -a foo -b (a will be set to foo.)
16 // 16 //
17 // cmd -ab foo (a and b will simply be set to true. foo is only listed as an argument.) 17 // cmd -ab foo (a and b will simply be set to true. foo is only listed as an argument.)
18 // 18 //
19 // You can also use the double-dash syntax. Examples: 19 // You can also use the double-dash syntax. Examples:
20 // 20 //
21 // cmd --value 21 // cmd --value
22 // 22 //
23 // cmd --value foo (value is set to foo) 23 // cmd --value foo (value is set to foo)
24 // 24 //
25 // cmd --value=foo (value is set to foo) 25 // cmd --value=foo (value is set to foo)
26 // 26 //
27 // Single dash and double dash syntax may be mixed. 27 // Single dash and double dash syntax may be mixed.
28 // 28 //
29 // Trailing arguments are treated as such. Examples: 29 // Trailing arguments are treated as such. Examples:
30 // 30 //
31 // cmd -abc foo bar (foo and bar are listed as arguments) 31 // cmd -abc foo bar (foo and bar are listed as arguments)
32 // 32 //
33 // cmd -a foo -b bar charlie (only bar and charlie are arguments) 33 // cmd -a foo -b bar charlie (only bar and charlie are arguments)
34 34
35 35
36 class Args 36 class Args
37 { 37 {
38 private $flags; 38 private $flags;
39 public $args; 39 public $args;
40 40
41 public function __construct() 41 public function __construct()
42 { 42 {
43 $this->flags = array(); 43 $this->flags = array();
44 $this->args = array(); 44 $this->args = array();
45 45
46 $argv = $GLOBALS['argv']; 46 $argv = $GLOBALS['argv'];
47 array_shift($argv); 47 array_shift($argv);
48 48
49 for($i = 0; $i < count($argv); $i++) 49 for($i = 0; $i < count($argv); $i++)
50 { 50 {
51 $str = $argv[$i]; 51 $str = $argv[$i];
52 52
53 // --foo 53 // --foo
54 if(strlen($str) > 2 && substr($str, 0, 2) == '--') 54 if(strlen($str) > 2 && substr($str, 0, 2) == '--')
55 { 55 {
56 $this->flags[substr($str, 2)] = true; 56 $this->flags[substr($str, 2)] = true;
57 57
58 $parts = explode('=', $str); 58 $parts = explode('=', $str);
59 59
60 // Does not have an =, so choose the next arg as its value 60 // Does not have an =, so choose the next arg as its value
61 if(count($parts) == 1 && isset($argv[$i + 1]) && preg_match('/^--?.+/', $argv[$i + 1]) == 0) 61 if(count($parts) == 1 && isset($argv[$i + 1]) && preg_match('/^--?.+/', $argv[$i + 1]) == 0)
62 { 62 {
63 $this->flags[substr($str, 2)] = $argv[$i + 1]; 63 $this->flags[substr($str, 2)] = $argv[$i + 1];
64 } 64 }
65 elseif(count($parts) == 2) // Has a =, so pick the second piece 65 elseif(count($parts) == 2) // Has a =, so pick the second piece
66 { 66 {
67 $this->flags[substr($str, 2)] = $parts[1]; 67 $this->flags[substr($str, 2)] = $parts[1];
68 } 68 }
69 } 69 }
70 elseif(strlen($str) == 2 && $str[0] == '-') // -a 70 elseif(strlen($str) == 2 && $str[0] == '-') // -a
71 { 71 {
72 $this->flags[$str[1]] = true; 72 $this->flags[$str[1]] = true;
73 if(isset($argv[$i + 1]) && preg_match('/^--?.+/', $argv[$i + 1]) == 0) 73 if(isset($argv[$i + 1]) && preg_match('/^--?.+/', $argv[$i + 1]) == 0)
74 $this->flags[$str[1]] = $argv[$i + 1]; 74 $this->flags[$str[1]] = $argv[$i + 1];
75 } 75 }
76 elseif(strlen($str) > 1 && $str[0] == '-') // -abcdef 76 elseif(strlen($str) > 1 && $str[0] == '-') // -abcdef
77 { 77 {
78 for($j = 1; $j < strlen($str); $j++) 78 for($j = 1; $j < strlen($str); $j++)
79 $this->flags[$str[$j]] = true; 79 $this->flags[$str[$j]] = true;
80 } 80 }
81 } 81 }
82 82
83 for($i = count($argv) - 1; $i >= 0; $i--) 83 for($i = count($argv) - 1; $i >= 0; $i--)
84 { 84 {
85 if(preg_match('/^--?.+/', $argv[$i]) == 0) 85 if(preg_match('/^--?.+/', $argv[$i]) == 0)
86 $this->args[] = $argv[$i]; 86 $this->args[] = $argv[$i];
87 else 87 else
88 break; 88 break;
89 } 89 }
90 90
91 $this->args = array_reverse($this->args); 91 $this->args = array_reverse($this->args);
92 } 92 }
93 93
94 public function flag($name) 94 public function flag($name)
95 { 95 {
96 return isset($this->flags[$name]) ? $this->flags[$name] : false; 96 return isset($this->flags[$name]) ? $this->flags[$name] : false;
97 } 97 }
98 } 98 }
Powered by Google Project Hosting