Extract argument parsing to specific functions

To avoid a big switch/case, implement the argument parsing logic in
separate static functions.
This commit is contained in:
Romain Vimont 2018-02-14 18:53:16 +01:00
parent 111068d733
commit 23d92a95b6

View file

@ -100,19 +100,7 @@ static void print_version(void) {
fprintf(stderr, " - libavutil %d.%d.%d\n", LIBAVUTIL_VERSION_MAJOR, LIBAVUTIL_VERSION_MINOR, LIBAVUTIL_VERSION_MICRO); fprintf(stderr, " - libavutil %d.%d.%d\n", LIBAVUTIL_VERSION_MAJOR, LIBAVUTIL_VERSION_MINOR, LIBAVUTIL_VERSION_MICRO);
} }
static SDL_bool parse_args(struct args *args, int argc, char *argv[]) { static SDL_bool parse_bit_rate(char *optarg, Uint32 *bit_rate) {
static const struct option long_options[] = {
{"bit-rate", required_argument, NULL, 'b'},
{"help", no_argument, NULL, 'h'},
{"max-size", required_argument, NULL, 'm'},
{"port", required_argument, NULL, 'p'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0 },
};
int c;
while ((c = getopt_long(argc, argv, "b:hm:p:v", long_options, NULL)) != -1) {
switch (c) {
case 'b': {
char *endptr; char *endptr;
if (*optarg == '\0') { if (*optarg == '\0') {
LOGE("Bit-rate parameter is empty"); LOGE("Bit-rate parameter is empty");
@ -138,14 +126,12 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
LOGE("Bitrate must be positive and less than 2^32: %s", optarg); LOGE("Bitrate must be positive and less than 2^32: %s", optarg);
return SDL_FALSE; return SDL_FALSE;
} }
args->bit_rate = (Uint32) value * mul;
break; *bit_rate = (Uint32) value * mul;
return SDL_TRUE;
} }
case 'h': {
args->help = SDL_TRUE; static SDL_bool parse_max_size(char *optarg, Uint16 *max_size) {
break;
}
case 'm': {
char *endptr; char *endptr;
if (*optarg == '\0') { if (*optarg == '\0') {
LOGE("Max size parameter is empty"); LOGE("Max size parameter is empty");
@ -160,10 +146,12 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
LOGE("Max size must be between 0 and 65535: %ld", value); LOGE("Max size must be between 0 and 65535: %ld", value);
return SDL_FALSE; return SDL_FALSE;
} }
args->max_size = (Uint16) value;
break; *max_size = (Uint16) value;
return SDL_TRUE;
} }
case 'p': {
static SDL_bool parse_port(char *optarg, Uint16 *port) {
char *endptr; char *endptr;
if (*optarg == '\0') { if (*optarg == '\0') {
LOGE("Invalid port parameter is empty"); LOGE("Invalid port parameter is empty");
@ -178,7 +166,43 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
LOGE("Port out of range: %ld", value); LOGE("Port out of range: %ld", value);
return SDL_FALSE; return SDL_FALSE;
} }
args->port = (Uint16) value;
*port = (Uint16) value;
return SDL_TRUE;
}
static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
static const struct option long_options[] = {
{"bit-rate", required_argument, NULL, 'b'},
{"help", no_argument, NULL, 'h'},
{"max-size", required_argument, NULL, 'm'},
{"port", required_argument, NULL, 'p'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0 },
};
int c;
while ((c = getopt_long(argc, argv, "b:hm:p:v", long_options, NULL)) != -1) {
switch (c) {
case 'b': {
if (!parse_bit_rate(optarg, &args->bit_rate)) {
return SDL_FALSE;
}
break;
}
case 'h': {
args->help = SDL_TRUE;
break;
}
case 'm': {
if (!parse_max_size(optarg, &args->max_size)) {
return SDL_FALSE;
}
break;
}
case 'p': {
if (!parse_port(optarg, &args->port)) {
return SDL_FALSE;
}
break; break;
} }
case 'v': { case 'v': {