#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
浏览源代码.
|
|
#define | AP_MAX_NCOMMAND 10 |
| |
|
#define | AP_LONG_FLAG "--" |
| |
|
#define | AP_SHORT_FLAG "-" |
| |
| #define | AP_END |
| |
|
#define | ap_min(a, b) ((a) > (b) ? (b) : (a)) |
| |
|
#define | ap_max(a, b) ((a) > (b) ? (a) : (b)) |
| |
|
#define | NOW (&pp->commands[pp->command_pos]) |
| |
|
#define | _green(s) "\033[32m" s "\033[0m" |
| |
|
#define | _red(s) "\033[31m" s "\033[0m" |
| |
|
#define | ERROR_MSG _red("[ERROR]") |
| |
|
#define | e(msg) (ERROR_MSG ": " msg "\n") |
| |
|
#define | ERROR_COMMAND_CONFLICT e("Conflict.") |
| |
|
#define | ERROR_NO_SUBCOMMAND e("Pass a subcommand.") |
| |
|
#define | ERROR_LOST_ARG_VALUE e("\"%s\" lost arg value.") |
| |
|
#define | ERROR_DONOT_NEED_VALUE e("\"%s\" does not need arg value.") |
| |
|
#define | ERROR_ARG_NOT_EXIST e("Arg name \"%s\" does not exist.") |
| |
|
#define | ERROR_LOST_ARG_NAME e("Except a arg name, but got \"%s\".") |
| |
|
#define | ERROR_SUBCOMMAND_NOT_EXIST e("Subcommand %s does not exist.") |
| |
|
#define | ap_defineArgs(name) static ap_arg_t name[] |
| |
|
#define | ap_defineCallback(name) void name(int argc, char* argv[], char* env[]) |
| |
|
|
typedef void(* | callback_t) (int argc, char *argv[], char *env[]) |
| |
|
typedef void(* | print_ap_command_t) (ap_command_t *) |
| |
|
typedef struct Parser | Parser |
| |
|
| enum | ArgValue { AP_YES = 0
, AP_NO
} |
| |
|
| struct Parser | p |
| |
|
struct Parser * | pp = &p |
| |
|
static int | have_global = 0 |
| |
|
static int | have_subcommand = 0 |
| |
- 作者
- your name (you@d.nosp@m.omai.nosp@m.n.com)
- 版本
- 0.1
- 日期
- 2024-01-09
- 版权所有
- Copyright (c) 2024
◆ AP_END
◆ _ap_parser_command_line()
| static void _ap_parser_command_line |
( |
int | argc, |
|
|
char * | argv[] ) |
|
inlinestatic |
◆ _is_eq()
| static int _is_eq |
( |
char * | arg_name, |
|
|
ap_arg_t | arg ) |
|
inlinestatic |
判断当前参数名是否与command中的参数名相同
- 参数
-
- 返回
- int
◆ ap_add_command()
| static void ap_add_command |
( |
char * | command, |
|
|
char * | description, |
|
|
char * | usage, |
|
|
callback_t | callback, |
|
|
ap_arg_t * | args ) |
|
inlinestatic |
添加一个子命令
- 参数
-
| command | |
| description | |
| usage | |
| callback | |
| args | |
◆ ap_default_print_command()
◆ ap_do_parser()
| static void ap_do_parser |
( |
int | argc, |
|
|
char * | argv[], |
|
|
char * | env[] ) |
|
inlinestatic |
◆ ap_get()
| static ap_arg_t * ap_get |
( |
char * | arg_name | ) |
|
|
inlinestatic |
◆ ap_init_parser()
| static void ap_init_parser |
( |
char * | print_message, |
|
|
print_ap_command_t | print_command ) |
|
inlinestatic |
初始化解析器
- 参数
-
| print_message | |
| print_command | |
初始值:= {
.ncommand = 0,
.command_pos = 0}