Next / Previous / Contents / Shipman's homepage

10. checkArgs(): Process the command line arguments

deduper
# - - -   c h e c k A r g s

def checkArgs():
    '''Check the command line.

      [ if the command line arguments are valid ->
          return an argparse.Namespace instance representing those
          arguments, with attributes .size for the size and
          .dir for the directory name
        else ->
          sys.stderr  +:=  error message
          stop execution ]      
    '''
    #-- 1
    # [ parser  :=  a new argparse.ArgumentParser instance ]
    parser = argparse.ArgumentParser(
        description="Reports large files with duplicate contents in a "
                    "directory structure")

For constants describing the option to specify a minimum size, see Section 8, “Manifest constants”. For the converter function that converts the specified value into an int, see Section 11, “sizeConverter(): Check the size option”.

deduper
    #-- 2
    # [ parser  +:=  a switch argument that stores its value in the
    #                SIZE_ATTR attribute ]
    parser.add_argument(SIZE_LONG_ARG, SIZE_SHORT_ARG,
        dest=SIZE_ATTR, default=SIZE_DEFAULT, type=sizeConverter,
        help="Lower size limit for files to be considered large, "
             "as a number optionally followed by 'k' for thousand, "
             "'m' for million, or 'g' for billions of bytes")

This script has one optional positional argument, the directory. See Section 8, “Manifest constants” for relevant definitions.

deduper
    #-- 3
    # [ parser  +:=  an optional positional argument with default
    #       value DIR_DEFAULT ]
    parser.add_argument(DIR_ATTR, nargs='?', default=DIR_DEFAULT,
        help="Path to the directory to be searched",
        metavar=DIR_METAVAR)

    #-- 4
    # [ if the command line arguments match (parser) ->
    #     args  :=  an argparse.Namespace instance representing those
    #               arguments
    #   else ->
    #     sys.stderr  +:=  error message
    #     stop execution ]
    args = parser.parse_args()

    #-- 5
    return args