getopt: improve help text; code shrink

function                                             old     new   delta
generate_output                                      356     351      -5
packed_usage                                       29271   29257     -14

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2013-01-24 11:36:00 +01:00
parent 243e733001
commit ee3bc70cea

View File

@ -35,27 +35,32 @@
//usage: "[OPTIONS] [--] OPTSTRING PARAMS" //usage: "[OPTIONS] [--] OPTSTRING PARAMS"
//usage:#define getopt_full_usage "\n\n" //usage:#define getopt_full_usage "\n\n"
//usage: IF_LONG_OPTS( //usage: IF_LONG_OPTS(
//usage: " -a,--alternative Allow long options starting with single -" //usage: IF_FEATURE_GETOPT_LONG(
//usage: "\n -l,--longoptions=LOPT[,...] Long options to be recognized" //usage: " -a,--alternative Allow long options starting with single -\n"
//usage: "\n -n,--name=PROGNAME The name under which errors are reported" //usage: " -l,--longoptions=LOPT[,...] Long options to recognize\n"
//usage: "\n -o,--options=OPTSTRING Short options to be recognized" //usage: )
//usage: "\n -q,--quiet Disable error reporting by getopt(3)" //usage: " -n,--name=PROGNAME The name under which errors are reported"
//usage: "\n -o,--options=OPTSTRING Short options to recognize"
//usage: "\n -q,--quiet No error messages on unrecognized options"
//usage: "\n -Q,--quiet-output No normal output" //usage: "\n -Q,--quiet-output No normal output"
//usage: "\n -s,--shell=SHELL Set shell quoting conventions" //usage: "\n -s,--shell=SHELL Set shell quoting conventions"
//usage: "\n -T,--test Test for getopt(1) version" //usage: "\n -T,--test Version test (exits with 4)"
//usage: "\n -u,--unquoted Don't quote the output" //usage: "\n -u,--unquoted Don't quote output"
//usage: ) //usage: )
//usage: IF_NOT_LONG_OPTS( //usage: IF_NOT_LONG_OPTS(
//usage: " -a Allow long options starting with single -" //usage: IF_FEATURE_GETOPT_LONG(
//usage: "\n -l LOPT[,...] Long options to be recognized" //usage: " -a Allow long options starting with single -\n"
//usage: "\n -n PROGNAME The name under which errors are reported" //usage: " -l LOPT[,...] Long options to recognize\n"
//usage: "\n -o OPTSTRING Short options to be recognized" //usage: )
//usage: "\n -q Disable error reporting by getopt(3)" //usage: " -n PROGNAME The name under which errors are reported"
//usage: "\n -o OPTSTRING Short options to recognize"
//usage: "\n -q No error messages on unrecognized options"
//usage: "\n -Q No normal output" //usage: "\n -Q No normal output"
//usage: "\n -s SHELL Set shell quoting conventions" //usage: "\n -s SHELL Set shell quoting conventions"
//usage: "\n -T Test for getopt(1) version" //usage: "\n -T Version test (exits with 4)"
//usage: "\n -u Don't quote the output" //usage: "\n -u Don't quote output"
//usage: ) //usage: )
//usage: IF_FEATURE_GETOPT_LONG( /* example uses -l, needs FEATURE_GETOPT_LONG */
//usage: "\n" //usage: "\n"
//usage: "\nExample:" //usage: "\nExample:"
//usage: "\n" //usage: "\n"
@ -73,6 +78,7 @@
//usage: "\n *) echo Error; exit 1;;" //usage: "\n *) echo Error; exit 1;;"
//usage: "\n esac" //usage: "\n esac"
//usage: "\ndone" //usage: "\ndone"
//usage: )
//usage: //usage:
//usage:#define getopt_example_usage //usage:#define getopt_example_usage
//usage: "$ cat getopt.test\n" //usage: "$ cat getopt.test\n"
@ -214,11 +220,6 @@ static const char *normalize(const char *arg)
static int generate_output(char **argv, int argc, const char *optstr, const struct option *longopts) static int generate_output(char **argv, int argc, const char *optstr, const struct option *longopts)
{ {
int exit_code = 0; /* We assume everything will be OK */ int exit_code = 0; /* We assume everything will be OK */
int opt;
#if ENABLE_FEATURE_GETOPT_LONG
int longindex;
#endif
const char *charptr;
if (quiet_errors) /* No error reporting from getopt(3) */ if (quiet_errors) /* No error reporting from getopt(3) */
opterr = 0; opterr = 0;
@ -233,13 +234,14 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru
#endif #endif
while (1) { while (1) {
opt =
#if ENABLE_FEATURE_GETOPT_LONG #if ENABLE_FEATURE_GETOPT_LONG
alternative ? int longindex;
getopt_long_only(argc, argv, optstr, longopts, &longindex) : int opt = alternative
getopt_long(argc, argv, optstr, longopts, &longindex); ? getopt_long_only(argc, argv, optstr, longopts, &longindex)
: getopt_long(argc, argv, optstr, longopts, &longindex)
;
#else #else
getopt(argc, argv, optstr); int opt = getopt(argc, argv, optstr);
#endif #endif
if (opt == -1) if (opt == -1)
break; break;
@ -257,9 +259,10 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru
if (opt == NON_OPT) if (opt == NON_OPT)
printf(" %s", normalize(optarg)); printf(" %s", normalize(optarg));
else { else {
const char *charptr;
printf(" -%c", opt); printf(" -%c", opt);
charptr = strchr(optstr, opt); charptr = strchr(optstr, opt);
if (charptr != NULL && *++charptr == ':') if (charptr && *++charptr == ':')
printf(" %s", printf(" %s",
normalize(optarg ? optarg : "")); normalize(optarg ? optarg : ""));
} }
@ -267,9 +270,11 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru
} }
if (!quiet_output) { if (!quiet_output) {
unsigned idx;
printf(" --"); printf(" --");
while (optind < argc) idx = optind;
printf(" %s", normalize(argv[optind++])); while (argv[idx])
printf(" %s", normalize(argv[idx++]));
bb_putchar('\n'); bb_putchar('\n');
} }
return exit_code; return exit_code;