Next / Previous / Contents / Shipman's homepage

4.11. class Args: Process the command line

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

class Args(object):
    '''Represents the command line.

      Exports:
        Args():
          [ if sys.argv[1:] is a valid command line ->
              return a new Args instance representing sys.argv[1:]
            else ->
              sys.stderr  +:=  error message
              stop execution ]
        .isVerbose:
          [ True iff sys.argv[1:] contains -v or --verbose ]
        .inFileList:
          [ list of INFILE arguments from sys.argv[1:] ]
    '''
    def __init__(self):
        #-- 1
        # [ parser  :=  a new argparse.ArgumentParser instance ]
        parser = argparse.ArgumentParser(
            description="Extract literate source files from "
                        "DocBook-XML 5.0 documents")

        #-- 2
        # [ parser  :=  a new switch [-v|--verbose] storing in .isVerbose ]
        parser.add_argument('-v', '--verbose', action='store_true',
            dest='isVerbose', help='Report on files generated')

        #-- 3
        # [ parser  :=  a new list of one or more positional
        #       arguments storing in .inFileList ]
        parser.add_argument('inFileList', nargs='+', metavar='INFILE',
            help="List of DocBook-XML 5.0 input files")

        #-- 4
        # [ if parser validates sys.argv[1:] ->
        #     self  :=  self as invariant
        #   else ->
        #     sys.stderr  +:=  error message
        #     stop execution ]
        parser.parse_args(namespace=self)