Index: /branches/end_stage/ippTools/src/stacktool.c
===================================================================
--- /branches/end_stage/ippTools/src/stacktool.c	(revision 16096)
+++ /branches/end_stage/ippTools/src/stacktool.c	(revision 16097)
@@ -95,34 +95,10 @@
 
     // 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);
+
+    // optional
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     // we have to support multipe exp_ids
@@ -136,4 +112,5 @@
     psTime *registered = NULL;
     {
+        bool status;
         psString registeredStr = psMetadataLookupStr(&status, config->args, "-registered");
         if (!status) {
@@ -210,15 +187,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 +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 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 +219,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 +241,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 +290,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 +315,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 +364,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 +389,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 +448,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 +497,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)) {
