Next / Previous / Contents / Shipman's homepage

18.2. Args.__parse(): Option syntax checking

listext2
# - - -   A r g s . _ _ b u i l d P a r s e r

    def _buildParser(self):
        '''Set up the argument parser.
        '''
        #-- 1
        # [ self._argParser  :=  an argparse.ArgumentParser instance ]
        self._argParser = argparse.ArgumentParser(add_help=True,
            description="A text file listing utility")

For the correspondence between arguments and attributes of self, see Section 18, “class Args: Command line arguments”.

listext2
        #-- 2
        # [ self._argParser  :=  self._argParser with all command line
        #       arguments routed to the corresponding attribute of self,
        #       except that --lead is routed to self.rawLeading ]
        self._argParser.add_argument("-b", "--break", dest="breakString",
            default=self.DEFAULT_BREAK_STRING,
            help="Keep blocks together if they are surrounded "
                 "by lines starting with this string")

See Section 18.3, “Args._checkPositive(): Check for a positive integer” for the type checker that insures that the --cols value is a positive integer.

listext2
        self._argParser.add_argument("-c", "--nCols",
            default=None, type=self._checkPositive,
            help="Number of columns side by side")

        self._argParser.add_argument("-l", "--leading",
            default=None, dest="rawLeading", metavar="LEADING",
            help="Interline spacing in points")

        self._argParser.add_argument("-o", "--out",
            dest="outFileName",
            default=self.DEFAULT_OUT_FILE,
            help="Output XSL-FO file name")

For the validator method for --points, see Section 18.4, “Args._checkPoints(): Check the font size option”.

listext2
        self._argParser.add_argument("-p", "--points",
            dest="pointSize", type=self._checkPoints,
            default=self.DEFAULT_POINT_SIZE,
            help="Output font size in whole points")

        self._argParser.add_argument("-t", "--tabs",
            dest="tabSize", type=self._checkPositive,
            default=self.DEFAULT_TAB_SIZE,
            help="Number of columns per tab stop")

        orientGroup = self._argParser.add_mutually_exclusive_group()
        orientGroup.add_argument("-L", "--landscape",
            dest="isLandscape", action="store_true",
            help="Landscape orientation")
        orientGroup.add_argument("-P", "--portrait",
            dest="isLandscape", action="store_false",
            help="Portrait orientation")
        self._argParser.set_defaults(
            isLandscape=self.DEFAULT_IS_LANDSCAPE)

        plexGroup = self._argParser.add_mutually_exclusive_group()
        plexGroup.add_argument("-1", "--simplex",
            dest="duplex", action="store_false",
            help="Double-sided printing")
        plexGroup.add_argument("-2", "--duplex",
            dest="duplex", action="store_true",
            help="Single-sided printing")
        self._argParser.set_defaults(
            duplex=self.DEFAULT_DUPLEX)

        self._argParser.add_argument("inFileName",
            metavar="INFILE",
            help="Input file")