Next / Previous / Contents / TCC Help System / NM Tech homepage

2.4. argCheck(): Check command line arguments

This function does all checking of command line arguments, and boils them down to a datetime.datetime instance.

jd
# - - -   a r g C h e c k

def  argCheck():
    """Check and convert the command line argument(s).
    """

The command line arguments in sys.argv[1:] can take either of two forms:

In the first case, we use Section 9.6, “parseDatetime(): Convert an external date-time string” to parse the single argument; in the second, we use Section 9.7, “parseDate(): Convert an external date string” and Section 9.8, “parseTime(): Convert an external time string” and then combine them into a datetime.datetime instance.

jd
    #-- 1 --
    # [ argList  :=  the command line arguments ]
    argList  =  sys.argv[1:]

    #-- 2 --
    # [ if (len(argList)==1) and argList[0] is a valid
    #   date-time string ->
    #     dt  :=  that date-time as a datetime.datetime instance
    #   else if (len(argList)==2) and (argList[0] is a valid
    #   date) and (argList[1] is a valid time) ->
    #     dt  :=  a datetime.datetime representing that date
    #             and time
    #   else ->
    #     sys.stderr  +:=  error message
    #     stop execution ]
    if  len(argList) == 1:
        try:
            dt  =  sidereal.parseDatetime ( argList[0] )
        except SyntaxError, detail:
            usage ( "Invalid date-time: %s" % detail )
    elif  len(argList) == 2:
        try:
            date  =  sidereal.parseDate ( argList[0] )
        except SyntaxError, detail:
            usage ( "Invalid date: %s" % detail )
        try:
            time  =  sidereal.parseTime ( argList[1] )
        except SyntaxError, detail:
            usage ( "Invalid time: %s" % detail )
        dt  =  date.combine ( date, time )
    else:
        usage ( "Incorrect number of arguments." )

    #-- 3 --
    return dt