]>
git.proxmox.com Git - libgit2.git/blob - examples/args.c
4 size_t is_prefixed(const char *str
, const char *pfx
)
6 size_t len
= strlen(pfx
);
7 return strncmp(str
, pfx
, len
) ? 0 : len
;
11 const char **out
, struct args_info
*args
, const char *opt
, const char *def
)
13 const char *found
= args
->argv
[args
->pos
];
14 size_t len
= is_prefixed(found
, opt
);
20 if (args
->pos
+ 1 == args
->argc
) {
25 *out
= args
->argv
[args
->pos
];
29 if (found
[len
] == '=') {
30 *out
= found
+ len
+ 1;
38 const char **out
, struct args_info
*args
, const char *opt
)
40 const char *found
= args
->argv
[args
->pos
];
41 size_t len
= is_prefixed(found
, opt
);
47 if (args
->pos
+ 1 == args
->argc
)
48 fatal("expected value following argument", opt
);
50 *out
= args
->argv
[args
->pos
];
54 if (found
[len
] == '=') {
55 *out
= found
+ len
+ 1;
62 static const char *match_numeric_arg(struct args_info
*args
, const char *opt
)
64 const char *found
= args
->argv
[args
->pos
];
65 size_t len
= is_prefixed(found
, opt
);
71 if (args
->pos
+ 1 == args
->argc
)
72 fatal("expected numeric value following argument", opt
);
74 found
= args
->argv
[args
->pos
];
85 uint16_t *out
, struct args_info
*args
, const char *opt
)
87 const char *found
= match_numeric_arg(args
, opt
);
94 val
= (uint16_t)strtoul(found
, &endptr
, 0);
95 if (!endptr
|| *endptr
!= '\0')
96 fatal("expected number after argument", opt
);
103 int match_uint32_arg(
104 uint32_t *out
, struct args_info
*args
, const char *opt
)
106 const char *found
= match_numeric_arg(args
, opt
);
113 val
= (uint32_t)strtoul(found
, &endptr
, 0);
114 if (!endptr
|| *endptr
!= '\0')
115 fatal("expected number after argument", opt
);
122 static int match_int_internal(
123 int *out
, const char *str
, int allow_negative
, const char *opt
)
126 int val
= (int)strtol(str
, &endptr
, 10);
128 if (!endptr
|| *endptr
!= '\0')
129 fatal("expected number", opt
);
130 else if (val
< 0 && !allow_negative
)
131 fatal("negative values are not allowed", opt
);
139 int match_bool_arg(int *out
, struct args_info
*args
, const char *opt
)
141 const char *found
= args
->argv
[args
->pos
];
143 if (!strcmp(found
, opt
)) {
148 if (!strncmp(found
, "--no-", strlen("--no-")) &&
149 !strcmp(found
+ strlen("--no-"), opt
+ 2)) {
158 int is_integer(int *out
, const char *str
, int allow_negative
)
160 return match_int_internal(out
, str
, allow_negative
, NULL
);
164 int *out
, struct args_info
*args
, const char *opt
, int allow_negative
)
166 const char *found
= match_numeric_arg(args
, opt
);
169 return match_int_internal(out
, found
, allow_negative
, opt
);
172 int match_arg_separator(struct args_info
*args
)
177 if (strcmp(args
->argv
[args
->pos
], "--") != 0)
185 void strarray_from_args(git_strarray
*array
, struct args_info
*args
)
189 array
->count
= args
->argc
- args
->pos
;
190 array
->strings
= calloc(array
->count
, sizeof(char *));
191 assert(array
->strings
!= NULL
);
193 for (i
= 0; args
->pos
< args
->argc
; ++args
->pos
) {
194 array
->strings
[i
++] = args
->argv
[args
->pos
];
196 args
->pos
= args
->argc
;