Index: trunk/ippTools/src/pstamptool.c
===================================================================
--- trunk/ippTools/src/pstamptool.c	(revision 18531)
+++ trunk/ippTools/src/pstamptool.c	(revision 18561)
@@ -102,29 +102,8 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
-        return false;
-    }
-    if (!uri) {
-        psError(PS_ERR_UNKNOWN, true, "-uri is required");
-        return false;
-    }
-    psString outProduct = psMetadataLookupStr(&status, config->args, "-out_product");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to looku value for -out_product");
-        return false;
-    }
-    if (!outProduct) {
-        psError(PS_ERR_UNKNOWN, true, "-out_product is required");
-        return false;
-    }
-
-    psString lastFileset = psMetadataLookupStr(&status, config->args, "-last_fileset");
-    psString state = psMetadataLookupStr(&status, config->args, "-state");
-    if (!state) {
-        state = "enabled";
-    }
+    PXOPT_LOOKUP_STR(uri,         config->args, "-uri",           true, false);
+    PXOPT_LOOKUP_STR(outProduct,  config->args, "-out_product",   true, false);
+    PXOPT_LOOKUP_STR(lastFileset, config->args, "-last_fileset", false, false);
+    PXOPT_LOOKUP_STR(state,       config->args, "-state",         false, false);
 
     if (!pstampDataStoreInsert(config->dbh,
@@ -144,18 +123,24 @@
 static bool datastoreMode(pxConfig *config)
 {
-    bool status = false;
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psString ds_id = psMetadataLookupStr(&status, config->args, "-ds_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -ds_id");
-        return false;
-    }
-
-    psString query = NULL;
-    psStringAppend(&query, "SELECT * FROM pstampDataStore");
-    if (ds_id) {
-        psStringAppend(&query, " WHERE ds_id = '%s'", ds_id);
-    }
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-ds_id", "ds_id", "==");
+
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    psString query = pxDataGet("pstamptool_datastore.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereSQL(where, NULL);
+        psStringAppend(&query, " %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
     if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -172,14 +157,4 @@
         psFree(output);
         return true;
-    }
-
-    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;
-        }
     }
 
@@ -197,27 +172,9 @@
 static bool moddatastoreMode(pxConfig *config)
 {
-    bool    status;
-
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psString ds_id = psMetadataLookupStr(&status, config->args, "-ds_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -ds_id");
-        return false;
-    }
-    if (!ds_id) {
-        psError(PS_ERR_UNKNOWN, true, "-ds_id is required");
-        return false;
-    }
-    psString lastFileset = psMetadataLookupStr(&status, config->args, "-last_fileset");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -last_fileset");
-        return false;
-    }
-    psString state = psMetadataLookupStr(&status, config->args, "-state");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -state");
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_STR(ds_id,       config->args, "-ds_id",         true, false);
+    PXOPT_LOOKUP_STR(lastFileset, config->args, "-last_fileset",  false, false);
+    PXOPT_LOOKUP_STR(state,       config->args, "-state",         false, false);
 
     if (!state && !lastFileset) {
@@ -226,9 +183,7 @@
     }
 
-    char *query = NULL;
+    char *query = psStringCopy ("UPDATE pstampDataStore SET");
     bool needComma = false;
     
-    psStringAppend(&query, "UPDATE pstampDataStore SET");
-
     if (lastFileset) {
         psStringAppend(&query, " lastFileset = '%s'", lastFileset);
@@ -237,6 +192,5 @@
 
     if (state) {
-        psStringAppend(&query, "%s state = '%s'", needComma ? "," : " ",
-            state);
+        psStringAppend(&query, "%s state = '%s'", needComma ? "," : " ", state);
         needComma = true; // be ready in case we add another field
     }
@@ -262,27 +216,9 @@
 static bool addReqMode(pxConfig *config)
 {
-    bool    status;
-
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psString uri = psMetadataLookupStr(&status, config->args, "-uri");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -uri");
-        return false;
-    }
-    if (!uri) {
-        psError(PS_ERR_UNKNOWN, true, "-uri is required");
-        return false;
-    }
-
-    // Data Store ID is optional
-    psString ds_id = psMetadataLookupStr(&status, config->args, "-ds_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -ds_id");
-        return false;
-    }
-    if (!ds_id) {
-        ds_id = "0";
-    }
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_STR(uri,         config->args, "-uri",           true, false);
+    // PXOPT_LOOKUP_STR(outFileset,  config->args, "-out_fileset",   true, false);
+    PXOPT_LOOKUP_STR(ds_id,       config->args, "-ds_id",         false, false);
 
     char *query ="INSERT INTO pstampRequest"
