1#ifndef AWS_COMMON_COMMAND_LINE_PARSER_H
2#define AWS_COMMON_COMMAND_LINE_PARSER_H
3/*
4 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License").
7 * You may not use this file except in compliance with the License.
8 * A copy of the License is located at
9 *
10 * http://aws.amazon.com/apache2.0
11 *
12 * or in the "license" file accompanying this file. This file is distributed
13 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * express or implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 */
17#include <aws/common/common.h>
18
19enum aws_cli_options_has_arg {
20 AWS_CLI_OPTIONS_NO_ARGUMENT = 0,
21 AWS_CLI_OPTIONS_REQUIRED_ARGUMENT = 1,
22 AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT = 2,
23};
24
25/* Ignoring padding since we're trying to maintain getopt.h compatibility */
26/* NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding) */
27struct aws_cli_option {
28 const char *name;
29 enum aws_cli_options_has_arg has_arg;
30 int *flag;
31 int val;
32};
33
34/**
35 * Initialized to 1 (for where the first argument would be). As arguments are parsed, this number is the index
36 * of the next argument to parse. Reset this to 1 to parse another set of arguments, or to rerun the parser.
37 */
38AWS_COMMON_API extern int aws_cli_optind;
39
40/**
41 * If an option has an argument, when the option is encountered, this will be set to the argument portion.
42 */
43AWS_COMMON_API extern const char *aws_cli_optarg;
44
45AWS_EXTERN_C_BEGIN
46/**
47 * A mostly compliant implementation of posix getopt_long(). Parses command-line arguments. argc is the number of
48 * command line arguments passed in argv. optstring contains the legitimate option characters. The option characters
49 * coorespond to aws_cli_option::val. If the character is followed by a :, the option requires an argument. If it is
50 * followed by '::', the argument is optional (not implemented yet).
51 *
52 * longopts, is an array of struct aws_cli_option. These are the allowed options for the program.
53 * The last member of the array must be zero initialized.
54 *
55 * If longindex is non-null, it will be set to the index in longopts, for the found option.
56 *
57 * Returns option val if it was found, '?' if an option was encountered that was not specified in the option string,
58 * returns -1 when all arguments that can be parsed have been parsed.
59 */
60AWS_COMMON_API int aws_cli_getopt_long(
61 int argc,
62 char *const argv[],
63 const char *optstring,
64 const struct aws_cli_option *longopts,
65 int *longindex);
66AWS_EXTERN_C_END
67
68#endif /* AWS_COMMON_COMMAND_LINE_PARSER_H */
69