Index: /branches/end_stage/ippTools/src/difftool.c
===================================================================
--- /branches/end_stage/ippTools/src/difftool.c	(revision 16099)
+++ /branches/end_stage/ippTools/src/difftool.c	(revision 16100)
@@ -97,37 +97,14 @@
 
     // 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);
 
     psTime *registered = NULL;
     {
+        bool status;
         psString registeredStr = psMetadataLookupStr(&status, config->args, "-registered");
         if (!status) {
@@ -166,15 +143,4 @@
     run->diff_id = psDBLastInsertID(config->dbh);
 
-    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 (!diffRunPrintObject(stdout, run, !simple)) {
             psError(PS_ERR_UNKNOWN, false, "failed to print object");
@@ -193,24 +159,7 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString diff_id = psMetadataLookupStr(&status, config->args, "-diff_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -diff_id");
-        return false;
-    }
-    if (!diff_id) {
-        psError(PS_ERR_UNKNOWN, true, "-diff_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;
-    }
+    // required options
+    PXOPT_LOOKUP_STR(diff_id, config->args, "-diff_id", true, false);
+    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
 
     if (state) {
@@ -227,39 +176,14 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString diff_id = psMetadataLookupStr(&status, config->args, "-diff_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -diff_id");
-        return false;
-    }
-    if (!diff_id) {
-        psError(PS_ERR_UNKNOWN, true, "-diff_id is required");
-        return 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;
-    }
-
-    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;
-    }
-
-    psString kind = psMetadataLookupStr(&status, config->args, "-kind");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -kind");
-        return false;
-    }
+    // required options
+    PXOPT_LOOKUP_STR(diff_id, config->args, "-diff_id", true, false);
+
+    // optional
+    PXOPT_LOOKUP_STR(stack_id, config->args, "-stack_id", false, false);
+    PXOPT_LOOKUP_STR(warp_id, config->args, "-warp_id", false, false);
+    PXOPT_LOOKUP_STR(kind, config->args, "-kind", false, false);
 
     // defaults to false
-    bool template = psMetadataLookupBool(&status, config->args, "-template");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -template");
-        return false;
-    }
+    PXOPT_LOOKUP_BOOL(template, config->args, "-template", false);
 
     // must provide either stack_id or warp_id but not BOTH
@@ -345,4 +269,5 @@
     }
 
+    bool status;
     psS32 count = psMetadataLookupS32(&status, output->data[0], "count(diff_id)");
 
@@ -371,10 +296,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);
 
     // find all rawImfiles matching the default query
@@ -425,14 +346,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)) {
@@ -460,10 +371,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("difftool_todiffskyfile.sql");
@@ -513,14 +420,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)) {
@@ -548,26 +445,11 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString diff_id = psMetadataLookupStr(&status, config->args, "-diff_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -diff_id");
-        return false;
-    }
-    if (!diff_id) {
-        psError(PS_ERR_UNKNOWN, true, "-diff_id is required");
-        return false;
-    }
-
-    psS16 code = psMetadataLookupS16(&status, config->args, "-code");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
-        return false;
-    }
-
-    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
-        return false;
-    }
+    // required
+    PXOPT_LOOKUP_STR(diff_id, config->args, "-diff_id", true, false);
+
+    // default to 0
+    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
+
+    PXOPT_LOOKUP_STR(uri, config->args, "-uri", true, false);
     if (code == 0 && !uri) {
         psError(PS_ERR_UNKNOWN, true, "-uri is required");
@@ -575,9 +457,5 @@
     }
 
-    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;
-    }
+    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", true, false);
     if (code == 0 && !path_base) {
         psError(PS_ERR_UNKNOWN, true, "-path_base is required");
@@ -586,21 +464,7 @@
 
     // 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_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);
 
     if (!psDBTransaction(config->dbh)) {
@@ -647,10 +511,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("difftool_skyfile.sql");
@@ -700,14 +560,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)) {
@@ -791,7 +641,4 @@
 
 
-
-
-
 static bool definepoprunMode(pxConfig *config)
 {
@@ -799,45 +646,12 @@
 
     // 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;
-    }
-
-    psString template_stack_id = psMetadataLookupStr(&status, config->args, "-template_stack_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -template_stack_id");
-        return false;
-    }
-    psString template_warp_id = psMetadataLookupStr(&status, config->args, "-template_warp_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -template_warp_id");
-        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_STR(template_stack_id, config->args, "-template_stack_id", false, false);
+    PXOPT_LOOKUP_STR(template_warp_id, config->args, "-template_warp_id", false, false);
+
     if (template_stack_id && template_warp_id) {
         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Only one template can be defined.");
@@ -850,14 +664,6 @@
     }
 
-    psString input_stack_id = psMetadataLookupStr(&status, config->args, "-input_stack_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -input_stack_id");
-        return false;
-    }
-    psString input_warp_id = psMetadataLookupStr(&status, config->args, "-input_warp_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -input_warp_id");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(input_stack_id, config->args, "-input_stack_id", false, false);
+    PXOPT_LOOKUP_STR(input_warp_id, config->args, "-input_warp_id", false, false);
     if (input_stack_id && input_warp_id) {
         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Only one input can be defined.");
@@ -870,8 +676,7 @@
     }
 
-    psString kind = NULL;
-
     psTime *registered = NULL;
     {
+        bool status;
         psString registeredStr = psMetadataLookupStr(&status, config->args, "-registered");
         if (!status) {
@@ -887,13 +692,5 @@
     }
 
-    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;
-        }
-    }
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     diffRunRow *run = diffRunRowAlloc(
@@ -936,5 +733,5 @@
             skycell_id,
             tess_id,
-            kind
+            NULL    // kind
         )) {
         if (!psDBRollback(config->dbh)) {
@@ -953,5 +750,5 @@
             skycell_id,
             tess_id,
-            kind
+            NULL    // kind
         )) {
         if (!psDBRollback(config->dbh)) {
