cmdutils: split per-option code out of parse_options().
This allows options like -target, which are just shortcuts for other options, to work without dummy function for all options they invoke.
This commit is contained in:
72
cmdutils.c
72
cmdutils.c
@ -203,29 +203,12 @@ static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
|
|||||||
}
|
}
|
||||||
#endif /* WIN32 && !__MINGW32CE__ */
|
#endif /* WIN32 && !__MINGW32CE__ */
|
||||||
|
|
||||||
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
|
int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options)
|
||||||
void (* parse_arg_function)(void *, const char*))
|
|
||||||
{
|
{
|
||||||
const char *opt, *arg;
|
|
||||||
int optindex, handleoptions=1;
|
|
||||||
const OptionDef *po;
|
const OptionDef *po;
|
||||||
|
|
||||||
/* perform system-dependent conversions for arguments list */
|
|
||||||
prepare_app_arguments(&argc, &argv);
|
|
||||||
|
|
||||||
/* parse options */
|
|
||||||
optindex = 1;
|
|
||||||
while (optindex < argc) {
|
|
||||||
void *dst;
|
|
||||||
opt = argv[optindex++];
|
|
||||||
|
|
||||||
if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
|
|
||||||
int bool_val = 1;
|
int bool_val = 1;
|
||||||
if (opt[1] == '-' && opt[2] == '\0') {
|
void *dst;
|
||||||
handleoptions = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
opt++;
|
|
||||||
po = find_option(options, opt);
|
po = find_option(options, opt);
|
||||||
if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
|
if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
|
||||||
/* handle 'no' bool option */
|
/* handle 'no' bool option */
|
||||||
@ -238,20 +221,18 @@ void parse_options(void *optctx, int argc, char **argv, const OptionDef *options
|
|||||||
po = find_option(options, "default");
|
po = find_option(options, "default");
|
||||||
if (!po->name) {
|
if (!po->name) {
|
||||||
unknown_opt:
|
unknown_opt:
|
||||||
fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
|
av_log(NULL, AV_LOG_ERROR, "Unrecognized option '%s'\n", opt);
|
||||||
exit_program(1);
|
return AVERROR(EINVAL);
|
||||||
}
|
|
||||||
arg = NULL;
|
|
||||||
if (po->flags & HAS_ARG) {
|
|
||||||
arg = argv[optindex++];
|
|
||||||
if (!arg) {
|
|
||||||
fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
|
|
||||||
exit_program(1);
|
|
||||||
}
|
}
|
||||||
|
if (po->flags & HAS_ARG && !arg) {
|
||||||
|
av_log(NULL, AV_LOG_ERROR, "Missing argument for option '%s'\n", opt);
|
||||||
|
return AVERROR(EINVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* new-style options contain an offset into optctx, old-style address of
|
/* new-style options contain an offset into optctx, old-style address of
|
||||||
* a global var*/
|
* a global var*/
|
||||||
dst = po->flags & OPT_OFFSET ? (uint8_t*)optctx + po->u.off : po->u.dst_ptr;
|
dst = po->flags & (OPT_OFFSET) ? (uint8_t*)optctx + po->u.off : po->u.dst_ptr;
|
||||||
|
|
||||||
if (po->flags & OPT_STRING) {
|
if (po->flags & OPT_STRING) {
|
||||||
char *str;
|
char *str;
|
||||||
str = av_strdup(arg);
|
str = av_strdup(arg);
|
||||||
@ -268,12 +249,39 @@ unknown_opt:
|
|||||||
int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg) :
|
int ret = po->flags & OPT_FUNC2 ? po->u.func2_arg(optctx, opt, arg) :
|
||||||
po->u.func_arg(opt, arg);
|
po->u.func_arg(opt, arg);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt);
|
av_log(NULL, AV_LOG_ERROR, "Failed to set value '%s' for option '%s'\n", arg, opt);
|
||||||
exit_program(1);
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (po->flags & OPT_EXIT)
|
if (po->flags & OPT_EXIT)
|
||||||
exit_program(0);
|
exit_program(0);
|
||||||
|
return !!(po->flags & HAS_ARG);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
|
||||||
|
void (* parse_arg_function)(void *, const char*))
|
||||||
|
{
|
||||||
|
const char *opt;
|
||||||
|
int optindex, handleoptions = 1, ret;
|
||||||
|
|
||||||
|
/* perform system-dependent conversions for arguments list */
|
||||||
|
prepare_app_arguments(&argc, &argv);
|
||||||
|
|
||||||
|
/* parse options */
|
||||||
|
optindex = 1;
|
||||||
|
while (optindex < argc) {
|
||||||
|
opt = argv[optindex++];
|
||||||
|
|
||||||
|
if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
|
||||||
|
if (opt[1] == '-' && opt[2] == '\0') {
|
||||||
|
handleoptions = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
opt++;
|
||||||
|
|
||||||
|
if ((ret = parse_option(optctx, opt, argv[optindex], options)) < 0)
|
||||||
|
exit_program(1);
|
||||||
|
optindex += ret;
|
||||||
} else {
|
} else {
|
||||||
if (parse_arg_function)
|
if (parse_arg_function)
|
||||||
parse_arg_function(optctx, opt);
|
parse_arg_function(optctx, opt);
|
||||||
|
@ -151,6 +151,13 @@ void show_help_options(const OptionDef *options, const char *msg, int mask, int
|
|||||||
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
|
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
|
||||||
void (* parse_arg_function)(void *optctx, const char*));
|
void (* parse_arg_function)(void *optctx, const char*));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse one given option.
|
||||||
|
*
|
||||||
|
* @return on success 1 if arg was consumed, 0 otherwise; negative number on error
|
||||||
|
*/
|
||||||
|
int parse_option(void *optctx, const char *opt, const char *arg, const OptionDef *options);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the given stream matches a stream specifier.
|
* Check if the given stream matches a stream specifier.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user