Skip to content

Commit

Permalink
Merge pull request #14 from xp-framework/feature/use
Browse files Browse the repository at this point in the history
Change from `set` to `use` prefix for argument methods
  • Loading branch information
thekid authored Mar 28, 2024
2 parents 9e6b121 + f6a8c81 commit bf1db83
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
22 changes: 6 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,26 @@ class Query extends Command {
private $connection, $query;
private $verbose= false;

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/main/php/xp/command/CmdRunner.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function commandUsage(Type $type) {
$extra= $details= $positional= [];
foreach ($type->methods()->annotated(Arg::class) as $method) {
$arg= $method->annotation(Arg::class)->arguments();
$name= strtolower(preg_replace('/^set/', '', $method->name()));
$name= strtolower(preg_replace('/^(use|set)/', '', $method->name()));
$first= $method->parameter(0);
$optional= $first ? $first->optional() : true;
$comment= $method->comment();
Expand Down Expand Up @@ -253,7 +253,7 @@ protected function runCommand($command, $params, $config) {
$name= '#'.($position + 1);
$short= null;
} else {
$select= $name= $arg->argument('name') ?? strtolower(preg_replace('/^set/', '', $method->name()));
$select= $name= $arg->argument('name') ?? strtolower(preg_replace('/^(use|set)/', '', $method->name()));
$short= $arg->argument('short');
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/php/util/cmd/unittest/CmdRunnerTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,18 @@ public function can_be_invoked_via_main() {

Assert::equals(12, $command::main(['-a', 12]));
}

#[Test]
public function argument_name_with_use() {
$command= newinstance(Command::class, [], '{
private $arg= null;
#[Arg]
public function useArg($arg) { $this->arg= $arg; }
public function run() { $this->out->write($this->arg); }
}');

$this->runWith([nameof($command), '-a', 'UNITTEST']);
Assert::equals('UNITTEST', $this->out->bytes());
}
}

0 comments on commit bf1db83

Please sign in to comment.