Index: trunk/ippTools/src/detselectConfig.c
===================================================================
--- trunk/ippTools/src/detselectConfig.c	(revision 11856)
+++ trunk/ippTools/src/detselectConfig.c	(revision 13578)
@@ -78,54 +78,14 @@
             "use the simple output format", false);
     
-#define PXTOOL_MODE(option, modeval, argset) \
-{ \
-    int N = 0; \
-    if ((N = psArgumentGet (argc, argv, option))) { \
-        psArgumentRemove (N, &argc, argv); \
-        if (config->mode) { \
-            psError(PS_ERR_UNKNOWN, true, "only one mode selection is allowed"); \
-            psFree(config); \
-            return NULL; \
-        } \
-        config->mode = modeval; \
-        config->args = psMemIncrRefCounter(argset); \
-    } \
-    if (!psMetadataAddMetadata(argSets, PS_LIST_TAIL, option, 0, NULL, argset)) {;\
-        psError(PS_ERR_UNKNOWN, false, "failed to add argset for %s", option);\
-    } \
-    psFree(argset); \
-}
-
     psMetadata *argSets = psMetadataAlloc();
-    // find which mode we're running under
-    PXTOOL_MODE("-search",  DETSELECT_MODE_SEARCH, searchArgs);
-    PXTOOL_MODE("-select",  DETSELECT_MODE_SELECT, selectArgs);
-
-    bool argErr = false;
-    if (config->mode == DETSELECT_MODE_NONE) {
-        argErr = true;
-        fprintf (stderr, "mode argument is required\n");
-    } else if (! psArgumentParse(config->args, &argc, argv) || argc != 1) {
-        argErr = true;
-        fprintf (stderr, "error parsing arguments\n");
-    }
-
-    if (argErr) {
-        printf("\nPan-STARRS detrend select Tool\n");
-        printf("Usage: %s <mode> [<options>]\n\n", argv[0]);
-        printf(" <mode> :\n\n");
-
-        psMetadataIterator *iter = psMetadataIteratorAlloc(argSets, 0, NULL);
-        psMetadataItem *item = NULL;
-        while ((item = psMetadataGetAndIncrement(iter))) {
-            if (!item->type == PS_DATA_METADATA) {
-                psAbort("all options must be specified as a metadata");            }
-
-            fprintf(stdout, "%s ", item->name);
-            psArgumentHelp(item->data.md);
-        }
-        psFree(iter);
-
+    psMetadata *modes = psMetadataAlloc();
+
+    PXTOOL_ADD_MODE("-search",  "search for an appropriate detrend", DETSELECT_MODE_SEARCH, searchArgs);
+    PXTOOL_ADD_MODE("-select",  "retreive detrend information", DETSELECT_MODE_SELECT, selectArgs);
+
+    if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
+        psError(PS_ERR_UNKNOWN, true, "option parsing failed");
         psFree(argSets);
+        psFree(modes);
         psFree(config);
         return NULL;
@@ -133,4 +93,5 @@
 
     psFree(argSets);
+    psFree(modes);
 
     // setup search criterion
@@ -262,8 +223,4 @@
     }
 
-    // save argv/argc
-    config->argv = argv;
-    config->argc = argc;
-
     return config;
 }
