Skip to content

Commit

Permalink
Readding commands directory - #153
Browse files Browse the repository at this point in the history
  • Loading branch information
bfren committed Aug 26, 2024
1 parent f99e611 commit 4732557
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/classes/cli/commands/errors/argument-missing.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Obadiah\Cli\Commands\Errors;

use Obadiah\App;
use Obadiah\Cli\Command;

App::check();

class Argument_Missing extends Command
{
/**
* Executed when a Command requires an argument that is missing.
*
* @param string $command_name Command name.
* @param string $arg_long Long form of the missing argument.
* @param string|null $arg_short Optional short form of the missing argument.
* @return void
*/
public function __construct(
public readonly string $command_name,
public readonly string $arg_long,
public readonly ?string $arg_short
) {}

/**
* Output error message and exit.
*
* @return never
*/
public function execute(): void
{
App::die("Command %s requires parameter %s%s.", $this->command_name, $this->arg_long, $this->arg_short == null ? "" : " | $this->arg_short");
}
}
31 changes: 31 additions & 0 deletions src/classes/cli/commands/errors/invalid.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Obadiah\Cli\Commands\Errors;

use Obadiah\App;
use Obadiah\Cli\Command;

App::check();

class Invalid extends Command
{
/**
* Executed when something goes wrong creating an instance of a command.
*
* @param string $command_class Command class name.
* @return void
*/
public function __construct(
public readonly string $command_class
) {}

/**
* Output error message and exit.
*
* @return never
*/
public function execute(): void
{
App::die("Error creating %s.", $this->command_class);
}
}
31 changes: 31 additions & 0 deletions src/classes/cli/commands/errors/unknown.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Obadiah\Cli\Commands\Errors;

use Obadiah\App;
use Obadiah\Cli\Command;

App::check();

class Unknown extends Command
{
/**
* Executed when a command has been called and is not mapped to an implementation class.
*
* @param string $command_name Command name.
* @return void
*/
public function __construct(
public readonly string $command_name
) {}

/**
* Output error message and exit.
*
* @return never
*/
public function execute(): void
{
App::die("Unknown command: %s.", $this->command_name);
}
}
33 changes: 33 additions & 0 deletions src/classes/cli/commands/hello-world.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Obadiah\Cli\Commands;

use Obadiah\App;
use Obadiah\Cli\Argument;
use Obadiah\Cli\Command;

App::check();

class Hello_World extends Command
{
/**
* Create command.
*
* @param string $person The person to say hello to.
* @return void
*/
public function __construct(
#[Argument("The name of the person to say hello to.", long: "to", required: true)]
public readonly string $person
) {}

/**
* Say hello!
*
* @return void
*/
public function execute(): void
{
App::die("Hello, $this->person!");
}
}
35 changes: 35 additions & 0 deletions src/classes/cli/commands/password-hash.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Obadiah\Cli\Commands;

use Obadiah\App;
use Obadiah\Cli\Argument;
use Obadiah\Cli\Command;
use Obadiah\Crypto\Crypto;
use SensitiveParameter;

App::check();

class Password_Hash extends Command
{
/**
* Create command.
*
* @param string $password The password to hash.
* @return void
*/
public function __construct(
#[Argument("The password to hash.", short: "p", required: true)]
#[SensitiveParameter] public readonly string $password
) {}

/**
* Output the hashed password.
*
* @return never
*/
public function execute(): void
{
App::die(Crypto::hash_password($this->password));
}
}
File renamed without changes.

0 comments on commit 4732557

Please sign in to comment.