From 68479bf07e7b1c4cc32b4b17148eeda563492745 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sat, 16 May 2026 17:06:31 +0300 Subject: [PATCH] userspace: Fix getopt_long usage - Add missing null entry after the last long option - Don't duplicate illegal option message, getopt already prints the message as I do not suppress it. Also handle missing arguments. --- userspace/programs/audioctl/main.cpp | 24 ++++++++++++------------ userspace/programs/cp/main.cpp | 10 +++++----- userspace/programs/dirname/main.cpp | 6 +++--- userspace/programs/ls/main.cpp | 8 ++++---- userspace/programs/mv/main.cpp | 4 ++-- userspace/programs/rm/main.cpp | 25 +++++++++++++------------ userspace/programs/uname/main.cpp | 23 ++++++++++++----------- 7 files changed, 51 insertions(+), 49 deletions(-) diff --git a/userspace/programs/audioctl/main.cpp b/userspace/programs/audioctl/main.cpp index 7e4b2b45..71d52bcc 100644 --- a/userspace/programs/audioctl/main.cpp +++ b/userspace/programs/audioctl/main.cpp @@ -117,6 +117,7 @@ int main(int argc, char** argv) { "pin", required_argument, nullptr, 'p' }, { "volume", required_argument, nullptr, 'v' }, { "help", no_argument, nullptr, 'h' }, + {} }; int ch = getopt_long(argc, argv, "ld:p:v:h", long_options, nullptr); @@ -125,16 +126,6 @@ int main(int argc, char** argv) switch (ch) { - case 'h': - fprintf(stderr, "usage: %s [OPTIONS]...\n", argv[0]); - fprintf(stderr, " control the audio server\n"); - fprintf(stderr, "OPTIONS:\n"); - fprintf(stderr, " -l, --list list devices and their pins\n"); - fprintf(stderr, " -d, --device N set device index N as the current one\n"); - fprintf(stderr, " -p, --pin N set pin N as the current one\n"); - fprintf(stderr, " -v, --volume N set volume to N%%. if + or - is given, volume is relative to the current volume\n"); - fprintf(stderr, " -h, --help show this message and exit\n"); - return 0; case 'l': list = true; break; @@ -149,8 +140,17 @@ int main(int argc, char** argv) volume_rel = (optarg[0] == '-'); volume = parse_u32_or_exit(optarg + volume_rel.has_value()); break; - case '?': - fprintf(stderr, "invalid option %c\n", optopt); + case 'h': + fprintf(stderr, "usage: %s [OPTIONS]...\n", argv[0]); + fprintf(stderr, " control the audio server\n"); + fprintf(stderr, "OPTIONS:\n"); + fprintf(stderr, " -l, --list list devices and their pins\n"); + fprintf(stderr, " -d, --device N set device index N as the current one\n"); + fprintf(stderr, " -p, --pin N set pin N as the current one\n"); + fprintf(stderr, " -v, --volume N set volume to N%%. if + or - is given, volume is relative to the current volume\n"); + fprintf(stderr, " -h, --help show this message and exit\n"); + return 0; + case ':': case '?': fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; } diff --git a/userspace/programs/cp/main.cpp b/userspace/programs/cp/main.cpp index 16298e09..8d3d5298 100644 --- a/userspace/programs/cp/main.cpp +++ b/userspace/programs/cp/main.cpp @@ -165,6 +165,7 @@ int main(int argc, char** argv) static option long_options[] { { "help", no_argument, nullptr, 'h' }, { "recursive", no_argument, nullptr, 'r' }, + {} }; int ch = getopt_long(argc, argv, "hr", long_options, nullptr); @@ -173,17 +174,16 @@ int main(int argc, char** argv) switch (ch) { + case 'r': + recursive = true; + break; case 'h': printf("usage: %s [OPTIONS]... SOURCE... DEST\n", argv[0]); printf("Copies files SOURCE... to DEST\n"); printf("OPTIONS:\n"); printf(" -h, --help Show this message and exit\n"); return 0; - case 'r': - recursive = true; - break; - case '?': - fprintf(stderr, "invalid option %c\n", optopt); + case ':': case '?': fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; } diff --git a/userspace/programs/dirname/main.cpp b/userspace/programs/dirname/main.cpp index 65f6175a..7b2d194e 100644 --- a/userspace/programs/dirname/main.cpp +++ b/userspace/programs/dirname/main.cpp @@ -11,6 +11,7 @@ int main(int argc, char* argv[]) static option long_options[] { { "zero", no_argument, nullptr, 'z' }, { "help", no_argument, nullptr, 'h' }, + {} }; int ch = getopt_long(argc, argv, "zh", long_options, nullptr); @@ -29,8 +30,7 @@ int main(int argc, char* argv[]) fprintf(stderr, " -z, --zero end each output with NUL instead of a newline\n"); fprintf(stderr, " -h, --help show this message and exit\n"); return 0; - case '?': - fprintf(stderr, "invalid option %c\n", optopt); + case ':': case '?': fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; } @@ -38,7 +38,7 @@ int main(int argc, char* argv[]) if (optind >= argc) { - fprintf(stderr, "missing operand\n"); + fprintf(stderr, "%s: missing operand\n", argv[0]); fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; } diff --git a/userspace/programs/ls/main.cpp b/userspace/programs/ls/main.cpp index 02a9a6c0..1cbb54df 100644 --- a/userspace/programs/ls/main.cpp +++ b/userspace/programs/ls/main.cpp @@ -407,7 +407,8 @@ int main(int argc, char* argv[]) { "directory" , no_argument, nullptr, 'd' }, { "human-readable", no_argument, nullptr, 'h' }, { "list", no_argument, nullptr, 'l' }, - { "help", no_argument, nullptr, 'x' }, + { "help", no_argument, nullptr, 0 }, + {} }; int ch = getopt_long(argc, argv, "aAlh", long_options, nullptr); @@ -431,7 +432,7 @@ int main(int argc, char* argv[]) case 'l': config.show_as_list = true; break; - case 'x': + case 0: fprintf(stderr, "usage: %s [OPTION]... [FILE]...\n", argv[0]); fprintf(stderr, " list information about FILEs\n"); fprintf(stderr, "OPTIONS:\n"); @@ -442,8 +443,7 @@ int main(int argc, char* argv[]) fprintf(stderr, " -l, --list use long listing format\n"); fprintf(stderr, " --help show this message and exit\n"); return 0; - case '?': - fprintf(stderr, "invalid option %c\n", optopt); + case ':': case '?': fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; } diff --git a/userspace/programs/mv/main.cpp b/userspace/programs/mv/main.cpp index 42881437..31c5ae44 100644 --- a/userspace/programs/mv/main.cpp +++ b/userspace/programs/mv/main.cpp @@ -197,6 +197,7 @@ int main(int argc, char** argv) { static option long_options[] { { "help", no_argument, nullptr, 'h' }, + {} }; int ch = getopt_long(argc, argv, "h", long_options, nullptr); @@ -211,8 +212,7 @@ int main(int argc, char** argv) printf("OPTIONS:\n"); printf(" -h, --help Show this message and exit\n"); return 0; - case '?': - fprintf(stderr, "invalid option %c\n", optopt); + case ':': case '?': fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; } diff --git a/userspace/programs/rm/main.cpp b/userspace/programs/rm/main.cpp index d3e61981..7eb8c34f 100644 --- a/userspace/programs/rm/main.cpp +++ b/userspace/programs/rm/main.cpp @@ -102,6 +102,7 @@ int main(int argc, char** argv) { "interactive", no_argument, nullptr, 'i' }, { "force", no_argument, nullptr, 'f' }, { "help", no_argument, nullptr, 'h' }, + {} }; int ch = getopt_long(argc, argv, "rRifh", long_options, nullptr); @@ -110,15 +111,6 @@ int main(int argc, char** argv) switch (ch) { - case 'h': - fprintf(stderr, "usage: %s [OPTIONS]... FILE...\n", argv[0]); - fprintf(stderr, " remove each FILE\n"); - fprintf(stderr, "OPTIONS:\n"); - fprintf(stderr, " -r, -R, --recursive remove directories and their contents recursively\n"); - fprintf(stderr, " -i, --interactive prompt removal for all files\n"); - fprintf(stderr, " -f, --force ignore nonexistent files and never prompt\n"); - fprintf(stderr, " -h, --help show this message and exit\n"); - return 0; case 'r': case 'R': recursive = true; break; @@ -130,8 +122,16 @@ int main(int argc, char** argv) force = false; interactive = true; break; - case '?': - fprintf(stderr, "invalid option %c\n", optopt); + case 'h': + fprintf(stderr, "usage: %s [OPTIONS]... FILE...\n", argv[0]); + fprintf(stderr, " remove each FILE\n"); + fprintf(stderr, "OPTIONS:\n"); + fprintf(stderr, " -r, -R, --recursive remove directories and their contents recursively\n"); + fprintf(stderr, " -i, --interactive prompt removal for all files\n"); + fprintf(stderr, " -f, --force ignore nonexistent files and never prompt\n"); + fprintf(stderr, " -h, --help show this message and exit\n"); + return 0; + case ':': case '?': fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; } @@ -139,7 +139,8 @@ int main(int argc, char** argv) if (optind >= argc && !force) { - fprintf(stderr, "missing operand. use --help for more information\n"); + fprintf(stderr, "%s: missing operand\n", argv[0]); + fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; } diff --git a/userspace/programs/uname/main.cpp b/userspace/programs/uname/main.cpp index 2e436a7e..5762a531 100644 --- a/userspace/programs/uname/main.cpp +++ b/userspace/programs/uname/main.cpp @@ -20,6 +20,7 @@ int main(int argc, char** argv) { "kernel-name", no_argument, nullptr, 's' }, { "kernel-version", no_argument, nullptr, 'v' }, { "help", no_argument, nullptr, 0 }, + {} }; int ch = getopt_long(argc, argv, "amnrsv", long_options, nullptr); @@ -28,15 +29,6 @@ int main(int argc, char** argv) switch (ch) { - case 0: - printf("usage: %s [OPTION]...\n", argv[0]); - printf(" -a, --all print all information\n"); - printf(" -m, --machine print machine name\n"); - printf(" -r, --release print release\n"); - printf(" -n, --nodename print node name\n"); - printf(" -s, --system print system name\n"); - printf(" -v, --version print version\n"); - return 0; case 'a': machine = true; nodename = true; @@ -59,8 +51,17 @@ int main(int argc, char** argv) case 'v': version = true; break; - case '?': - fprintf(stderr, "invalid option %c\n", optopt); + case 0: + printf("usage: %s [OPTION]...\n", argv[0]); + printf(" -a, --all print all information\n"); + printf(" -m, --machine print machine name\n"); + printf(" -r, --release print release\n"); + printf(" -n, --nodename print node name\n"); + printf(" -s, --system print system name\n"); + printf(" -v, --version print version\n"); + printf(" --help show this message and exit\n"); + return 0; + case ':': case '?': fprintf(stderr, "see '%s --help' for usage\n", argv[0]); return 1; }