aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file cmd_help.c |
| 3 | * @author Adam Piecek <piecek@cesnet.cz> |
| 4 | * @brief 'help' command of the libyang's yanglint tool. |
| 5 | * |
| 6 | * Copyright (c) 2023-2023 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include "cmd.h" |
| 18 | |
| 19 | #include <getopt.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include "libyang.h" |
| 24 | |
| 25 | #include "common.h" |
| 26 | #include "yl_opt.h" |
| 27 | |
| 28 | void |
| 29 | cmd_help_help(void) |
| 30 | { |
| 31 | printf("Usage: help [cmd ...]\n"); |
| 32 | } |
| 33 | |
| 34 | int |
| 35 | cmd_help_opt(struct yl_opt *yo, const char *cmdline, char ***posv, int *posc) |
| 36 | { |
| 37 | int rc = 0, argc = 0; |
| 38 | int opt, opt_index; |
| 39 | struct option options[] = { |
| 40 | {"help", no_argument, NULL, 'h'}, |
| 41 | {NULL, 0, NULL, 0} |
| 42 | }; |
| 43 | |
| 44 | if ((rc = parse_cmdline(cmdline, &argc, &yo->argv))) { |
| 45 | return rc; |
| 46 | } |
| 47 | |
| 48 | while ((opt = getopt_long(argc, yo->argv, commands[CMD_HELP].optstring, options, &opt_index)) != -1) { |
| 49 | if (opt == 'h') { |
| 50 | cmd_help_help(); |
| 51 | return 1; |
| 52 | } else { |
Michal Vasko | 1407d7d | 2023-08-21 09:57:54 +0200 | [diff] [blame] | 53 | YLMSG_E("Unknown option."); |
aPiecek | a83b8e0 | 2023-06-07 15:25:16 +0200 | [diff] [blame] | 54 | return 1; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | *posv = &yo->argv[optind]; |
| 59 | *posc = argc - optind; |
| 60 | |
| 61 | return rc; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | print_generic_help(void) |
| 66 | { |
| 67 | printf("Available commands:\n"); |
| 68 | for (uint16_t i = 0; commands[i].name; i++) { |
| 69 | if (commands[i].helpstring != NULL) { |
| 70 | printf(" %-15s %s\n", commands[i].name, commands[i].helpstring); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | cmd_help_exec(struct ly_ctx **ctx, struct yl_opt *yo, const char *posv) |
| 77 | { |
| 78 | (void)ctx, (void)yo; |
| 79 | |
| 80 | if (!posv) { |
| 81 | print_generic_help(); |
| 82 | } else { |
| 83 | /* print specific help for the selected command(s) */ |
| 84 | |
| 85 | int8_t match = 0; |
| 86 | |
| 87 | /* get the command of the specified name */ |
| 88 | for (uint16_t i = 0; commands[i].name; i++) { |
| 89 | if (strcmp(posv, commands[i].name) == 0) { |
| 90 | match = 1; |
| 91 | if (commands[i].help_func != NULL) { |
| 92 | commands[i].help_func(); |
| 93 | } else { |
| 94 | printf("%s: %s\n", posv, commands[i].helpstring); |
| 95 | } |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | if (!match) { |
| 100 | /* if unknown command specified, print the list of commands */ |
| 101 | printf("Unknown command \'%s\'\n", posv); |
| 102 | print_generic_help(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |