forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoCommand.php
More file actions
66 lines (60 loc) · 2.3 KB
/
Copy pathDemoCommand.php
File metadata and controls
66 lines (60 loc) · 2.3 KB
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
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-02-27
* Time: 18:58
*/
namespace Inhere\Console\Examples;
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
/**
* Class DemoCommand
* @package app\console\commands
*/
class DemoCommand extends Command
{
protected static $name = 'demo';
protected static $description = 'this is a demo independent command. but config use configure(), it like symfony console: argument define by position';
/**
* {@inheritDoc}
*/
protected function configure()
{
$this->createDefinition()
->setDescription(self::getDescription())
->setExample($this->handleAnnotationVars('{script} {command} john male 43 --opt1 value1'))
->addArgument('name', Input::ARG_REQUIRED, 'description for the argument [name], is required')
->addArgument('sex', Input::ARG_OPTIONAL, 'description for the argument [sex], is optional')
->addArgument('age', Input::ARG_OPTIONAL, 'description for the argument [age], is optional')
->addOption('yes', 'y', Input::OPT_BOOLEAN, 'description for the option [yes], is boolean')
->addOption('opt1', null, Input::OPT_REQUIRED, 'description for the option [opt1], is required')
->addOption('opt2', null, Input::OPT_OPTIONAL, 'description for the option [opt2], is optional')
;
}
/**
* description text by annotation. it is invalid when configure() is exists
* @param Input $input
* @param Output $output
* @return int|void
*/
public function execute($input, $output)
{
$output->write('hello, this in ' . __METHOD__);
// $name = $input->getArg('name');
$output->write(<<<EOF
this is argument and option example:
the opt1's value
option: opt1 |
| |
php examples/app demo john male 43 --opt1 value1 -y
| | | | | |
script command | | |______ option: yes, it use shortcat: y, and it is a Input::OPT_BOOLEAN, so no value.
| |___ |
argument: name | argument: age
argument: sex
EOF
);
}
}