Sort parameters by letter

For readability, sort the command-line arguments parsing by letter.
This commit is contained in:
Romain Vimont 2018-02-14 18:39:46 +01:00
parent 9a07b694f1
commit d3c76c004e

View file

@ -102,60 +102,16 @@ static void print_version(void) {
static int parse_args(struct args *args, int argc, char *argv[]) { static int parse_args(struct args *args, int argc, char *argv[]) {
static const struct option long_options[] = { static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'},
{"max-size", required_argument, NULL, 'm'},
{"bit-rate", required_argument, NULL, 'b'}, {"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'}, {"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0 }, {NULL, 0, NULL, 0 },
}; };
int c; int c;
while ((c = getopt_long(argc, argv, "hvp:m:b:", long_options, NULL)) != -1) { while ((c = getopt_long(argc, argv, "b:hm:p:v", long_options, NULL)) != -1) {
switch (c) { switch (c) {
case 'h': {
args->help = SDL_TRUE;
break;
}
case 'v': {
args->version = SDL_TRUE;
break;
}
case 'p': {
char *endptr;
if (*optarg == '\0') {
LOGE("Invalid port parameter is empty");
return -1;
}
long value = strtol(optarg, &endptr, 0);
if (*endptr != '\0') {
LOGE("Invalid port: %s", optarg);
return -1;
}
if (value & ~0xffff) {
LOGE("Port out of range: %ld", value);
return -1;
}
args->port = (Uint16) value;
break;
}
case 'm': {
char *endptr;
if (*optarg == '\0') {
LOGE("Max size parameter is empty");
return -1;
}
long value = strtol(optarg, &endptr, 0);
if (*endptr != '\0') {
LOGE("Invalid max size: %s", optarg);
return -1;
}
if (value & ~0xffff) {
LOGE("Max size must be between 0 and 65535: %ld", value);
return -1;
}
args->max_size = (Uint16) value;
break;
}
case 'b': { case 'b': {
char *endptr; char *endptr;
if (*optarg == '\0') { if (*optarg == '\0') {
@ -185,6 +141,50 @@ static int parse_args(struct args *args, int argc, char *argv[]) {
args->bit_rate = (Uint32) value * mul; args->bit_rate = (Uint32) value * mul;
break; break;
} }
case 'h': {
args->help = SDL_TRUE;
break;
}
case 'm': {
char *endptr;
if (*optarg == '\0') {
LOGE("Max size parameter is empty");
return -1;
}
long value = strtol(optarg, &endptr, 0);
if (*endptr != '\0') {
LOGE("Invalid max size: %s", optarg);
return -1;
}
if (value & ~0xffff) {
LOGE("Max size must be between 0 and 65535: %ld", value);
return -1;
}
args->max_size = (Uint16) value;
break;
}
case 'p': {
char *endptr;
if (*optarg == '\0') {
LOGE("Invalid port parameter is empty");
return -1;
}
long value = strtol(optarg, &endptr, 0);
if (*endptr != '\0') {
LOGE("Invalid port: %s", optarg);
return -1;
}
if (value & ~0xffff) {
LOGE("Port out of range: %ld", value);
return -1;
}
args->port = (Uint16) value;
break;
}
case 'v': {
args->version = SDL_TRUE;
break;
}
default: default:
// getopt prints the error message on stderr // getopt prints the error message on stderr
return -1; return -1;