Expose device serial as an optional argument

The device serial was provided as a positional argument:

    scrcpy 0123456789abcdef

Instead, expose it as an optional argument, -s or --serial:

    scrcpy -s 0123456789abcdef

This avoids inconsistency between platforms when the positional
argument is passed before the options (which is undefined).
This commit is contained in:
Romain Vimont 2018-02-14 18:57:29 +01:00
parent 23d92a95b6
commit 8697659890

View file

@ -20,11 +20,7 @@ struct args {
static void usage(const char *arg0) { static void usage(const char *arg0) {
fprintf(stderr, fprintf(stderr,
"Usage: %s [options] [serial]\n" "Usage: %s [options]\n"
"\n"
" serial\n"
" The device serial number. Mandatory only if several devices\n"
" are connected to adb.\n"
"\n" "\n"
"Options:\n" "Options:\n"
"\n" "\n"
@ -46,6 +42,10 @@ static void usage(const char *arg0) {
" Set the TCP port the client listens on.\n" " Set the TCP port the client listens on.\n"
" Default is %d.\n" " Default is %d.\n"
"\n" "\n"
" -s, --serial\n"
" The device serial number. Mandatory only if several devices\n"
" are connected to adb.\n"
"\n"
" -v, --version\n" " -v, --version\n"
" Print the version of scrcpy.\n" " Print the version of scrcpy.\n"
"\n" "\n"
@ -177,11 +177,12 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"max-size", required_argument, NULL, 'm'}, {"max-size", required_argument, NULL, 'm'},
{"port", required_argument, NULL, 'p'}, {"port", required_argument, NULL, 'p'},
{"serial", required_argument, NULL, 's'},
{"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, "b:hm:p:v", long_options, NULL)) != -1) { while ((c = getopt_long(argc, argv, "b:hm:p:s:v", long_options, NULL)) != -1) {
switch (c) { switch (c) {
case 'b': { case 'b': {
if (!parse_bit_rate(optarg, &args->bit_rate)) { if (!parse_bit_rate(optarg, &args->bit_rate)) {
@ -205,6 +206,10 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
} }
break; break;
} }
case 's': {
args->serial = optarg;
break;
}
case 'v': { case 'v': {
args->version = SDL_TRUE; args->version = SDL_TRUE;
break; break;
@ -216,9 +221,6 @@ static SDL_bool parse_args(struct args *args, int argc, char *argv[]) {
} }
int index = optind; int index = optind;
if (index < argc) {
args->serial = argv[index++];
}
if (index < argc) { if (index < argc) {
LOGE("Unexpected additional argument: %s", argv[index]); LOGE("Unexpected additional argument: %s", argv[index]);
return SDL_FALSE; return SDL_FALSE;