1 /* 2 * Copyright (c) 2018 Oticon A/S 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #ifndef _CMDLINE_COMMON_H 7 #define _CMDLINE_COMMON_H 8 9 #include <stdbool.h> 10 #include <stddef.h> 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 16 #define _MAX_LINE_WIDTH 100 /*Total width of the help message*/ 17 /* Horizontal alignment of the 2nd column of the help message */ 18 #define _LONG_HELP_ALIGN 30 19 20 #define _MAXOPT_SWITCH_LEN 32 /* Maximum allowed length for a switch name */ 21 #define _MAXOPT_NAME_LEN 32 /* Maximum allowed length for a variable name */ 22 23 #define _HELP_SWITCH "[-h] [--h] [--help] [-?]" 24 #define _HELP_DESCR "Display this help" 25 26 #define _MAX_STRINGY_LEN (_MAXOPT_SWITCH_LEN + _MAXOPT_NAME_LEN + 2 + 1 + 2 + 1) 27 28 29 /** 30 * Prototype for a callback function when an option is found: 31 * inputs: 32 * argv: Whole argv[i] option as received in main 33 * offset: Offset to the end of the option string 34 * (including a possible ':' or '=') 35 * If the option had a value, it would be placed in &argv[offset] 36 */ 37 typedef void (*option_found_callback_f)(char *argv, int offset); 38 39 /* 40 * Structure defining each command line option 41 */ 42 struct args_struct_t { 43 /* 44 * if manual is set cmd_args_parse*() will ignore it except for 45 * displaying it the help messages and initializing <dest> to its 46 * default 47 */ 48 bool manual; 49 /* For help messages, should it be wrapped in "[]" */ 50 bool is_mandatory; 51 /* It is just a switch: it does not have something to store after */ 52 bool is_switch; 53 /* Option name we search for: --<option> */ 54 char *option; 55 /* 56 * Name of the option destination in the help messages: 57 * "--<option>=<name>" 58 */ 59 char *name; 60 /* Type of option (see cmd_read_option_value()) */ 61 char type; 62 /* Pointer to where the read value will be stored (may be NULL) */ 63 void *dest; 64 /* Optional callback to be called when the switch is found */ 65 option_found_callback_f call_when_found; 66 /* Long description for the help messages */ 67 char *descript; 68 }; 69 70 #define ARG_TABLE_ENDMARKER \ 71 {false, false, false, NULL, NULL, 0, NULL, NULL, NULL} 72 73 int cmd_is_option(const char *arg, const char *option, int with_value); 74 int cmd_is_help_option(const char *arg); 75 void cmd_read_option_value(const char *str, void *dest, const char type, 76 const char *option); 77 void cmd_args_set_defaults(struct args_struct_t args_struct[]); 78 bool cmd_parse_one_arg(char *argv, struct args_struct_t args_struct[]); 79 void cmd_print_switches_help(struct args_struct_t args_struct[]); 80 81 #ifdef __cplusplus 82 } 83 #endif 84 85 #endif /* _CMDLINE_COMMON_H */ 86