Skip to content

Commit 09c0b11

Browse files
committed
feat: pretty print help commands
1 parent c034537 commit 09c0b11

File tree

5 files changed

+75
-55
lines changed

5 files changed

+75
-55
lines changed

templates/cli/app/services/client.php.twig

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ use {{ spec.title | caseUcfirst }}\Client;
88
use Utopia\CLI\CLI;
99
use Utopia\Validator\Mock;
1010
use Utopia\CLI\Console;
11+
use {{ spec.title | caseUcfirst }}\Parser;
1112
1213
$client = new Client();
1314
$cli = new CLI();
15+
$parser = new Parser();
1416
1517
$cli->
16-
init(function() use ($cli) {
18+
init(function() use ($cli, $parser) {
1719
1820
if (array_key_exists('help', $cli->getArgs())) {
1921
$taskName = $cli->match()->getName();
@@ -22,16 +24,13 @@ $cli->
2224
$params = $task->getParams();
2325
2426
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
25-
26-
printf("\nUsage : {{ language.params.executableName }} client {$taskName} --[OPTIONS] \n\n");
27-
printf($description);
28-
printf("Options:\n");
29-
$mask = "\t%-20.20s %-125.125s\n";
30-
31-
foreach ($params as $key => $value) {
32-
if ($key !== 'help')
33-
printf($mask, $key, $value['description']);
34-
}
27+
Console::log("\nUsage : {{ language.params.executableName }} client {$taskName} --[OPTIONS] \n");
28+
Console::log($description);
29+
Console::log("Options:");
30+
array_walk($params, function(&$key) {
31+
$key = $key['description'];
32+
});
33+
$parser->formatArray($params);
3534
Console::exit(0);
3635
}
3736
});
@@ -76,16 +75,18 @@ $cli
7675
7776
$cli
7877
->task('help')
79-
->action(function() {
78+
->action(function() use ($parser) {
8079
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
81-
printf("\nUsage : {{ language.params.executableName }} client [COMMAND]\n\n");
82-
printf("Commands :\n");
83-
$mask = "\t%-20.20s %-125.125s\n";
84-
printf($mask, "setEndpoint", "Set your server endpoint.");
85-
printf($mask, "setProject", "Set the project you want to connect to.");
86-
printf($mask, "setKey", "Set the API key for the project.");
87-
printf($mask, "setLocale", "Set your preferred locale (eg: en-US).");
88-
printf("\nRun '{{ language.params.executableName }} client COMMAND --help' for more information on a command.\n");
80+
Console::log("\nUsage : {{ language.params.executableName }} client [COMMAND]\n");
81+
Console::log("Commands :");
82+
$commands = [
83+
"setEndpoint" => "Set your server endpoint.",
84+
"setProject" => "Set the project you want to connect to.",
85+
"setKey" => "Set the API key for the project.",
86+
"setLocale" => "Set your preferred locale (eg: en-US)."
87+
];
88+
$parser->formatArray($commands);
89+
Console::log("\nRun '{{ language.params.executableName }} client COMMAND --help' for more information on a command.");
8990
});
9091
9192

templates/cli/app/services/help.php.twig

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ namespace {{ spec.title | caseUcfirst }}\Services;
55
require_once './vendor/autoload.php';
66
77
use Utopia\CLI\Console;
8+
use {{ spec.title | caseUcfirst }}\Parser;
89
9-
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
10+
$parser = new Parser();
1011
11-
printf("\nUsage : {{ language.params.executableName }} [SERVICE] [COMMAND] --[OPTION]\n\n");
12-
printf("Services :\n");
13-
$mask = "\t%-20.20s %-125.125s\n";
12+
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
13+
Console::log("\nUsage : {{ language.params.executableName }} [SERVICE] [COMMAND] --[OPTION]\n");
14+
Console::log("Services :");
15+
$commands = [
1416
{% for service in spec.services %}
15-
printf($mask, "{{ service.name }}", "{{ service.description | replace({'"':'\''}) | raw }}");
17+
"{{ service.name }}" => "{{ service.description | replace({'"':'\''}) | raw }}",
1618
{% endfor %}
17-
printf("\nRun '{{ language.params.executableName }} [SERVICE] help' for more information on a service.\n");
19+
];
20+
$parser->formatArray($commands);
21+
Console::log("\nRun '{{ language.params.executableName }} [SERVICE] help' for more information on a service.");

