1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef BS_CMD_LINE_H
7 #define BS_CMD_LINE_H
8 
9 #include <stdbool.h>
10 #include "bs_oswrap.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #define _MAX_LINE_WIDTH 120 /*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 
21 #define _MAXOPT_SWITCH_LEN  32 /* Maximum allowed length for a switch name */
22 #define _MAXOPT_NAME_LEN    32 /* Maximum allowed length for a variable name */
23 
24 #define _HELP_SWITCH  "[-h] [--h] [--help] [-?]"
25 #define _HELP_DESCR   "Print help"
26 
27 #define _MAX_STRINGY_LEN (_MAXOPT_SWITCH_LEN+_MAXOPT_NAME_LEN+2+1+2+1)
28 
29 
30 /**
31  * Prototype for a callback function when an option is found:
32  * inputs:
33  *  argv: Whole argv[i] option as received in main
34  *  offset: Offset to the end of the option string
35  *          (including a possible ':' or '=')
36  *  If the option had a value, it would be placed in &argv[offset]
37  */
38 typedef void (*option_found_callback_f)(char *argv, int offset);
39 
40 /*
41  * Structure defining each command line option
42  */
43 typedef struct {
44   /*
45    * if manual is set bs_args_parse_*() will ignore it except for
46    * displaying it the help messages and initializing <dest> to its
47    * default
48    */
49   bool manual;
50   /* For help messages, should it be wrapped in "[]" */
51   bool is_mandatory;
52   /* It is just a switch: it does not have something to store after */
53   bool is_switch;
54   /* Option name we search for: --<option> */
55   char *option;
56   /*
57    * Name of the option destination in the help messages:
58    * "--<option>=<name>"
59    */
60   char *name;
61   /* Type of option (see bs_read_optionparam()),
62    * including also l for lists & b for booleans */
63   char type;
64   /* Pointer to where the read value will be stored (may be NULL) */
65   void *dest;
66   /* Optional callback to be called when the switch is found */
67   option_found_callback_f call_when_found;
68   /* Long description for the help messages */
69   char *descript;
70 } bs_args_struct_t;
71 
72 #define ARG_TABLE_ENDMARKER \
73     {false, false, false, NULL, NULL, 0, NULL, NULL, NULL}
74 
75 int bs_is_option(const char *arg, const char *option, int with_value);
76 int bs_is_multi_opt(const char *arg, const char *option, uint* index, int with_value);
77 int bs_is_help(const char *arg);
78 void bs_read_optionparam(const char* str, void *dest, const char type, const char *long_d);
79 
80 void bs_args_set_defaults(bs_args_struct_t args_struct[]);
81 void bs_args_print_switches_help(bs_args_struct_t args_struct[]);
82 void bs_args_print_long_help(bs_args_struct_t args_struct[]);
83 void bs_args_parse_cmd_line(int argc, char *argv[], bs_args_struct_t args_struct[]);
84 void bs_args_parse_all_cmd_line(int argc, char *argv[], bs_args_struct_t args_struct[]);
85 bool bs_args_parse_one_arg(char *argv, bs_args_struct_t args_struct[]);
86 void bs_args_override_exe_name(char *name);
87 void bs_args_set_trace_prefix(char *name);
88 void bs_override_post_help(void (*new_f)(void));
89 
90 //Users can define these 2, so they will be used in the help message:
91 //  char executable_name[];
92 //  void component_print_post_help();
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif
99