Structure command line options help

It will allow to correctly print the help for the current terminal size
(even if for now it is hardcoded to 80 columns).
file
Romain Vimont 3 years ago
parent 9ec3406568
commit f59e9e3cb0

@ -8,244 +8,419 @@
#include "options.h" #include "options.h"
#include "util/log.h" #include "util/log.h"
#include "util/strbuf.h"
#include "util/str_util.h" #include "util/str_util.h"
#define STR_IMPL_(x) #x #define STR_IMPL_(x) #x
#define STR(x) STR_IMPL_(x) #define STR(x) STR_IMPL_(x)
void struct sc_option {
scrcpy_print_usage(const char *arg0) { char shortopt;
fprintf(stderr, const char *longopt;
"Usage: %s [options]\n" // no argument: argdesc == NULL && !optional_arg
"\n" // optional argument: argdesc != NULL && optional_arg
"Options:\n" // required argument: argdesc != NULL && !optional_arg
"\n" const char *argdesc;
" --always-on-top\n" bool optional_arg;
" Make scrcpy window always on top (above other windows).\n" const char *text;
"\n" };
" -b, --bit-rate value\n"
" Encode the video at the given bit-rate, expressed in bits/s.\n" static const struct sc_option options[] = {
" Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n" {
" Default is " STR(DEFAULT_BIT_RATE) ".\n" .longopt = "always-on-top",
"\n" .text = "Make scrcpy window always on top (above other windows).",
" --codec-options key[:type]=value[,...]\n" },
" Set a list of comma-separated key:type=value options for the\n" {
" device encoder.\n" .shortopt = 'b',
" The possible values for 'type' are 'int' (default), 'long',\n" .longopt = "bit-rate",
" 'float' and 'string'.\n" .argdesc = "value",
" The list of possible codec options is available in the\n" .text = "Encode the video at the gitven bit-rate, expressed in bits/s. "
" Android documentation:\n" "Unit suffixes are supported: 'K' (x1000) and 'M' (x1000000).\n"
" <https://d.android.com/reference/android/media/MediaFormat>\n" "Default is " STR(DEFAULT_BIT_RATE) ".",
"\n" },
" --crop width:height:x:y\n" {
" Crop the device screen on the server.\n" .longopt = "codec-options",
" The values are expressed in the device natural orientation\n" .argdesc = "key[:type]=value[,...]",
" (typically, portrait for a phone, landscape for a tablet).\n" .text = "Set a list of comma-separated key:type=value options for the "
" Any --max-size value is computed on the cropped size.\n" "device encoder.\n"
"\n" "The possible values for 'type' are 'int' (default), 'long', "
" --disable-screensaver\n" "'float' and 'string'.\n"
" Disable screensaver while scrcpy is running.\n" "The list of possible codec options is available in the "
"\n" "Android documentation: "
" --display id\n" "<https://d.android.com/reference/android/media/MediaFormat>",
" Specify the display id to mirror.\n" },
"\n" {
" The list of possible display ids can be listed by:\n" .longopt = "crop",
" adb shell dumpsys display\n" .argdesc = "width:height:x:y",
" (search \"mDisplayId=\" in the output)\n" .text = "Crop the device screen on the server.\n"
"\n" "The values are expressed in the device natural orientation "
" Default is 0.\n" "(typically, portrait for a phone, landscape for a tablet). "
"\n" "Any --max-size value is cmoputed on the cropped size.",
" --display-buffer ms\n" },
" Add a buffering delay (in milliseconds) before displaying.\n" {
" This increases latency to compensate for jitter.\n" .longopt = "disable-screensaver",
"\n" .text = "Disable screensaver while scrcpy is running.",
" Default is 0 (no buffering).\n" },
"\n" {
" --encoder name\n" .longopt = "display",
" Use a specific MediaCodec encoder (must be a H.264 encoder).\n" .argdesc = "id",
"\n" .text = "Specify the display id to mirror.\n"
" --force-adb-forward\n" "The list of possible display ids can be listed by:\n"
" Do not attempt to use \"adb reverse\" to connect to the\n" " adb shell dumpsys display\n"
" device.\n" "(search \"mDisplayId=\" in the output)\n"
"\n" "Default is 0.",
" --forward-all-clicks\n" },
" By default, right-click triggers BACK (or POWER on) and\n" {
" middle-click triggers HOME. This option disables these\n" .longopt = "display-buffer",
" shortcuts and forwards the clicks to the device instead.\n" .argdesc = "ms",
"\n" .text = "Add a buffering delay (in milliseconds) before displaying. "
" -f, --fullscreen\n" "This increases latency to compensate for jitter.\n"
" Start in fullscreen.\n" "Default is 0 (no buffering).",
"\n" },
" -K, --hid-keyboard\n" {
" Simulate a physical keyboard by using HID over AOAv2.\n" .longopt = "encoder",
" It provides a better experience for IME users, and allows to\n" .argdesc = "name",
" generate non-ASCII characters, contrary to the default\n" .text = "Use a specific MediaCodec encoder (must be a H.264 encoder).",
" injection method.\n" },
" It may only work over USB, and is currently only supported\n" {
" on Linux.\n" .longopt = "force-adb-forward",
"\n" .text = "Do not attempt to use \"adb reverse\" to connect to the "
" -h, --help\n" "device.",
" Print this help.\n" },
"\n" {
" --legacy-paste\n" .longopt = "forward-all-clicks",
" Inject computer clipboard text as a sequence of key events\n" .text = "By default, right-click triggers BACK (or POWER on) and "
" on Ctrl+v (like MOD+Shift+v).\n" "middle-click triggers HOME. This option disables these "
" This is a workaround for some devices not behaving as\n" "shortcuts and forwards the clicks to the device instead.",
" expected when setting the device clipboard programmatically.\n" },
"\n" {
" --lock-video-orientation[=value]\n" .shortopt = 'f',
" Lock video orientation to value.\n" .longopt = "fullscreen",
" Possible values are \"unlocked\", \"initial\" (locked to the\n" .text = "Start in fullscreen.",
" initial orientation), 0, 1, 2 and 3.\n" },
" Natural device orientation is 0, and each increment adds a\n" {
" 90 degrees rotation counterclockwise.\n" .shortopt = 'K',
" Default is \"unlocked\".\n" .longopt = "hid-keyboard",
" Passing the option without argument is equivalent to passing\n" .text = "Simulate a physical keyboard by using HID over AOAv2.\n"
" \"initial\".\n" "It provides a better experience for IME users, and allows to "
"\n" "generate non-ASCII characters, contrary to the default "
" --max-fps value\n" "injection method.\n"
" Limit the frame rate of screen capture (officially supported\n" "It may only work over USB, and is currently only supported "
" since Android 10, but may work on earlier versions).\n" "on Linux.",
"\n" },
" -m, --max-size value\n" {
" Limit both the width and height of the video to value. The\n" .shortopt = 'h',
" other dimension is computed so that the device aspect-ratio\n" .longopt = "help",
" is preserved.\n" .text = "Print this help.",
" Default is 0 (unlimited).\n" },
"\n" {
" -n, --no-control\n" .longopt = "legacy-paste",
" Disable device control (mirror the device in read-only).\n" .text = "Inject computer clipboard text as a sequence of key events "
"\n" "on Ctrl+v (like MOD+Shift+v).\n"
" -N, --no-display\n" "This is a workaround for some devices not behaving as "
" Do not display device (only when screen recording is\n" "expected when setting the device clipboard programmatically.",
" enabled).\n" },
"\n" {
" --no-key-repeat\n" .longopt = "lock-video-orientation",
" Do not forward repeated key events when a key is held down.\n" .argdesc = "value",
"\n" .optional_arg = true,
" --no-mipmaps\n" .text = "Lock video orientation to value.\n"
" If the renderer is OpenGL 3.0+ or OpenGL ES 2.0+, then\n" "Possible values are \"unlocked\", \"initial\" (locked to the "
" mipmaps are automatically generated to improve downscaling\n" "initial orientation), 0, 1, 2 and 3. Natural device "
" quality. This option disables the generation of mipmaps.\n" "orientation is 0, and each increment adds a 90 degrees "
"\n" "rotation counterclockwise.\n"
" -p, --port port[:port]\n" "Default is \"unlocked\".\n"
" Set the TCP port (range) used by the client to listen.\n" "Passing the option without argument is equivalent to passing "
" Default is " STR(DEFAULT_LOCAL_PORT_RANGE_FIRST) ":" "\"initial\".",
STR(DEFAULT_LOCAL_PORT_RANGE_LAST) ".\n" },
"\n" {
" --power-off-on-close\n" .longopt = "max-fps",
" Turn the device screen off when closing scrcpy.\n" .argdesc = "value",
"\n" .text = "Limit the frame rate of screen capture (officially supported "
" --prefer-text\n" "since Android 10, but may work on earlier versions).",
" Inject alpha characters and space as text events instead of\n" },
" key events.\n" {
" This avoids issues when combining multiple keys to enter a\n" .shortopt = 'm',
" special character, but breaks the expected behavior of alpha\n" .longopt = "max-size",
" keys in games (typically WASD).\n" .argdesc = "value",
"\n" .text = "Limit both the width and height of the video to value. The "
" --push-target path\n" "other dimension is computed so that the device aspect-ratio "
" Set the target directory for pushing files to the device by\n" "is preserved.\n"
" drag & drop. It is passed as is to \"adb push\".\n" "Default is 0 (unlimited).",
" Default is \"/sdcard/Download/\".\n" },
"\n" {
" -r, --record file.mp4\n" .shortopt = 'n',
" Record screen to file.\n" .longopt = "no-control",
" The format is determined by the --record-format option if\n" .text = "Disable device control (mirror the device in read-only).",
" set, or by the file extension (.mp4 or .mkv).\n" },
"\n" {
" --record-format format\n" .shortopt = 'N',
" Force recording format (either mp4 or mkv).\n" .longopt = "no-display",
"\n" .text = "Do not display device (only when screen recording "
" --render-driver name\n"
" Request SDL to use the given render driver (this is just a\n"
" hint).\n"
" Supported names are currently \"direct3d\", \"opengl\",\n"
" \"opengles2\", \"opengles\", \"metal\" and \"software\".\n"
" <https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER>\n"
"\n"
" --rotation value\n"
" Set the initial display rotation.\n"
" Possible values are 0, 1, 2 and 3. Each increment adds a 90\n"
" degrees rotation counterclockwise.\n"
"\n"
" -s, --serial serial\n"
" The device serial number. Mandatory only if several devices\n"
" are connected to adb.\n"
"\n"
" --shortcut-mod key[+...][,...]\n"
" Specify the modifiers to use for scrcpy shortcuts.\n"
" Possible keys are \"lctrl\", \"rctrl\", \"lalt\", \"ralt\",\n"
" \"lsuper\" and \"rsuper\".\n"
"\n"
" A shortcut can consist in several keys, separated by '+'.\n"
" Several shortcuts can be specified, separated by ','.\n"
"\n"
" For example, to use either LCtrl+LAlt or LSuper for scrcpy\n"
" shortcuts, pass \"lctrl+lalt,lsuper\".\n"
"\n"
" Default is \"lalt,lsuper\" (left-Alt or left-Super).\n"
"\n"
" -S, --turn-screen-off\n"
" Turn the device screen off immediately.\n"
"\n"
" -t, --show-touches\n"
" Enable \"show touches\" on start, restore the initial value\n"
" on exit.\n"
" It only shows physical touches (not clicks from scrcpy).\n"
"\n"
#ifdef HAVE_V4L2 #ifdef HAVE_V4L2
" --v4l2-sink /dev/videoN\n" "or V4L2 sink "
" Output to v4l2loopback device.\n" #endif
" It requires to lock the video orientation (see\n" "is enabled).",
" --lock-video-orientation).\n" },
"\n" {
" --v4l2-buffer ms\n" .longopt = "no-key-repeat",
" Add a buffering delay (in milliseconds) before pushing\n" .text = "Do not forward repeated key events when a key is held down.",
" frames. This increases latency to compensate for jitter.\n" },
"\n" {
" This option is similar to --display-buffer, but specific to\n" .longopt = "no-mipmaps",
" V4L2 sink.\n" .text = "If the renderer is OpenGL 3.0+ or OpenGL ES 2.0+, then "
"\n" "mipmaps are automatically generated to improve downscaling "
" Default is 0 (no buffering).\n" "quality. This option disables the generation of mipmaps.",
"\n" },
{
.shortopt = 'p',
.longopt = "port",
.argdesc = "port[:port]",
.text = "Set the TCP port (range) used by the client to listen.\n"
"Default is " STR(DEFAULT_LOCAL_PORT_RANGE_FIRST) ":"
STR(DEFAULT_LOCAL_PORT_RANGE_LAST) ".",
},
{
.longopt = "power-off-on-close",
.text = "Turn the device screen off when closing scrcpy.",
},
{
.longopt = "prefer-text",
.text = "Inject alpha characters and space as text events instead of"
"key events.\n"
"This avoids issues when combining multiple keys to enter a "
"special character, but breaks the expected behavior of alpha "
"keys in games (typically WASD).",
},
{
.longopt = "push-target",
.argdesc = "path",
.text = "Set the target directory for pushing files to the device by "
"drag & drop. It is passed as is to \"adb push\".\n"
"Default is \"/sdcard/Download/\".",
},
{
.shortopt = 'r',
.longopt = "record",
.argdesc = "file.mp4",
.text = "Record screen to file.\n"
"The format is determined by the --record-format option if "
"set, or by the file extension (.mp4 or .mkv).",
},
{
.longopt = "record-format",
.argdesc = "format",
.text = "Force recording format (either mp4 or mkv).",
},
{
.longopt = "render-driver",
.argdesc = "name",
.text = "Request SDL to use the given render driver (this is just a "
"hint).\n"
"Supported names are currently \"direct3d\", \"opengl\", "
"\"opengles2\", \"opengles\", \"metal\" and \"software\".\n"
"<https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER>",
},
{
.longopt = "rotation",
.argdesc = "value",
.text = "Set the initial display rotation.\n"
"Possible values are 0, 1, 2 and 3. Each increment adds a 90 "
"degrees rotation counterclockwise.",
},
{
.shortopt = 's',
.longopt = "serial",
.argdesc = "serial",
.text = "The device serial number. Mandatory only if several devices "
"are connected to adb.",
},
{
.longopt = "shortcut-mod",
.argdesc = "key[+...][,...]",
.text = "Specify the modifiers to use for scrcpy shortcuts.\n"
"Possible keys are \"lctrl\", \"rctrl\", \"lalt\", \"ralt\", "
"\"lsuper\" and \"rsuper\".\n"
"A shortcut can consist in several keys, separated by '+'. "
"Several shortcuts can be specified, separated by ','.\n"
"For example, to use either LCtrl+LAlt or LSuper for scrcpy "
"shortcuts, pass \"lctrl+lalt,lsuper\".\n"
"Default is \"lalt,lsuper\" (left-Alt or left-Super).",
},
{
.shortopt = 'S',
.longopt = "turn-screen-off",
.text = "Turn the device screen off immediately.",
},
{
.shortopt = 't',
.longopt = "show-touches",
.text = "Enable \"show touches\" on start, restore the initial value "
"on exit.\n"
"It only shows physical touches (not clicks from scrcpy).",
},
#ifdef HAVE_V4L2
{
.longopt = "v4l2-sink",
.argdesc = "/dev/videoN",
.text = "Output to v4l2loopback device.\n"
"It requires to lock the video orientation (see "
"--lock-video-orientation).",
},
{
.longopt = "v4l2-buffer",
.argdesc = "ms",
.text = "Add a buffering delay (in milliseconds) before pushing "
"frames. This increases latency to compensate for jitter.\n"
"This option is similar to --display-buffer, but specific to "
"V4L2 sink.\n"
"Default is 0 (no buffering).",
},
#endif #endif
" -V, --verbosity value\n" {
" Set the log level (verbose, debug, info, warn or error).\n" .shortopt = 'V',
.longopt = "verbosity",
.argdesc = "value",
.text = "Set the log level (verbose, debug, info, warn or error).\n"
#ifndef NDEBUG #ifndef NDEBUG
" Default is debug.\n" "Default is debug.",
#else #else
" Default is info.\n" "Default is info.",
#endif #endif
"\n" },
" -v, --version\n" {
" Print the version of scrcpy.\n" .shortopt = 'v',
"\n" .longopt = "version",
" -w, --stay-awake\n" .text = "Print the version of scrcpy.",
" Keep the device on while scrcpy is running, when the device\n" },
" is plugged in.\n" {
"\n" .shortopt = 'w',
" --window-borderless\n" .longopt = "stay-awake",
" Disable window decorations (display borderless window).\n" .text = "Keep the device on while scrcpy is running, when the device "
"\n" "is plugged in.",
" --window-title text\n" },
" Set a custom window title.\n" {
"\n" .longopt = "window-borderless",
" --window-x value\n" .text = "Disable window decorations (display borderless window)."
" Set the initial window horizontal position.\n" },
" Default is \"auto\".\n" {
"\n" .longopt = "window-title",
" --window-y value\n" .argdesc = "text",
" Set the initial window vertical position.\n" .text = "Set a custom window title.",
" Default is \"auto\".\n" },
"\n" {
" --window-width value\n" .longopt = "window-x",
" Set the initial window width.\n" .argdesc = "value",
" Default is 0 (automatic).\n" .text = "Set the initial window horizontal position.\n"
"\n" "Default is \"auto\".",
" --window-height value\n" },
" Set the initial window height.\n" {
" Default is 0 (automatic).\n" .longopt = "window-y",
"\n" .argdesc = "value",
.text = "Set the initial window vertical position.\n"
"Default is \"auto\".",
},
{
.longopt = "window-width",
.argdesc = "value",
.text = "Set the initial window width.\n"
"Default is 0 (automatic).",
},
{
.longopt = "window-height",
.argdesc = "value",
.text = "Set the initial window height.\n"
"Default is 0 (automatic).",
},
};
static void
print_option_usage_header(const struct sc_option *opt) {
struct sc_strbuf buf;
if (!sc_strbuf_init(&buf, 64)) {
goto error;
}
bool ok = true;
(void) ok; // only used for assertions
if (opt->shortopt) {
ok = sc_strbuf_append_char(&buf, '-');
assert(ok);
ok = sc_strbuf_append_char(&buf, opt->shortopt);
assert(ok);
if (opt->longopt) {
ok = sc_strbuf_append_staticstr(&buf, ", ");
assert(ok);
}
}
if (opt->longopt) {
ok = sc_strbuf_append_staticstr(&buf, "--");
assert(ok);
if (!sc_strbuf_append_str(&buf, opt->longopt)) {
goto error;
}
}
if (opt->argdesc) {
if (opt->optional_arg && !sc_strbuf_append_char(&buf, '[')) {
goto error;
}
if (!sc_strbuf_append_char(&buf, '=')) {
goto error;
}
if (!sc_strbuf_append_str(&buf, opt->argdesc)) {
goto error;
}
if (opt->optional_arg && !sc_strbuf_append_char(&buf, ']')) {
goto error;
}
}
fprintf(stderr, "\n %s\n", buf.s);
free(buf.s);
return;
error:
fprintf(stderr, "<ERROR>\n");
}
static void
print_option_usage(const struct sc_option *opt, unsigned cols) {
assert(cols > 8); // sc_str_wrap_lines() requires indent < columns
assert(opt->text);
print_option_usage_header(opt);
char *text = sc_str_wrap_lines(opt->text, cols, 8);
if (!text) {
fprintf(stderr, "<ERROR>\n");
return;
}
fprintf(stderr, "%s\n", text);
free(text);
}
void
scrcpy_print_usage(const char *arg0) {
const unsigned cols = 80; // For now, use a hardcoded value
fprintf(stderr, "Usage: %s [options]\n\n"
"Options:\n", arg0);
for (size_t i = 0; i < ARRAY_LEN(options); ++i) {
print_option_usage(&options[i], cols);
}
// Print shortcuts section
fprintf(stderr, "\n"
"Shortcuts:\n" "Shortcuts:\n"
"\n" "\n"
" In the following list, MOD is the shortcut modifier. By default,\n" " In the following list, MOD is the shortcut modifier. By default,\n"
@ -334,7 +509,7 @@ scrcpy_print_usage(const char *arg0) {
"\n" "\n"
" Drag & drop non-APK file\n" " Drag & drop non-APK file\n"
" Push file to device (see --push-target)\n" " Push file to device (see --push-target)\n"
"\n", arg0); "\n");
} }
static bool static bool

Loading…
Cancel
Save