| 174 | } |
| 175 | |
| 176 | static void human_help(char *buffer, const jsmntok_t *result) |
| 177 | { |
| 178 | unsigned int i; |
| 179 | /* `curr` is used as a temporary token */ |
| 180 | const jsmntok_t *curr; |
| 181 | const char *prev_cat; |
| 182 | /* Contains all commands objects, which have the following structure : |
| 183 | * { |
| 184 | * "command": "The command name and usage", |
| 185 | * "category": "The command category", |
| 186 | * "description": "The command's description", |
| 187 | * "verbose": "The command's detailed description" |
| 188 | * } |
| 189 | */ |
| 190 | const jsmntok_t * help_array = json_get_member(buffer, result, "help"); |
| 191 | const jsmntok_t **help = tal_arr(NULL, const jsmntok_t *, |
| 192 | help_array->size); |
| 193 | |
| 194 | /* Populate an array for easy sorting with asort */ |
| 195 | json_for_each_arr(i, curr, help_array) |
| 196 | help[i] = curr; |
| 197 | |
| 198 | asort(help, tal_count(help), compare_help, buffer); |
| 199 | |
| 200 | prev_cat = ""; |
| 201 | for (i = 0; i < tal_count(help); i++) { |
| 202 | const jsmntok_t *category, *command, *desc; |
| 203 | |
| 204 | category = json_get_member(buffer, help[i], "category"); |
| 205 | if (category && !json_tok_streq(buffer, category, prev_cat)) { |
| 206 | prev_cat = json_strdup(help, buffer, category); |
| 207 | printf("=== %s ===\n\n", prev_cat); |
| 208 | } |
| 209 | |
| 210 | command = json_get_member(buffer, help[i], "command"); |
| 211 | desc = json_get_member(buffer, help[i], "description"); |
| 212 | printf("%.*s\n", |
| 213 | command->end - command->start, buffer + command->start); |
| 214 | printf(" %.*s\n\n", |
| 215 | desc->end - desc->start, buffer + desc->start); |
| 216 | } |
| 217 | tal_free(help); |
| 218 | |
| 219 | printf("---\nrun `lightning-cli help <command>` for more information on a specific command\n"); |
| 220 | } |
| 221 | |
| 222 | enum format { |
| 223 | JSON, |
no test coverage detected