-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemoOptionsStrictTypes.php
187 lines (146 loc) · 6.75 KB
/
DemoOptionsStrictTypes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* JBZoo Toolbox - Cli.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Cli
*/
declare(strict_types=1);
namespace DemoApp\Commands;
use JBZoo\Cli\CliCommand;
use Symfony\Component\Console\Input\InputOption;
class DemoOptionsStrictTypes extends CliCommand
{
protected function configure(): void
{
$this
->setName('options-strict-types')
->setDescription('Show description of command.')
->setHelp("Full description and usage of command.\nYou can use severla lines.")
// None
->addOption('opt', 'o', InputOption::VALUE_NONE, 'Just a boolean flag')
// Required
->addOption('opt-req', null, InputOption::VALUE_REQUIRED, 'The option with required value')
->addOption(
'opt-req-default',
null,
InputOption::VALUE_REQUIRED,
'The option is requred but it has default value',
42,
)
// Optional
->addOption(
'opt-optional',
null,
InputOption::VALUE_OPTIONAL,
'Option is not required and can be undefined',
)
->addOption(
'opt-optional-default',
null,
InputOption::VALUE_OPTIONAL,
'Option is not required with default value',
42,
)
// Array
->addOption(
'opt-array-optional',
null,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Multiple values are allowed. Can be empty',
)
->addOption(
'opt-array-req',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Multiple values are allowed. Value is required',
)
->addOption(
'opt-array-req-default',
'a',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Multiple values are allowed. Value is required with defaut value',
[42, 'foo', 'bar'],
)
// Arguments
->addArgument('arg-req', InputOption::VALUE_REQUIRED, 'Argument is required')
->addArgument('arg-default', InputOption::VALUE_REQUIRED, 'Argument is optional with default value', 42)
->addArgument('arg-optional', InputOption::VALUE_OPTIONAL, 'Argument is optional, no default value');
parent::configure();
}
protected function executeAction(): int
{
// //////////////////////////////////////// Just a boolean flag
// ./my-app examples:agruments
$this->getOpt('opt'); // false
// ./my-app examples:agruments --opt
$this->getOpt('opt'); // true
// ./my-app examples:agruments -o
$this->getOpt('opt'); // true
// //////////////////////////////////////// The option requires a value
// ./my-app examples:agruments --opt-req
$this->getOpt('opt-req'); // Exception: The "--opt-req" option requires a value.
// ./my-app examples:agruments --opt-req=123.6
$this->getOpt('opt-req'); // "123.6"
// ./my-app examples:agruments --opt-req=123.6
$this->getOptBool('opt-req'); // true
// ./my-app examples:agruments --opt-req=123.6
$this->getOptInt('opt-req'); // 123
// ./my-app examples:agruments --opt-req=123.6
$this->getOptFloat('opt-req'); // 123.6
// ./my-app examples:agruments --opt-req=" 123.6 "
$this->getOptString('opt-req'); // "123.6"
// ./my-app examples:agruments --opt-req=123.6
$this->getOptArray('opt-req'); // ["123.6"]
// ./my-app examples:agruments --opt-req="15 July 2021 13:48:00"
$this->getOptDatetime('opt-req'); // \DateTimeImmutable {date: 2021-07-15 13:48:00. UTC (+00:00) }
// //////////////////////////////////////// The option requires a value with default value
// ./my-app examples:agruments
$this->getOpt('opt-req-default'); // 42
// ./my-app examples:agruments --opt-req-default
$this->getOpt('opt-req-default'); // Exception: The "--opt-req-default" option requires a value.
// ./my-app examples:agruments --opt-req-default=123.6
$this->getOpt('opt-req-default'); // "123.6"
// //////////////////////////////////////// Multiple values are allowed. Value is required with defaut value
// ./my-app examples:agruments
$this->getOpt('opt-array-req-default'); // "bar"
// ./my-app examples:agruments
$this->getOptArray('opt-array-req-default'); // [42, 'foo', 'bar']
// ./my-app examples:agruments --opt-array-req-default=123 --opt-array-req-default=asdasd
$this->getOptArray('opt-array-req-default'); // ['123', 'Qwerty']
// ./my-app examples:agruments -a123 -aQwerty
$this->getOptArray('opt-array-req-default'); // ['123', 'Qwerty']
// ./my-app examples:agruments -a123 -aQwerty
$this->getOpt('opt-array-req-default'); // 'Qwerty'
// ./my-app examples:agruments -aQwerty -aAsd
$this->getOpt('opt-array-req-default'); // 'Asd'
$input = $this->outputMode->getInput();
// //////////////////////////////////////// Arguments
// ./my-app examples:agruments
$input->getArgument('arg-req'); // null
// ./my-app examples:agruments Qwerty
$input->getArgument('arg-req'); // "Qwerty"
// ./my-app examples:agruments Qwerty
$input->getArgument('arg-default'); // 42
// ./my-app examples:agruments Qwerty Some text
$input->getArgument('arg-default'); // "Some"
// ./my-app examples:agruments Qwerty "Some text"
$input->getArgument('arg-default'); // "Some text"
// ./my-app examples:agruments Qwerty "Some text"
$input->getArgument('arg-optional'); // []
// ./my-app examples:agruments Qwerty "Some text" 123
$input->getArgument('arg-optional'); // ["123"]
// ./my-app examples:agruments Qwerty "Some text" 123 456 "789 098"
$input->getArgument('arg-optional'); // ["123", "456", "789 098"]
// //////////////////////////////////////// Standard input
// echo " Qwerty 123 " | php ./my-app examples:agruments
$this->_('STDIN: "' . self::getStdIn() . '"'); // " Qwerty 123 \n"
// Default success exist code is "0". Max value is 255.
return self::SUCCESS;
}
}