Overview
Namespaces
Classes
Exceptions
1: <?php
2:
3: /**
4: * PhpOptions
5: * @link https://github.com/masicek/PhpOptions
6: * @author Viktor Mašíček <viktor@masicek.net>
7: * @license "New" BSD License
8: */
9:
10: namespace PhpOptions;
11:
12: /**
13: * Static class for getting options and argumets from command-line.
14: *
15: * @author Viktor Mašíček <viktor@masicek.net>
16: */
17: class Arguments
18: {
19:
20: /**
21: * List of options with their values readed form command-line.
22: *
23: * @var array
24: */
25: private static $options = NULL;
26:
27: /**
28: * List of arguments readed form command-line.
29: *
30: * @var array
31: */
32: private static $arguments = NULL;
33:
34:
35: /**
36: * Return array of options with their values in command-line.
37: * Options are prepared for better work.
38: * <pre>
39: * --abc="xxx" => ('abc' => 'xxx')
40: * --abc ="xxx" => ('abc' => 'xxx')
41: * --abc "xxx" => ('abc' => 'xxx')
42: * -abc="xxx" => ('a' => TRUE, 'b' => TRUE, 'c' => 'xxx')
43: * -abc "xxx" => ('a' => TRUE, 'b' => TRUE, 'c' => 'xxx')
44: * -abc ="xxx" => ('a' => TRUE, 'b' => TRUE, 'c' => 'xxx')
45: * -a -bc ="xxx" => ('a' => TRUE, 'b' => TRUE, 'c' => 'xxx')
46: * </pre>
47: *
48: * @return array ([option] => [value], ...)
49: */
50: public static function options()
51: {
52: self::setAll();
53: return self::$options;
54: }
55:
56:
57: /**
58: * Return array of arguments in command-line.
59: *
60: * @return array
61: */
62: public static function getArguments()
63: {
64: self::setAll();
65: return self::$arguments;
66: }
67:
68:
69: /**
70: * Set options and arguments from command-line into internal parameters,
71: * if parameters are empty.
72: *
73: * @return void
74: */
75: private static function setAll()
76: {
77: if (is_null(self::$options) || is_null(self::$arguments))
78: {
79: $all = self::readAll();
80: self::$options = $all['options'];
81: self::$arguments = $all['arguments'];
82: }
83: }
84:
85:
86: /**
87: * Read array of options and arguments in command-line.
88: *
89: * @return array ([options], [arguments])
90: */
91: private static function readAll()
92: {
93: $cmdOptions = array();
94: $cmdArguments = array();
95: $args = $_SERVER['argv'];
96:
97: // delete script name
98: array_shift($args);
99:
100: if (count($args) > 0)
101: {
102: // clean arguments
103: $argsClean = array();
104: foreach ($args as $arg)
105: {
106: $position = strpos($arg, '=');
107: if ($position === 0)
108: {
109: $argsClean[] = substr($arg, 1);
110: }
111: elseif ($position !== FALSE)
112: {
113: $argsClean[] = substr($arg, 0, $position);
114: $argsClean[] = substr($arg, $position + 1);
115: }
116: else
117: {
118: $argsClean[] = $arg;
119: }
120: }
121: $args = $argsClean;
122:
123: $previous = NULL;
124: foreach ($args as $arg)
125: {
126: // long option
127: if (substr($arg, 0, 2) == '--')
128: {
129: $option = substr($arg, 2);
130: $cmdOptions[$option] = TRUE;
131: $previous = $option;
132: }
133: // short options (exception for signed integer and real)
134: elseif (substr($arg, 0, 1) == '-' && !(preg_match('/^-[0-9]+([.,][0-9]+)?$/', $arg)))
135: {
136: foreach (str_split(substr($arg, 1)) as $char)
137: {
138: $cmdOptions[$char] = TRUE;
139: $previous = $char;
140: }
141: }
142: // value for previous option
143: elseif ($previous)
144: {
145: $cmdOptions[$previous] = $arg;
146: $previous = NULL;
147: }
148: // arguments
149: else
150: {
151: $cmdArguments[] = $arg;
152: }
153: }
154: }
155:
156: return array('options' => $cmdOptions, 'arguments' => $cmdArguments);
157: }
158:
159:
160: }
161: