1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.6.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Console;
16:
17: /**
18: * Provides an interface for interacting with
19: * a command's options and arguments.
20: */
21: class Arguments
22: {
23: /**
24: * Positional argument name map
25: *
26: * @var string[]
27: */
28: protected $argNames;
29:
30: /**
31: * Positional arguments.
32: *
33: * @var string[]
34: */
35: protected $args;
36:
37: /**
38: * Named options
39: *
40: * @var array
41: */
42: protected $options;
43:
44: /**
45: * Constructor
46: *
47: * @param string[] $args Positional arguments
48: * @param array $options Named arguments
49: * @param string[] $argNames List of argument names. Order is expected to be
50: * the same as $args.
51: */
52: public function __construct(array $args, array $options, array $argNames)
53: {
54: $this->args = $args;
55: $this->options = $options;
56: $this->argNames = $argNames;
57: }
58:
59: /**
60: * Get all positional arguments.
61: *
62: * @return string[]
63: */
64: public function getArguments()
65: {
66: return $this->args;
67: }
68:
69: /**
70: * Get positional arguments by index.
71: *
72: * @param int $index The argument index to access.
73: * @return string|null The argument value or null
74: */
75: public function getArgumentAt($index)
76: {
77: if ($this->hasArgumentAt($index)) {
78: return $this->args[$index];
79: }
80:
81: return null;
82: }
83:
84: /**
85: * Check if a positional argument exists
86: *
87: * @param int $index The argument index to check.
88: * @return bool
89: */
90: public function hasArgumentAt($index)
91: {
92: return isset($this->args[$index]);
93: }
94:
95: /**
96: * Check if a positional argument exists by name
97: *
98: * @param string $name The argument name to check.
99: * @return bool
100: */
101: public function hasArgument($name)
102: {
103: $offset = array_search($name, $this->argNames, true);
104: if ($offset === false) {
105: return false;
106: }
107:
108: return isset($this->args[$offset]);
109: }
110:
111: /**
112: * Check if a positional argument exists by name
113: *
114: * @param string $name The argument name to check.
115: * @return string|null
116: */
117: public function getArgument($name)
118: {
119: $offset = array_search($name, $this->argNames, true);
120: if ($offset === false || !isset($this->args[$offset])) {
121: return null;
122: }
123:
124: return $this->args[$offset];
125: }
126:
127: /**
128: * Get an array of all the options
129: *
130: * @return array
131: */
132: public function getOptions()
133: {
134: return $this->options;
135: }
136:
137: /**
138: * Get an option's value or null
139: *
140: * @param string $name The name of the option to check.
141: * @return string|int|bool|null The option value or null.
142: */
143: public function getOption($name)
144: {
145: if (isset($this->options[$name])) {
146: return $this->options[$name];
147: }
148:
149: return null;
150: }
151:
152: /**
153: * Check if an option is defined and not null.
154: *
155: * @param string $name The name of the option to check.
156: * @return bool
157: */
158: public function hasOption($name)
159: {
160: return isset($this->options[$name]);
161: }
162: }
163: