Skip to content

Commit

Permalink
commands auto build help with --help arg
Browse files Browse the repository at this point in the history
  • Loading branch information
dvnc0 committed Aug 23, 2023
1 parent f823168 commit de54481
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/App/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ public function run(): void {

$action = $command->action;

if (!empty($cli_params['help'])) {
print($cli_params['help']);
return;
}

if (is_callable($action)) {
call_user_func($action, $cli_params);
return;
Expand Down
14 changes: 14 additions & 0 deletions src/App/Core/Command_Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
use Clyde\Objects\Application_Object;
use Clyde\Request\Request;
use Exception;
use Clyde\Tools\Help;

/**
* @phpstan-import-type CommandObject from \Clyde\Objects\Command_Object
*/
class Command_Parser
{
protected Help $Help;

/**
* Construct
*/
public function __construct() {
$this->Help = new Help;
}
/**
* Builds command data from a request and the Application
*
Expand All @@ -32,6 +41,11 @@ public function buildCommandData(Request $Request, Application_Object $Applicati
$has = [];
$cli_params = [];

if (isset($Request->arguments['help'])) {
$help_value = $this->Help->buildCommandHelpOutput($command, $Application_Object);
return [$command, ['help' => $help_value]];
}

foreach($possible_args as $title => $arg) {
if (isset($Request->arguments[$title])) {
$arg_passed = $Request->arguments[$title];
Expand Down
1 change: 1 addition & 0 deletions src/App/Objects/Flag_Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class Flag_Object
public string $help;
public string $title;
public bool $set_value = TRUE;
public bool $is_flag = TRUE;
}
1 change: 1 addition & 0 deletions src/App/Objects/Option_Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ class Option_Object
public bool $required = FALSE;
public string $help;
public string $title;
public bool $is_flag = FALSE;
}
85 changes: 71 additions & 14 deletions src/App/Tools/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Clyde\Tools;

use Clyde\Objects\Application_Object;
use Clyde\Objects\Command_Object;
use Clyde\Tools\Table;
use Exception;

Expand Down Expand Up @@ -39,6 +40,75 @@ class Help
* @return string
*/
public function buildHelpOutPut(Application_Object $Application_Object): string {
$file = $this->parseHelpTemplate($Application_Object);

$this->Table = new Table;
$rows = [];
foreach ($Application_Object->commands as $command) {
if ($command->hidden_command === TRUE){
continue;
}
$rows[] = [$command->command_name, $command->about];
}

$help_info = $this->Table->buildTable([
'headers' => ['Command', 'Description'],
'rows' => $rows
]);

$file = preg_replace('%\#commands\#%', $help_info, $file);

return $file;
}

/**
* Builds command help screens
*
* @param Command_Object $command The command object
* @param Application_Object $Application_Object Application object
* @return string
*/
public function buildCommandHelpOutput(Command_Object $command, Application_Object $Application_Object): string {
$file = $this->parseHelpTemplate($Application_Object);
$rows = [];

foreach($command->args as $arg) {
$value = $arg->is_flag ? '' : '=[VALUE]';
$rows[] = [
'--' . $arg->long_name . $value,
'-' . $arg->short_name . $value,
$arg->help,
$arg->required ? 'True' : 'False',
$arg->is_flag ? 'True' : 'False',
];
}

$this->Table = new Table;
$help_info = <<<TXT
Command: $command->command_name
About: $command->about
Usage:
TXT;
$help_info .= $this->Table->buildTable([
'headers' => ['Arg', 'Alias', 'Description', 'Required', 'Is Flag'],
'rows' => $rows,
]);

$file = preg_replace('%\#commands\#%', $help_info, $file);

return $file;
}

/**
* Initial parsing of help template
*
* @param Application_Object $Application_Object Application Object
* @return string
*/
protected function parseHelpTemplate(Application_Object $Application_Object): string {
$help_data = [
$Application_Object->application_name,
$Application_Object->author,
Expand All @@ -60,20 +130,7 @@ public function buildHelpOutPut(Application_Object $Application_Object): string
$file_contents = file_get_contents($template);

$file = preg_replace($this->help_lexemes, $help_data, $file_contents);

$this->Table = new Table;
$rows = [];
foreach ($Application_Object->commands as $command) {
$rows[] = [$command->command_name, $command->about];
}

$help_info = $this->Table->buildTable([
'headers' => ['Command', 'Description'],
'rows' => $rows
]);

$file = preg_replace('%\#commands\#%', $help_info, $file);


return $file;
}
}

0 comments on commit de54481

Please sign in to comment.