[git commit] getopt: improve help text; code shrink

Denys Vlasenko vda.linux at googlemail.com
Thu Jan 24 10:36:00 UTC 2013


commit: http://git.busybox.net/busybox/commit/?id=ee3bc70cea2064d9243e7a39cd1c90dc66eb5e4a
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

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

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 util-linux/getopt.c |   59 +++++++++++++++++++++++++++-----------------------
 1 files changed, 32 insertions(+), 27 deletions(-)

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


More information about the busybox-cvs mailing list