def parse_args():
    """
    Parse the command line arguments
    """
    parser = argparse.ArgumentParser(description = 'Decrypt AEADs',
                                     add_help = True,
                                     formatter_class = argparse.ArgumentDefaultsHelpFormatter,
                                     )
    parser.add_argument('-v', '--verbose',
                        dest='verbose',
                        action='store_true', default=False,
                        help='Enable verbose operation',
                        )
    parser.add_argument('--debug',
                        dest='debug',
                        action='store_true', default=False,
                        help='Enable debug operation',
                        )
    parser.add_argument('--format',
                        dest='format',
                        default='raw',
                        help='Select output format (aead, raw or yubikey-csv)',
                        )
    parser.add_argument('--output-dir',
                        dest='output_dir',
                        help='Output dir basename (for --format aead)',
                        metavar='DIR',
                        )
    parser.add_argument('--print-filename',
                        dest='print_filename',
                        action='store_true', default=False,
                        help='Prefix each row with the AEAD filename',
                        )
    parser.add_argument('--key-handle',
                        dest='key_handle',
                        help='Key handle used when generating the AEADs.',
                        metavar='HANDLE',
                        )
    parser.add_argument('--key-handle-out',
                        dest='key_handle_out',
                        help='Key handle used when generating *new* AEADs (with --format aead).',
                        metavar='HANDLE',
                        )
    parser.add_argument('--aes-key',
                        dest='aes_key',
                        required=True,
                        help='AES key used when generating the AEADs.',
                        metavar='HEXSTR',
                        )
    parser.add_argument('--aes-key-out',
                        dest='aes_key_out',
                        required=False,
                        help='AES key used when generating *new* AEADs (with --format aead).',
                        metavar='HEXSTR',
                        )
    parser.add_argument('--start-public-id',
                        dest='start_id',
                        required=False, default=None,
                        help='The first public id to decrypt',
                        metavar='INT-OR-MODHEX',
                        )
    parser.add_argument('--stop-public-id',
                        dest='stop_id',
                        required=False, default=None,
                        help='The last public id to decrypt',
                        metavar='INT-OR-MODHEX',
                        )
    parser.add_argument('--fail-fast',
                        dest='fail_fast',
                        action='store_true', default=False,
                        help='Terminate on the first AEAD failure, rather than keep going.',
                        )
    parser.add_argument('paths',
                        nargs='+',
                        help='Files and/or directories to process.',
                        metavar='FILE-OR-DIR'
                        )
    args = parser.parse_args()
    # argument fixups
    args.format = args.format.lower()
    args.aes_key = args.aes_key.decode('hex')
    if args.key_handle:
        args.key_handle = pyhsm.util.key_handle_to_int(args.key_handle)
    if args.start_id is not None:
        try:
            n = int(args.start_id)
        except ValueError:
            hexstr = pyhsm.yubikey.modhex_decode(args.start_id)
            n = int(hexstr, 16)
        args.start_id = n
    if args.stop_id is not None:
        try:
            n = int(args.stop_id)
        except ValueError:
            hexstr = pyhsm.yubikey.modhex_decode(args.stop_id)
            n = int(hexstr, 16)
        args.stop_id = n
    # some checks
    if args.format == 'aead':
        if not args.output_dir:
            sys.stderr.write("error: --output-dir is required when using --format aead.\n")
            return False
        if not os.path.isdir(args.output_dir):
            sys.stderr.write("error: Output directory '%s' not found\n" % (args.output_dir))
            return False
        if not args.aes_key_out:
            sys.stderr.write("error: --aes-key-out is required when using --format aead.\n")
            return False
        if not args.key_handle_out:
            sys.stderr.write("error: --key-handle-out is required when using --format aead.\n")
            return False
        # argument fixups
        args.aes_key_out = args.aes_key_out.decode('hex')
        args.key_handle_out_orig = args.key_handle_out # save to use in AEAD output paths
        args.key_handle_out = pyhsm.util.key_handle_to_int(args.key_handle_out)
    return args
Parse the command line arguments