[pacman-dev] [PATCH 8/8] Replace hardcoded option numbers with enumeration

Laszlo Papp djszapi2 at gmail.com
Fri Sep 25 14:56:05 EDT 2009


Pacman's long option parsing used hardcoded numbers to identify them.
This is not good practice, so replace them with enumeration constants.

   ./src/pacman/conf.h:
       - This contains the enumeration of long options that are used
       in pacman.c source file.
   ./src/pacman/pacman.c
       - This source contains long options where the changing was
       happened. It's safer and more comfortable now, instead of
       hard coding 10-15 or more integer value into the code.

Signed-off-by: Laszlo Papp <djszapi at archlinux.us>
---
 src/pacman/conf.h   |   18 ++++++++++++++++
 src/pacman/pacman.c |   56 +++++++++++++++++++++++++-------------------------
 2 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/src/pacman/conf.h b/src/pacman/conf.h
index 3c588a7..c854df4 100644
--- a/src/pacman/conf.h
+++ b/src/pacman/conf.h
@@ -88,6 +88,24 @@ enum {
 	PM_OP_DEPTEST
 };
 
+/* Long Operations */
+enum {
+	PM_LONG_OP_NOCONFIRM = 1000,
+	PM_LONG_OP_CONFIG,
+	PM_LONG_OP_IGNORE,
+	PM_LONG_OP_DEBUG,
+	PM_LONG_OP_NOPROGRESSBAR,
+	PM_LONG_OP_NOSCRIPTLET,
+	PM_LONG_OP_ASK,
+	PM_LONG_OP_CACHEDIR,
+	PM_LONG_OP_ASDEPS,
+	PM_LONG_OP_LOGFILE,
+	PM_LONG_OP_IGNOREGROUP,
+	PM_LONG_OP_NEEDED,
+	PM_LONG_OP_ASEXPLICIT,
+	PM_LONG_OP_ARCH
+};
+
 /* clean method */
 enum {
 	PM_CLEAN_KEEPINST = 0, /* default */
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index ac51502..30b39b5 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -378,20 +378,20 @@ static int parseargs(int argc, char *argv[])
 		{"verbose",    no_argument,       0, 'v'},
 		{"downloadonly", no_argument,     0, 'w'},
 		{"refresh",    no_argument,       0, 'y'},
-		{"noconfirm",  no_argument,       0, 1000},
-		{"config",     required_argument, 0, 1001},
-		{"ignore",     required_argument, 0, 1002},
-		{"debug",      optional_argument, 0, 1003},
-		{"noprogressbar", no_argument,    0, 1004},
-		{"noscriptlet", no_argument,      0, 1005},
-		{"ask",        required_argument, 0, 1006},
-		{"cachedir",   required_argument, 0, 1007},
-		{"asdeps",     no_argument,       0, 1008},
-		{"logfile",    required_argument, 0, 1009},
-		{"ignoregroup", required_argument, 0, 1010},
-		{"needed",     no_argument,       0, 1011},
-		{"asexplicit",     no_argument,   0, 1012},
-		{"arch",       required_argument, 0, 1013},
+		{"noconfirm",  no_argument,       0, PM_LONG_OP_NOCONFIRM},
+		{"config",     required_argument, 0, PM_LONG_OP_CONFIG},
+		{"ignore",     required_argument, 0, PM_LONG_OP_IGNORE},
+		{"debug",      optional_argument, 0, PM_LONG_OP_DEBUG},
+		{"noprogressbar", no_argument,    0, PM_LONG_OP_NOPROGRESSBAR},
+		{"noscriptlet", no_argument,      0, PM_LONG_OP_NOSCRIPTLET},
+		{"ask",        required_argument, 0, PM_LONG_OP_ASK},
+		{"cachedir",   required_argument, 0, PM_LONG_OP_CACHEDIR},
+		{"asdeps",     no_argument,       0, PM_LONG_OP_ASDEPS},
+		{"logfile",    required_argument, 0, PM_LONG_OP_LOGFILE},
+		{"ignoregroup", required_argument, 0, PM_LONG_OP_IGNOREGROUP},
+		{"needed",     no_argument,       0, PM_LONG_OP_NEEDED},
+		{"asexplicit",     no_argument,   0, PM_LONG_OP_ASEXPLICIT},
+		{"arch",       required_argument, 0, PM_LONG_OP_ARCH},
 		{0, 0, 0, 0}
 	};
 
@@ -403,21 +403,21 @@ static int parseargs(int argc, char *argv[])
 		}
 		switch(opt) {
 			case 0: break;
-			case 1000: config->noconfirm = 1; break;
-			case 1001:
+			case PM_LONG_OP_NOCONFIRM: config->noconfirm = 1; break;
+			case PM_LONG_OP_CONFIG:
 				if(config->configfile) {
 					free(config->configfile);
 				}
 				config->configfile = strndup(optarg, PATH_MAX);
 				break;
-			case 1002:
+			case PM_LONG_OP_IGNORE:
 				list = strsplit(optarg, ',');
 				for(item = list; item; item = alpm_list_next(item)) {
 					alpm_option_add_ignorepkg((char *)alpm_list_getdata(item));
 				}
 				FREELIST(list);
 				break;
-			case 1003:
+			case PM_LONG_OP_DEBUG:
 				/* debug levels are made more 'human readable' than using a raw logmask
 				 * here, error and warning are set in config_new, though perhaps a
 				 * --quiet option will remove these later */
@@ -440,34 +440,34 @@ static int parseargs(int argc, char *argv[])
 				/* progress bars get wonky with debug on, shut them off */
 				config->noprogressbar = 1;
 				break;
-			case 1004: config->noprogressbar = 1; break;
-			case 1005: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
-			case 1006: config->noask = 1; config->ask = atoi(optarg); break;
-			case 1007:
+			case PM_LONG_OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
+			case PM_LONG_OP_NOSCRIPTLET: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
+			case PM_LONG_OP_ASK: config->noask = 1; config->ask = atoi(optarg); break;
+			case PM_LONG_OP_CACHEDIR:
 				if(alpm_option_add_cachedir(optarg) != 0) {
 					pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
 							optarg, alpm_strerrorlast());
 					return(1);
 				}
 				break;
-			case 1008:
+			case PM_LONG_OP_ASDEPS:
 				config->flags |= PM_TRANS_FLAG_ALLDEPS;
 				break;
-			case 1009:
+			case PM_LONG_OP_LOGFILE:
 				config->logfile = strndup(optarg, PATH_MAX);
 				break;
-			case 1010:
+			case PM_LONG_OP_IGNOREGROUP:
 				list = strsplit(optarg, ',');
 				for(item = list; item; item = alpm_list_next(item)) {
 					alpm_option_add_ignoregrp((char *)alpm_list_getdata(item));
 				}
 				FREELIST(list);
 				break;
-			case 1011: config->flags |= PM_TRANS_FLAG_NEEDED; break;
-			case 1012:
+			case PM_LONG_OP_NEEDED: config->flags |= PM_TRANS_FLAG_NEEDED; break;
+			case PM_LONG_OP_ASEXPLICIT:
 				config->flags |= PM_TRANS_FLAG_ALLEXPLICIT;
 				break;
-			case 1013:
+			case PM_LONG_OP_ARCH:
 				setarch(optarg);
 				break;
 			case 'Q': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
-- 
1.6.4.4



More information about the pacman-dev mailing list