IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 4794


Ignore:
Timestamp:
Aug 16, 2005, 1:18:38 PM (21 years ago)
Author:
Paul Price
Message:

Expanding discussion of psArguments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/pslib/psLibSDRS.tex

    r4719 r4794  
    1 %%% $Id: psLibSDRS.tex,v 1.324 2005-08-05 20:24:03 price Exp $
     1%%% $Id: psLibSDRS.tex,v 1.325 2005-08-16 23:18:38 price Exp $
    22\documentclass[panstarrs,spec]{panstarrs}
    33
     
    16661666(supplied via \code{argc, argv}) into a metadata container of
    16671667\code{arguments}.  The input \code{arguments} shall contain the list
    1668 of possible arguments (lacking a leading dash) as the keywords
    1669 providing the default values (of the appropriate type).  As matching
    1670 arguments are found on the command line, the values shall be read into
    1671 the \code{arguments} metadata, with the appropriate type.  Multiple
    1672 values (i.e., when an argument takes two or more values) are specified
    1673 using a \code{PS_DATA_METADATA_MULTI} list (i.e., by specifying a type
    1674 with the \code{PS_META_DUPLICATE_OK} flag).  A boolean argument shall
    1675 be set to \code{true} by the presence of the argument itself (no value
    1676 is specified).  The arguments and their values shall be removed from
    1677 the list of command line arguments as they are processed.  The
    1678 function shall return \code{false} without modifying the
    1679 \code{arguments} if any argument (i.e., a string beginning with a
    1680 dash; but not a string that does not start with a dash, which is
    1681 likely a mandatory argument for the program) was encountered that is
    1682 not present in the \code{arguments}.
     1668of possible arguments as the keywords providing the default values (of
     1669the appropriate type).  As matching arguments are found on the command
     1670line, the values shall be read into the \code{arguments} metadata,
     1671with the appropriate type.  Multiple values (i.e., when an argument
     1672takes two or more values) are specified using a
     1673\code{PS_DATA_METADATA_MULTI} list (i.e., by specifying a type with
     1674the \code{PS_META_DUPLICATE_OK} flag).  A boolean argument shall be
     1675set to \code{true} by the presence of the argument itself (no value is
     1676specified).  The arguments and their values shall be removed from the
     1677list of command line arguments as they are processed.  The function
     1678shall return \code{false} without modifying the \code{arguments} if
     1679any argument (i.e., a string beginning with a dash; but not a string
     1680that does not start with a dash, which is likely a mandatory argument
     1681for the program; the purpose of this requirement is so that the
     1682default values are preserved) was encountered that is not present in
     1683the \code{arguments}.
    16831684
    16841685\begin{prototype}
     
    16901691\code{psArgumentParse}) of the form:
    16911692\begin{verbatim}
    1692 Optional arguments:
    1693     -names      FRED    This is the comment from the first value
    1694                 JIM     This is the comment from the second value
    1695                 BOB     This is the comment from the third value
    1696     -answer     42      This is a comment
    1697     -truth      FALSE   This is another comment
     1693Optional arguments, with default values:
     1694    -names     (FRED)     This is the comment from the first value
     1695               (JIM)      This is the comment from the second value
     1696               (BOB)      This is the comment from the third value
     1697    -answer    (42)       This is a comment
     1698    -truth     (FALSE)    This is another comment
    16981699\end{verbatim}
    16991700Here the \code{arguments} contains three keywords, \code{names, magic,
     
    17041705the mandatory arguments.  An example follows:
    17051706\begin{verbatim}
    1706 void help(psMetadata *arguments,        // Command-line arguments
    1707           const char *programName       // Name of the program = argv[0]
    1708     )
    1709 {
    1710     printf("Pan-STARRS Phase 2 processing\n\n");
    1711     printf("Usage: %s INPUT.fits OUTPUT.fits\n\n", programName);
    1712     psArgumentHelp(arguments);
    1713 }
    1714 
    17151707int main(int argc, char *argv[])
    17161708{
    17171709    // Parse optional command-line arguments
    17181710    psMetadata *arguments = psMetadataAlloc(); // The arguments, with default values
    1719     psMetadataAddStr(arguments, PS_LIST_TAIL, "bias", "Name of the bias image", NULL);
    1720     psMetadataAddStr(arguments, PS_LIST_TAIL, "dark", "Name of the dark image", NULL);
    1721     psMetadataAddStr(arguments, PS_LIST_TAIL, "flat", "Name of the flat-field image", NULL);
    1722     psMetadataAddS32(arguments, PS_LIST_TAIL, "chip", "Chip number to process (if positive)", -1);
    1723     psMetadataAddS32(arguments, PS_LIST_TAIL | PS_META_DUPLICATE_OK, "timerange", "The lower time value", 0);
    1724     psMetadataAddS32(arguments, PS_LIST_TAIL, "timerange", "The upper time value", 10);
    1725     psMetadataAddBool(arguments, PS_LIST_TAIL, "ok", "Is this OK?", FALSE);
    1726     if (! psArgumentParse(arguments, &argc, argv) || argc != 2) {
    1727         help(arguments, argv[0]);
     1711    psMetadataAdd(arguments, PS_LIST_TAIL, "-string", PS_META_STR, "Test string", "SomeString");
     1712    psMetadataAdd(arguments, PS_LIST_TAIL, "-bool", PS_META_BOOL, "Test bool", false);
     1713    psMetadataAdd(arguments, PS_LIST_TAIL, "-int", PS_META_S32 | PS_META_DUPLICATE_OK, "Test integer 1", 1);
     1714    psMetadataAdd(arguments, PS_LIST_TAIL, "-int", PS_META_S32 | PS_META_DUPLICATE_OK, "Test integer 2", 2);
     1715    psMetadataAdd(arguments, PS_LIST_TAIL, "-int", PS_META_S32 | PS_META_DUPLICATE_OK, "Test integer 3", 3);
     1716    psMetadataAdd(arguments, PS_LIST_TAIL, "-float", PS_META_F32, "Test float", 1.234567);
     1717    if (! psArgumentParse(arguments, &argc, argv) || argc != 3) {
     1718        printf("\nName of the program here\n\n");
     1719        printf("Usage: %s INPUT OUTPUT\n\n", argv[0]);
     1720        psArgumentHelp(arguments);
     1721        psFree(arguments);
    17281722        exit(EXIT_FAILURE);
    17291723    }
     1724    const char *inputName = argv[1];    // Name of input
     1725    const char *outputName = argv[2];   // Name of output
     1726    printf("Success: %s %s\n", inputName, outputName);
     1727
     1728    psString string = psMetadataLookupString(NULL, arguments, "-string");
     1729    float floating = psMetadataLookupF32(NULL, arguments, "-float");
     1730    bool boolean = psMetadataLookupBool(NULL, arguments, "-truth");
     1731    printf("String: %s\n", string);
     1732    printf("Float: %f\n", floating);
     1733    printf("Boolean: %d\n", boolean);
     1734
     1735    psMetadataItem *intItem = psMetadataLookup(arguments, "-int");
     1736    if (intItem->type == PS_META_MULTI) {
     1737        psList *intMulti = intItem->data.V;
     1738        psListIterator *intIter = psListIteratorAlloc(intMulti, PS_LIST_HEAD, false);
     1739        psMetadataItem *intMultiItem = NULL;
     1740        while (intMultiItem = psListGetAndIncrement(intIter)) {
     1741            printf("Integer: %d\n", intMultiItem->data.S32);
     1742        }
     1743        psFree(intIter);
     1744    }
     1745}
     1746\end{verbatim}
     1747
     1748The above program should produce, on being given no arguments:
     1749\begin{verbatim}
     1750
     1751Name of the program here
     1752
     1753Usage: ./program INPUT OUTPUT
     1754
     1755Optional arguments, with default values:
     1756    -string    (SomeString)      Test string
     1757    -bool      (FALSE)           Test bool
     1758    -int       (1)               Test integer 1
     1759               (2)               Test integer 2
     1760               (3)               Test integer 3
     1761    -float     (1.234567e+00)    Test float
     1762\end{verbatim}
     1763
     1764and called with: \code{./program -string whatevers -float 9.876543
     1765-int 4 5 6 -bool input output}, then it should produce:
     1766
     1767\begin{verbatim}
     1768Success: input output
     1769String: SomeString
     1770Float: 1.234567
     1771Boolean: 0
     1772Integer: 1
     1773Integer: 2
     1774Integer: 3
    17301775\end{verbatim}
    17311776
Note: See TracChangeset for help on using the changeset viewer.