2017 © Pedro Peláez
 

library command

XPCLI

image

xp-framework/command

XPCLI

  • Monday, April 2, 2018
  • by thekid
  • Repository
  • 2 Watchers
  • 0 Stars
  • 23,305 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 14 Versions
  • 9 % Grown

The README.md

Commands

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.4+ Supports PHP 8.0+ Latest Stable Version, (*1)

Also known as "xpcli": Command line argument parsing via annotations., (*2)

Example

use util\cmd\{Command, Arg};
use rdbms\DriverManager;
use io\streams\Streams;

/** Performs an SQL query */
class Query extends Command {
  private $connection, $query;
  private $verbose= false;

  /** Connection DSN, e.g. `mysql://user:pass@host[:port][/database]` */
  #[Arg(position: 0)]
  public function useConnection(string $dsn) {
    $this->connection= DriverManager::getConnection($dsn);
    $this->connection->connect();
  }

  /** SQL query. Use `-` to read from standard input */
  #[Arg(position: 1)]
  public function useQuery(string $query) {
    if ('-' === $query) {
      $this->query= Streams::readAll($this->in->stream());
    } else {
      $this->query= $query;
    }
  }

  /** Verbose output */
  #[Arg]
  public function useVerbose() {
    $this->verbose= true;
  }

  /** @return int */
  public function run() {
    $this->verbose && $this->out->writeLine('@ ', $this->connection);
    $this->verbose && $this->out->writeLine('>>> ', $this->query);

    $result= $this->connection->open($this->query);
    if ($result->isSuccess()) {
      $this->verbose && $this->out->writeLine('<<< ', $result->affected());
      return $result->affected() ? 0 : 1;
    } else {
      $this->verbose && $this->out->writeLine('<<< Results');
      foreach ($result as $found => $record) {
        $this->out->writeLine($record);
      }
      return isset($found) ? 0 : 2;
    }
  }
}

To execute the class, use the cmd command:, (*3)

$ xp -m /path/to/rdbms cmd Query 'mysql://localhost/test' 'select * from account' -v
@ rdbms.mysqlx.MySqlxConnection(->rdbms.DSN@(mysql://localhost/test), rdbms.mysqlx.MySqlxProtocol(...)
>>> select * from account
<<< Results
[
  account_id => 1
  username => "kiesel"
  email => "alex.dandrea@example.com"
]
[
  account_id => 2
  username => "thekid"
  email => "timm.friebe@example.com"
]

To show the command's usage, supply -? or --help:, (*4)

query-class-usage, (*5)

See also

  • RFC #0133 - Add support for filenames as argument for XPCLI
  • RFC #0102 - XP Class Runners (original RFC)

The Versions

02/04 2018
20/06 2017

dev-refactor/remove-injection

dev-refactor/remove-injection http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

29/08 2016

v7.2.0

7.2.0.0 http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

11/07 2016

dev-feature/commands-runnable-via-xp

dev-feature/commands-runnable-via-xp http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

11/07 2016

v7.1.3

7.1.3.0 http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

05/07 2016

v7.1.2

7.1.2.0 http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

05/05 2016

v7.1.1

7.1.1.0 http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

02/05 2016

v7.1.0

7.1.0.0 http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

21/02 2016

v7.0.0

7.0.0.0 http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

10/01 2016

v6.10.0

6.10.0.0 http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp

09/01 2016

v6.9.2

6.9.2.0 http://xp-framework.net/

XPCLI

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

module xp