PHPackages                             xp-framework/command - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. xp-framework/command

ActiveLibrary[CLI &amp; Console](/categories/cli)

xp-framework/command
====================

XPCLI

v12.0.1(1y ago)067.1k↓17.9%[1 PRs](https://github.com/xp-framework/command/pulls)BSD-3-ClausePHPPHP &gt;=7.4.0CI passing

Since Jan 9Pushed 1y ago1 watchersCompare

[ Source](https://github.com/xp-framework/command)[ Packagist](https://packagist.org/packages/xp-framework/command)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-framework-command/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (22)Used By (0)

Commands
========

[](#commands)

[![Build status on GitHub](https://github.com/xp-framework/command/workflows/Tests/badge.svg)](https://github.com/xp-framework/command/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/10567da9038a820ebc4c1f6e3a2ef0bd9e72a7eb63d3749f925d3d9303e861e0/68747470733a2f2f706f7365722e707567782e6f72672f78702d6672616d65776f726b2f636f6d6d616e642f76657273696f6e2e737667)](https://packagist.org/packages/xp-framework/command)

Also known as "xpcli": Command line argument parsing via annotations.

Example
-------

[](#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('