templates/cli/app/services/init.php.twig

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use Exception;
88
use Utopia\CLI\CLI;
99
use Utopia\Validator\Mock;
1010
use Utopia\CLI\Console;
11+
use {{ spec.title | caseUcfirst }}\Parser;
1112
1213
const USER_PREFERENCES_FILE = __DIR__."/../../.preferences/.prefs.json";
1314
const PREFERENCE_ENDPOINT = "endpoint";
@@ -141,9 +142,10 @@ function promptUser()
141142
}
142143
143144
$cli = new CLI();
145+
$parser = new Parser();
144146
145147
$cli->
146-
init(function() use ($cli) {
148+
init(function() use ($cli, $parser) {
147149
148150
if (array_key_exists('help', $cli->getArgs())) {
149151
$taskName = $cli->match()->getName();
@@ -152,23 +154,20 @@ $cli->
152154
$params = $task->getParams();
153155
154156
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
155-
156-
printf("\nUsage : {{ language.params.executableName }} {$taskName} --[OPTIONS] \n\n");
157-
printf($description);
158-
printf("Options:\n");
159-
$mask = "\t%-20.20s %-125.125s\n";
160-
161-
foreach ($params as $key => $value) {
162-
if ($key !== 'help')
163-
printf($mask, $key, $value['description']);
164-
}
157+
Console::log("\nUsage : {{ language.params.executableName }} {$taskName} --[OPTIONS] \n");
158+
Console::log($description);
159+
Console::log("Options:");
160+
array_walk($params, function(&$key) {
161+
$key = $key['description'];
162+
});
163+
$parser->formatArray($params);
165164
Console::exit(0);
166165
}
167166
});
168167
169168
$cli
170169
->task('init')
171-
->label('description', "The init command is used to initialise your CLI\n\n")
170+
->label('description', "The init command is used to initialise your CLI\n")
172171
->param('endpoint', '', new Mock(), 'Your {{ spec.title | caseUcfirst }} endpoint', true)
173172
{% for header in spec.global.headers %}
174173
->param('{{ header.key | lower }}', '', new Mock(), '{{ header.description }}', true)

templates/cli/app/services/service.php.twig

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $parser = new Parser();
1515
$cli = new CLI();
1616
1717
$cli->
18-
init(function() use ($cli) {
18+
init(function() use ($cli, $parser) {
1919
2020
if (array_key_exists('help', $cli->getArgs())) {
2121
$taskName = $cli->match()->getName();
@@ -24,16 +24,13 @@ $cli->
2424
$params = $task->getParams();
2525
2626
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
27-
28-
printf("\nUsage : {{ language.params.executableName }} {{ service.name }} {$taskName} --[OPTIONS] \n\n");
29-
printf($description);
30-
printf("Options:\n");
31-
$mask = "\t%-20.20s %-125.125s\n";
32-
33-
foreach ($params as $key => $value) {
34-
if ($key !== 'help')
35-
printf($mask, $key, $value['description']);
36-
}
27+
Console::log("\nUsage : {{ language.params.executableName }} {{ service.name }} {$taskName} --[OPTIONS] \n");
28+
Console::log($description);
29+
Console::log("Options:");
30+
array_walk($params, function(&$key) {
31+
$key = $key['description'];
32+
});
33+
$parser->formatArray($params);
3734
Console::exit(0);
3835
}
3936
});
@@ -106,15 +103,17 @@ $cli
106103
107104
$cli
108105
->task('help')
109-
->action(function() {
106+
->action(function() use ($parser) {
110107
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m");
111-
printf("\nUsage : {{ language.params.executableName }} {{ service.name }} [COMMAND]\n\n");
112-
printf("Commands :\n");
113-
$mask = "\t%-20.20s %-125.125s\n";
108+
Console::log("\nUsage : {{ language.params.executableName }} {{ service.name }} [COMMAND]\n");
109+
Console::log("Commands :");
110+
$commands = [
114111
{% for method in service.methods %}
115-
printf($mask, "{{ method.name | caseCamel }}", "{{ method.description | replace({'"':'\''}) | raw }}");
112+
"{{ method.name | caseCamel }}" => "{{ method.description | replace({'"':'\''}) | raw }}",
116113
{% endfor %}
117-
printf("\nRun '{{ language.params.executableName }} {{ service.name }} COMMAND --help' for more information on a command.\n");
114+
];
115+
$parser->formatArray($commands);
116+
Console::log("\nRun '{{ language.params.executableName }} {{ service.name }} COMMAND --help' for more information on a command.");
118117
});
119118
120119

templates/cli/src/Parser.php.twig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,21 @@ class Parser {
123123
$table->injectData($transformedData);
124124
$table->display();
125125
}
126+
127+
public function formatArray(Array $arr) {
128+
$descriptionColumnLimit = 60;
129+
$commandNameColumnLimit = 20;
130+
$mask = "\t%-${commandNameColumnLimit}.${commandNameColumnLimit}s %-${descriptionColumnLimit}.${descriptionColumnLimit}s\n";
131+
132+
array_walk($arr, function(&$key) use ($descriptionColumnLimit){
133+
$key = explode("\n", wordwrap($key, $descriptionColumnLimit));
134+
});
135+
136+
foreach($arr as $key => $value) {
137+
foreach($value as $sentence) {
138+
printf($mask, $key, $sentence);
139+
$key = "";
140+
}
141+
}
142+
}
126143
}

0 commit comments

Comments
 (0)