Next / Previous / Contents / Shipman's homepage

14. class Args: Process command line arguments

When this class is first instantiated, it checks all the command line arguments. If they are valid, it returns an instance containing the digested command line arguments. If there are any problems, it prints a usage message and terminates execution.

This class implements the Singleton pattern as described in Design Patterns by Gamma et al. In practice, this means that when the class is first instantiated, all the command line arguments will be checked and stored away in the instance (assuming they are valid). Any subsequent instantiations of Args will retrieve that same instance.

To implement this behavior, this class inherits from the Singleton base class from the author's logscan module; see Section 5, “Imported modules” for a documentation link.

nomcompile3
# - - - - -   c l a s s   A r g s

class Args(Singleton):
    '''Check and digest the command line arguments.

      Exports:
        Args():
          [ if the command line arguments are valid for this
            program ->
              return a singleton Args instance that represents
              those arguments
            else ->
              sys.stderr  +:=  (usage message) + (error message)
              stop execution ]
        .ranksFileName:
          [ if a ranks file was specified ->
              that file name
            else -> DEFAULT_RANKS_FILE ]
        .basename:
          [ the base file name argument as a string ]
        .stdFileName:    [ self.basename + STD_EXTENSION ]
        .altFileName:    [ self.basename + ALT_EXTENSION ]
        .treeFileName:   [ self.basename + TREE_EXTENSION ]
        .abbrFileName:   [ self.basename + ABBR_EXTENSION ]
        .collFileName:   [ self.basename + COLL_EXTENSION ]
        .xmlFileName:    [ self.basename + XML_EXTENSION ]
    '''

Because the constructor may be called more than once, we must use a class variable, everCalled, to insure that the actual argument processing is done only on the first call. This variable is set initially to False; the constructor sets it to True on the first instantiation.

nomcompile3
    everCalled = False