Next / Previous / Contents / Shipman's homepage

14.1. Args.__init__(): Constructor

nomcompile3
# - - -   A r g s . _ _ i n i t _ _

    def __init__ ( self ):
        '''Constructor
        '''

If the class variable everCalled is True, the constructor has already validated and processed the arguments, so its work is done.

nomcompile3
        #-- 1 --
        if Args.everCalled:
            return
        else:
            Args.everCalled = True

Rather than write specific code for each possible argument, we'll use the standard Python optparse module to check all the command line arguments.

nomcompile3
        #-- 2 --
        # [ parser  :=  an optparse.OptionParser instance that
        #       processes this program's command line arguments ]
        parser = self.__buildParser()

        #-- 3 --
        # [ if the command line arguments are valid ->
        #     options  :=  an optparse.Values instance containing
        #                  options
        #     argList  :=  positional arguments from the command
        #                  line as a sequence of strings
        #   else ->
        #       sys.stderr  +:=  error message(s)
        #       stop execution ]
        (options, argList) = parser.parse_args()

At this point, options has attributes for each command line option. So far we have only one, the -r or --ranks option.

nomcompile3
        #-- 4 --
        # [ if options.ranksFileName is None ->
        #     self.ranksFileName  :=  DEFAULT_RANKS_FILE
        #   else ->
        #     self.ranksFileName  :=  options.ranksFileName ]
        self.ranksFileName = options.ranksFileName or DEFAULT_RANKS_FILE

There should be exactly one positional argument, the base file name.

nomcompile3
        #-- 5 --
        # [ if argList has one element ->
        #       self.basename  :=  that element
        #   else ->
        #       sys.stderr  +:=  error message(s)
        #       stop execution ]
        if len(argList) == 1:
            self.basename = argList[0]
        else:
            parser.error ( "One positional argument required: "
                "the base file name." )

Finally, form the various input and output file names.

nomcompile3
        #-- 6 --
        self.stdFileName = self.basename + STD_EXTENSION
        self.altFileName = self.basename + ALT_EXTENSION
        self.treeFileName = self.basename + TREE_EXTENSION
        self.abbrFileName = self.basename + ABBR_EXTENSION
        self.collFileName = self.basename + COLL_EXTENSION
        self.xmlFileName = self.basename + XML_EXTENSION