1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#ifndef CLI_H
17425bb815Sopenharmony_ci#define CLI_H
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci#include <string.h>
20425bb815Sopenharmony_ci
21425bb815Sopenharmony_ci/**
22425bb815Sopenharmony_ci * Command line option definition.
23425bb815Sopenharmony_ci */
24425bb815Sopenharmony_citypedef struct
25425bb815Sopenharmony_ci{
26425bb815Sopenharmony_ci  int id; /**< unique ID of the option (CLI_OPT_DEFAULT, or anything >= 0) */
27425bb815Sopenharmony_ci  const char *opt; /**< short option variant (in the form of "x" without dashes) */
28425bb815Sopenharmony_ci  const char *longopt; /**< long option variant (in the form of "xxx" without dashes) */
29425bb815Sopenharmony_ci  const char *meta; /**< name(s) of the argument(s) of the option, for display only */
30425bb815Sopenharmony_ci  const char *help; /**< descriptive help message of the option */
31425bb815Sopenharmony_ci} cli_opt_t;
32425bb815Sopenharmony_ci
33425bb815Sopenharmony_ci/**
34425bb815Sopenharmony_ci * Special marker for default option which also marks the end of the option list.
35425bb815Sopenharmony_ci */
36425bb815Sopenharmony_ci#define CLI_OPT_DEFAULT -1
37425bb815Sopenharmony_ci
38425bb815Sopenharmony_ci/**
39425bb815Sopenharmony_ci * Returned by cli_consume_option () when no more options are available
40425bb815Sopenharmony_ci * or an error occured.
41425bb815Sopenharmony_ci */
42425bb815Sopenharmony_ci#define CLI_OPT_END -2
43425bb815Sopenharmony_ci
44425bb815Sopenharmony_ci/**
45425bb815Sopenharmony_ci * State of the sub-command processor.
46425bb815Sopenharmony_ci * No fields should be accessed other than error and arg.
47425bb815Sopenharmony_ci */
48425bb815Sopenharmony_citypedef struct
49425bb815Sopenharmony_ci{
50425bb815Sopenharmony_ci  /* Public fields. */
51425bb815Sopenharmony_ci  const char *error; /**< public field for error message */
52425bb815Sopenharmony_ci  const char *arg; /**< last processed argument as string */
53425bb815Sopenharmony_ci
54425bb815Sopenharmony_ci  /* Private fields. */
55425bb815Sopenharmony_ci  int argc; /**< remaining number of arguments */
56425bb815Sopenharmony_ci  char **argv; /**< remaining arguments */
57425bb815Sopenharmony_ci  const cli_opt_t *opts; /**< options */
58425bb815Sopenharmony_ci} cli_state_t;
59425bb815Sopenharmony_ci
60425bb815Sopenharmony_ci/**
61425bb815Sopenharmony_ci * Macro for writing command line option definition struct literals.
62425bb815Sopenharmony_ci */
63425bb815Sopenharmony_ci#define CLI_OPT_DEF(...) /*(cli_opt_t)*/ { __VA_ARGS__ }
64425bb815Sopenharmony_ci
65425bb815Sopenharmony_ci/*
66425bb815Sopenharmony_ci * Functions for CLI.
67425bb815Sopenharmony_ci */
68425bb815Sopenharmony_ci
69425bb815Sopenharmony_cicli_state_t cli_init (const cli_opt_t *options_p, int argc, char **argv);
70425bb815Sopenharmony_civoid cli_change_opts (cli_state_t *state_p, const cli_opt_t *options_p);
71425bb815Sopenharmony_ciint cli_consume_option (cli_state_t *state_p);
72425bb815Sopenharmony_ciconst char * cli_consume_string (cli_state_t *state_p);
73425bb815Sopenharmony_ciint cli_consume_int (cli_state_t *state_p);
74425bb815Sopenharmony_civoid cli_help (const char *prog_name_p, const char *command_name_p, const cli_opt_t *options_p);
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci#endif /* !CLI_H */
77