| 1 | #ifndef AWS_COMMON_COMMAND_LINE_PARSER_H |
| 2 | #define AWS_COMMON_COMMAND_LINE_PARSER_H |
| 3 | /** |
| 4 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | * SPDX-License-Identifier: Apache-2.0. |
| 6 | */ |
| 7 | #include <aws/common/common.h> |
| 8 | |
| 9 | AWS_PUSH_SANE_WARNING_LEVEL |
| 10 | |
| 11 | enum aws_cli_options_has_arg { |
| 12 | AWS_CLI_OPTIONS_NO_ARGUMENT = 0, |
| 13 | AWS_CLI_OPTIONS_REQUIRED_ARGUMENT = 1, |
| 14 | AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT = 2, |
| 15 | }; |
| 16 | |
| 17 | /** |
| 18 | * Invoked when a subcommand is encountered. argc and argv[] begins at the command encountered. |
| 19 | * command_name is the name of the command being handled. |
| 20 | */ |
| 21 | typedef int(aws_cli_options_subcommand_fn)(int argc, char *const argv[], const char *command_name, void *user_data); |
| 22 | |
| 23 | /** |
| 24 | * Dispatch table to dispatch cli commands from. |
| 25 | * command_name should be the exact string for the command you want to handle from the command line. |
| 26 | */ |
| 27 | struct aws_cli_subcommand_dispatch { |
| 28 | aws_cli_options_subcommand_fn *subcommand_fn; |
| 29 | const char *command_name; |
| 30 | }; |
| 31 | |
| 32 | /* Ignoring padding since we're trying to maintain getopt.h compatibility */ |
| 33 | /* NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding) */ |
| 34 | struct aws_cli_option { |
| 35 | const char *name; |
| 36 | enum aws_cli_options_has_arg has_arg; |
| 37 | int *flag; |
| 38 | int val; |
| 39 | }; |
| 40 | |
| 41 | AWS_EXTERN_C_BEGIN |
| 42 | /** |
| 43 | * Initialized to 1 (for where the first argument would be). As arguments are parsed, this number is the index |
| 44 | * of the next argument to parse. Reset this to 1 to parse another set of arguments, or to rerun the parser. |
| 45 | */ |
| 46 | AWS_COMMON_API extern int aws_cli_optind; |
| 47 | |
| 48 | /** |
| 49 | * If an option has an argument, when the option is encountered, this will be set to the argument portion. |
| 50 | */ |
| 51 | AWS_COMMON_API extern const char *aws_cli_optarg; |
| 52 | |
| 53 | /** |
| 54 | * If 0x02 was returned by aws_cli_getopt_long(), this value will be set to the argument encountered. |
| 55 | */ |
| 56 | AWS_COMMON_API extern const char *aws_cli_positional_arg; |
| 57 | |
| 58 | /** |
| 59 | * A mostly compliant implementation of posix getopt_long(). Parses command-line arguments. argc is the number of |
| 60 | * command line arguments passed in argv. optstring contains the legitimate option characters. The option characters |
| 61 | * correspond to aws_cli_option::val. If the character is followed by a :, the option requires an argument. If it is |
| 62 | * followed by '::', the argument is optional (not implemented yet). |
| 63 | * |
| 64 | * longopts, is an array of struct aws_cli_option. These are the allowed options for the program. |
| 65 | * The last member of the array must be zero initialized. |
| 66 | * |
| 67 | * If longindex is non-null, it will be set to the index in longopts, for the found option. |
| 68 | * |
| 69 | * Returns option val if it was found, '?' if an option was encountered that was not specified in the option string, |
| 70 | * 0x02 (START_OF_TEXT) will be returned if a positional argument was encountered. returns -1 when all arguments that |
| 71 | * can be parsed have been parsed. |
| 72 | */ |
| 73 | AWS_COMMON_API int aws_cli_getopt_long( |
| 74 | int argc, |
| 75 | char *const argv[], |
| 76 | const char *optstring, |
| 77 | const struct aws_cli_option *longopts, |
| 78 | int *longindex); |
| 79 | |
| 80 | /** |
| 81 | * Resets global parser state for use in another parser run for the application. |
| 82 | */ |
| 83 | AWS_COMMON_API void aws_cli_reset_state(void); |
| 84 | |
| 85 | /** |
| 86 | * Dispatches the current command line arguments with a subcommand from the second input argument in argv[], if |
| 87 | * dispatch table contains a command that matches the argument. When the command is dispatched, argc and argv will be |
| 88 | * updated to reflect the new argument count. The cli options are required to come after the subcommand. If either, no |
| 89 | * dispatch was found or there was no argument passed to the program, this function will return AWS_OP_ERR. Check |
| 90 | * aws_last_error() for details on the error. |
| 91 | * @param argc number of arguments passed to int main() |
| 92 | * @param argv the arguments passed to int main() |
| 93 | * @param parse_cb, optional, specify NULL if you don't want to handle this. This argument is for parsing "meta" |
| 94 | * commands from the command line options prior to dispatch occurring. |
| 95 | * @param dispatch_table table containing functions and command name to dispatch on. |
| 96 | * @param table_length number of entries in dispatch_table. |
| 97 | * @return AWS_OP_SUCCESS(0) on success, AWS_OP_ERR(-1) on failure |
| 98 | */ |
| 99 | AWS_COMMON_API int aws_cli_dispatch_on_subcommand( |
| 100 | int argc, |
| 101 | char *const argv[], |
| 102 | struct aws_cli_subcommand_dispatch *dispatch_table, |
| 103 | int table_length, |
| 104 | void *user_data); |
| 105 | AWS_EXTERN_C_END |
| 106 | AWS_POP_SANE_WARNING_LEVEL |
| 107 | |
| 108 | #endif /* AWS_COMMON_COMMAND_LINE_PARSER_H */ |