PHP code example of 7csn / console

1. Go to this page and download the library: Download 7csn/console library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

7csn / console example snippets


  #!/usr/bin/env php
  
  
  use app\console\DemoCommand;
  use chaser\console\Application;
  
  (class_exists(DemoCommand::class)) {
      $app->add(new DemoCommand());
  }
  
  $code = $app->run(); # 0 ~ 255
  
  exit($code);
  

  
  
  namespace app\console;
  
  use chaser\console\argument\Option;
  use chaser\console\argument\Parameter;
  use chaser\console\command\Command;
  use chaser\console\input\InputInterface;
  use chaser\console\output\OutputInterface;
  
  class DemoCommand extends Command
  {
      /**
       * 返回命令默认名称
       *
       * @return string
       */
      public static function getDefaultName(): string
      {
          return 'demo';
      }
  
      /**
       * 返回命令默认描述
       *
       * @return string
       */
      public static function getDefaultDescription(): string
      {
          return 'This is a demo command';
      }
  
      /**
       * 返回命令输入定义参数组
       *
       * @return <Parameter|Option>[]
       */
      public function getArguments(): array
      {
          return [];
      }
  
      /**
       * 运行命令
       *
       * @param InputInterface $input
       * @param OutputInterface $output
       * @return int
       */
      public function run(InputInterface $input, OutputInterface $output): int
      {
          return 0;
      }
  }
  

    public function __construct(string $name, int $mode = 0, string $description = '', string ...$defaults){}
    

    public function __construct(string $name, ?string $shortcut = null, int $mode = 0, string $description = '', string ...$defaults){}
    

  public function run(InputInterface $input, OutputInterface $output): int{}
  

  >   # 获取输入实体(检测参数组是否定义合理、参数提供的值是否合理,异常则报错)
  >   $concrete = $this->getConcrete($input);
  >   

  >   # 判断是否有位置参数值
  >   $concrete->hasParameter('位置参数名');
  >   
  >   # 获取位置参数值
  >   $concrete->getParameter('位置参数名');
  >   

  >   # 判断是否有选项
  >   $concrete->hasOption('选项名');
  >
  >   # 获取选项值
  >   $concrete->hasOption('选项名');    
  >   
shell
  > php index.php
  
shell
  > php index.php -O 1
  
shell
  # 列出全部命令
  > php index.php list
    
  # 列出以“demo”开头的命令
  > php index.php list demo
  
shell
  # 查看 help 命令自身详情
  > php index.php help
    
  # 查看 demo 命令详情
  > php index.php help demo