@@ -313,23 +249,22 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
-        return false;
-    }
-
-    psString query = psStringCopy(
-            "SELECT"
-            " *"
-            " FROM pstampRequest"
-            " WHERE state = 'new'"
-        );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "pstampRequest");
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-ds_id", "ds_id", "==");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    psString query = pxDataGet("pstamptool_pendingreq.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampRequest");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -358,15 +293,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(output);
-            return false;
-        }
-    }
-
     // negative simple so the default is true
     if (!ippdbPrintMetadatas(stdout, output, "pstampRequest", !simple)) {
@@ -380,21 +304,19 @@
     return true;
 }
-static bool completedReqMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
-        return false;
-    }
+
+static bool listReqMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_STR(req_id,  config->args, "-req_id", true, false);
+    PXOPT_LOOKUP_U64(limit,   config->args, "-limit",  false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = NULL;
     psStringAppend(&query, 
-        "SELECT * FROM pstampRequest WHERE state = 'run' AND "
-            "(SELECT count(*) FROM pstampJob WHERE pstampJob.req_id = pstampRequest.req_id "
-            " AND pstampJob.state != 'stop') = 0" );
-
+		   "SELECT * FROM pstampRequest WHERE state = 'run' AND "
+		   "(SELECT count(*) FROM pstampJob WHERE pstampJob.req_id = pstampRequest.req_id "
+		   " AND pstampJob.state != 'stop') = 0" );
+    
     // treat limit == 0 as "no limit"
     if (limit) {
@@ -409,4 +331,5 @@
         return false;
     }
+    psFree(query);
 
     psArray *output = p_psDBFetchResult(config->dbh);
@@ -415,20 +338,8 @@
         return false;
     }
-
     if (!psArrayLength(output)) {
         psTrace("pstamptool", PS_LOG_INFO, "no rows found");
         psFree(output);
         return true;
-    }
-
-    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(output);
-            return false;
-        }
     }
 
@@ -445,30 +356,15 @@
 }
 
-static bool listReqMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
-        return false;
-    }
-
-    psString query = NULL;
-
-    psString req_id = psMetadataLookupStr(&status, config->args, "-req_id");
-    
-    if (req_id) {
-        psStringAppend(&query,
-                "SELECT"
-                " *"
-                " FROM pstampRequest"
-                " WHERE req_id = %s", req_id
-            );
-    } else {
-        fprintf(stderr, "req_id must be specified\n");
-        exit (1);
-    }
+static bool completedReqMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    psString query = 
+        "SELECT * FROM pstampRequest WHERE state = 'run' AND "
+            "(SELECT count(*) FROM pstampJob WHERE pstampJob.req_id = pstampRequest.req_id "
+            " AND pstampJob.state != 'stop') = 0" ;
 
     // treat limit == 0 as "no limit"
@@ -484,5 +380,4 @@
         return false;
     }
-    psFree(query);
 
     psArray *output = p_psDBFetchResult(config->dbh);
@@ -491,19 +386,9 @@
         return false;
     }
+
     if (!psArrayLength(output)) {
         psTrace("pstamptool", PS_LOG_INFO, "no rows found");
         psFree(output);
         return true;
-    }
-
-    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(output);
-            return false;
-        }
     }
 
@@ -522,48 +407,13 @@
 static bool processedReqMode(pxConfig *config)
 {
-    bool    status;
-
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psString req_id = psMetadataLookupStr(&status, config->args, "-req_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -req_id");
-        return false;
-    }
-    if (!req_id) {
-        psError(PS_ERR_UNKNOWN, true, "-req_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;
-    }
-    psString outProduct = psMetadataLookupStr(&status, config->args, "-outProduct");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -outProduct");
-        return false;
-    }
-    psString fault = psMetadataLookupStr(&status, config->args, "-fault");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -fault");
-        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 name = psMetadataLookupStr(&status, config->args, "-name");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -name");
-        return false;
-    }
-    psString reqType = psMetadataLookupStr(&status, config->args, "-reqType");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -reqType");
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_STR(req_id,     config->args, "-req_id",     true, false);
+    PXOPT_LOOKUP_STR(state,      config->args, "-state",      false, false);
+    PXOPT_LOOKUP_STR(outProduct, config->args, "-outProduct", false, false);
+    PXOPT_LOOKUP_STR(fault, 	 config->args, "-fault",      false, false);
+    PXOPT_LOOKUP_STR(uri,   	 config->args, "-uri",        false, false);
+    PXOPT_LOOKUP_STR(name,  	 config->args, "-name",       false, false);
+    PXOPT_LOOKUP_STR(reqType,    config->args, "-reqType",    false, false);
 
     psString query = NULL;
