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.
This commit is contained in:
2026-05-16 17:06:31 +03:00
parent d528314ae3
commit 68479bf07e
7 changed files with 51 additions and 49 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}