Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 15376)
+++ trunk/ippTools/src/stacktool.c	(revision 16170)
@@ -95,34 +95,11 @@
 
     // required options
-    bool status = false;
-    psString workdir = psMetadataLookupStr(&status, config->args, "-workdir");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -workdir");
-        return false;
-    }
-    if (!workdir) {
-        psError(PS_ERR_UNKNOWN, true, "-workdir is required");
-        return false;
-    }
-
-    psString skycell_id = psMetadataLookupStr(&status, config->args, "-skycell_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -skycell_id");
-        return false;
-    }
-    if (!skycell_id) {
-        psError(PS_ERR_UNKNOWN, true, "-skycell_id is required");
-        return false;
-    }
-
-    psString tess_id = psMetadataLookupStr(&status, config->args, "-tess_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -tess_id");
-        return false;
-    }
-    if (!tess_id) {
-        psError(PS_ERR_UNKNOWN, true, "-tess_id is required");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(workdir, config->args, "-workdir", true, false);
+    PXOPT_LOOKUP_STR(skycell_id, config->args, "-skycell_id", true, false);
+    PXOPT_LOOKUP_STR(tess_id, config->args, "-tess_id", true, false);
+
+    // default
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_TIME(registered, config->args, "-registered", false, false);
 
     // we have to support multipe exp_ids
@@ -132,19 +109,4 @@
         psError(PS_ERR_UNKNOWN, true, "-warp_id is required");
         return false;
-    }
-
-    psTime *registered = NULL;
-    {
-        psString registeredStr = psMetadataLookupStr(&status, config->args, "-registered");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -registered");
-            return false;
-        }
-        // pass through NULL as this is an optional field
-        if (registeredStr) {
-            registered = psTimeFromISO(registeredStr, PS_TIME_UTC);
-        } else {
-            registered = NULL;
-        }
     }
 
@@ -158,5 +120,4 @@
             tess_id
     );
-    psFree(registered);
     if (!run) {
         psError(PS_ERR_UNKNOWN, false, "failed to alloc stackRun object");
@@ -210,15 +171,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            psFree(run);
-            return false;
-        }
-    }
-
     if (!stackRunPrintObject(stdout, run, !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print object");
@@ -237,24 +187,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString stack_id = psMetadataLookupStr(&status, config->args, "-stack_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -stack_id");
-        return false;
-    }
-    if (!stack_id) {
-        psError(PS_ERR_UNKNOWN, true, "-stack_id is required");
-        return false;
-    }
-
-    psString state = psMetadataLookupStr(&status, config->args, "-state");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -state");
-        return false;
-    }
-    if (!state) {
-        psError(PS_ERR_UNKNOWN, true, "-state is required");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(stack_id, config->args, "-stack_id", true, false);
+    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
 
     if (state) {
@@ -271,26 +203,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString stack_id = psMetadataLookupStr(&status, config->args, "-stack_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -stack_id");
-        return false;
-    }
-    if (!stack_id) {
-        psError(PS_ERR_UNKNOWN, true, "-stack_id is required");
-        return false;
-
-    }
-
-    psString warp_id = psMetadataLookupStr(&status, config->args, "-warp_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -warp_id");
-        return false;
-    }
-    if (!warp_id) {
-        psError(PS_ERR_UNKNOWN, true, "-warp_id is required");
-        return false;
-    }
-
+    PXOPT_LOOKUP_STR(stack_id, config->args, "-stack_id", true, false);
+    PXOPT_LOOKUP_STR(warp_id, config->args, "-warp_id", true, false);
 
     // XXX need to validate the warp_id here
@@ -313,10 +225,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("stacktool_inputskyfile.sql");
@@ -366,14 +274,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
@@ -401,10 +299,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("stacktool_tosum.sql");
@@ -454,14 +348,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
@@ -489,53 +373,16 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString stack_id = psMetadataLookupStr(&status, config->args, "-stack_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -stack_id");
-        return false;
-    }
-    if (!stack_id) {
-        psError(PS_ERR_UNKNOWN, true, "-stack_id is required");
-        return false;
-
-    }
-
-    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
-        return false;
-    }
-
-    psString path_base = psMetadataLookupStr(&status, config->args, "-path_base");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -path_base");
-        return false;
-    }
+    // required
+    PXOPT_LOOKUP_STR(stack_id, config->args, "-stack_id", true, false);
 
     // optional
-    psF64 bg = psMetadataLookupF64(&status, config->args, "-bg");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg");
-        return false;
-    }
-
-    psF64 bg_stdev = psMetadataLookupF64(&status, config->args, "-bg_stdev");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -bg_stdev");
-        return false;
-    }
-
-    psF64 good_frac = psMetadataLookupF64(&status, config->args, "-good_frac");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -good_frac");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
+    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
+    PXOPT_LOOKUP_F64(bg, config->args, "-bg", false, false);
+    PXOPT_LOOKUP_F64(bg_stdev, config->args, "-bg_stdev", false, false);
+    PXOPT_LOOKUP_F64(good_frac, config->args, "-good_frac", false, false);
 
     // default values
-    psS16 code = psMetadataLookupS16(&status, config->args, "-code");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
-        return false;
-    }
+    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
 
     if (!psDBTransaction(config->dbh)) {
@@ -585,10 +432,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("stacktool_sumskyfile.sql");
@@ -638,14 +481,4 @@
     }
 
-    bool simple = false;
-    {
-        bool status = false;
-        simple = psMetadataLookupBool(&status, config->args, "-simple");
-        if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
-            return false;
-        }
-    }
-
     if (psArrayLength(output)) {
         if (!convertIdToStr(output)) {