@@ -621,18 +471,7 @@
 static bool revertReqMode(pxConfig *config)
 {
-    bool    status;
-
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psS64 req_id = psMetadataLookupS64(&status, config->args, "-req_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -req_id");
-        return false;
-    }
-    if (!req_id) {
-        psError(PS_ERR_UNKNOWN, true, "-req_id is required");
-        return false;
-    }
-        
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_S64(req_id,     config->args, "-req_id",     true, false);
 
     printf("Revert request %" PRId64 "\n", req_id);
@@ -653,106 +492,41 @@
 static bool addJobMode(pxConfig *config)
 {
-    bool    status;
     bool    stampJob = false;
-
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psString job_type = psMetadataLookupStr(&status, config->args, "-job_type");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -job_type");
-        return false;
-    }
-    if (job_type) {
-        if (!strcmp(job_type, "get_image") || !strcmp(job_type, "detect_query")) {
-            stampJob = false;
-        } else if (!strcmp(job_type, "stamp")) {
-            stampJob = true;
-        } else {
-            psError(PS_ERR_UNKNOWN, false, "unknown value for -job_type: %s", job_type);
-            return false;
-        }
+    char   *query = NULL;
+
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_STR(uri,         config->args, "-uri",        true, false);
+    PXOPT_LOOKUP_STR(req_id,      config->args, "-req_id",     true, false);
+    PXOPT_LOOKUP_STR(rownum,      config->args, "-rownum",     true, false);
+    PXOPT_LOOKUP_STR(job_type,    config->args, "-job_type",   false, false);
+    PXOPT_LOOKUP_STR(outputBase,  config->args, "-outputBase", true,  false);
+    PXOPT_LOOKUP_STR(argString,   config->args, "-args",       false, false);
+    PXOPT_LOOKUP_STR(stateString, config->args, "-state",      false, false);
+    PXOPT_LOOKUP_STR(fault,       config->args, "-fault",      false, false);
+
+    // default value for job_type is defined in pstamptoolConfig.c
+    if (!strcmp(job_type, "get_image") || !strcmp(job_type, "detect_query")) {
+	stampJob = false;
+    } else if (!strcmp(job_type, "stamp")) {
+	stampJob = true;
     } else {
-        job_type = "stamp";
-        stampJob = true;
-    }
-
-    psString rownum = psMetadataLookupStr(&status, config->args, "-rownum");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -rownum");
-        return false;
-    }
-    if (!rownum) {
-        psError(PS_ERR_UNKNOWN, true, "-rownum 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;
-    }
-    if (!uri) {
-        psError(PS_ERR_UNKNOWN, true, "-uri is required");
-        return false;
-    }
-
-    psString req_id = psMetadataLookupStr(&status, config->args, "-req_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -req_id");
-        return false;
-    }
-    if (!req_id) {
-        psError(PS_ERR_UNKNOWN, true, "-req_id is required");
-        return false;
-    }
-
-    psString outputBase = psMetadataLookupStr(&status, config->args, "-outputBase");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -outputBase");
-        return false;
-    }
-    if (!outputBase) {
-        psError(PS_ERR_UNKNOWN, true, "-outputBase is required");
-        return false;
-    }
-
-    psString argString = psMetadataLookupStr(&status, config->args, "-args");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -args");
-        return false;
-    }
-    if (!argString) {
-        if (stampJob) {
-            psError(PS_ERR_UNKNOWN, true, "-args is required");
-            return false;
-        }
-    }
-
-    psString stateString = psMetadataLookupStr(&status, config->args, "-state");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -state");
-        return false;
-    }
-    if (!stateString) {
-        stateString = "run";
-    }
-    psString fault = psMetadataLookupStr(&status, config->args, "-fault");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -fault");
-        return false;
-    }
-    if (!fault) {
-        fault = "0";
-    }
-
-    char *query;
-    
+	psError(PS_ERR_UNKNOWN, false, "unknown value for -job_type: %s", job_type);
+	return false;
+    }
+
+    if (stampJob && !argString) {
+	psError(PS_ERR_UNKNOWN, true, "-args is required for stamp job");
+	return false;
+    }
+
     if (stampJob) {
-        query ="INSERT INTO pstampJob"
-                 " (req_id, rownum, state, jobType, uri, outputBase, fault, args)"
-                 " VALUES( %s, '%s', '%s', '%s', '%s', '%s', %s, '%s')";
+	query = pxDataGet("pstamptool_addjob_stampjob.sql");
     } else {
-        query ="INSERT INTO pstampJob"
-                 " (req_id, rownum, state, jobType, uri, outputBase, fault)"
-                 " VALUES( %s, '%s', '%s', '%s', '%s', '%s', %s)";
+	query = pxDataGet("pstamptool_addjob_otherjob.sql");
+    }
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
     }
 
@@ -780,16 +554,17 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
-        return false;
-    }
+    PXOPT_LOOKUP_STR(req_id, config->args, "-req_id", false, false);
+    PXOPT_LOOKUP_STR(job_id, config->args, "-job_id", false, false);
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = NULL;
 
-    psString req_id = psMetadataLookupStr(&status, config->args, "-req_id");
-    psString job_id = psMetadataLookupStr(&status, config->args, "-job_id");
-    
+    if (!req_id && !job_id) {
+        fprintf(stderr, "either req_id or job_id must be specified\n");
+        exit (1);
+    }
+
     if (req_id) {
         psStringAppend(&query,
@@ -799,5 +574,5 @@
                 " WHERE req_id = %s", req_id
             );
-    } else if (job_id) {
+    } else {
         psStringAppend(&query,
                 "SELECT"
@@ -806,8 +581,5 @@
                 " WHERE job_id = %s", job_id
             );
-    } else {
-        fprintf(stderr, "either req_id or job_id must be specified\n");
-        exit (1);
-    }
+    } 
 
     // treat limit == 0 as "no limit"
@@ -836,15 +608,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(output);
-            return false;
-        }
-    }
-
     // negative simple so the default is true
     if (!ippdbPrintMetadatas(stdout, output, "pstampJob", !simple)) {
@@ -863,23 +624,23 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    bool status = false;
-    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -limit");
-        return false;
-    }
-
-    psString query = psStringCopy(
-            "SELECT"
-            " *"
-            " FROM pstampJob"
-            " WHERE state = 'run'"
-        );
-
-    if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "pstampJob");
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where, "-cam_id", "cam_id", "==");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
+    psString query = pxDataGet("pstamptool_pendingjob.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    // use psDBGenerateWhereSQL because the SQL yields an intermediate table
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampJob");
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -908,15 +669,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(output);
-            return false;
-        }
-    }
-
     // negative simple so the default is true
     if (!ippdbPrintMetadatas(stdout, output, "pstampJob", !simple)) {
@@ -933,31 +683,9 @@
 static bool processedJobMode(pxConfig *config)
 {
-    bool    status;
-
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    psString job_id = psMetadataLookupStr(&status, config->args, "-job_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -job_id");
-        return false;
-    }
-    if (!job_id) {
-        psError(PS_ERR_UNKNOWN, true, "-job_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;
-    }
-    psString fault = psMetadataLookupStr(&status, config->args, "-fault");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -fault");
-        return false;
-    }
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_STR(job_id,    config->args, "-job_id", true, false);
+    PXOPT_LOOKUP_STR(state,     config->args, "-state",  true, false);
+    PXOPT_LOOKUP_STR(fault,     config->args, "-fault",  false, false);
 
     psString faultStr = NULL;
@@ -967,9 +695,9 @@
         psStringAppend(&faultStr, ", fault = '%s'", fault);
     }
-        
+
     char *query ="UPDATE pstampJob"
-                 " SET state = '%s' %s"
-                 " WHERE job_id = '%s'";
-
+	" SET state = '%s' %s"
+	" WHERE job_id = '%s'";
+    
     if (!p_psDBRunQuery(config->dbh, query, state, faultStr, job_id)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
