def _parse_params(paragrepper, argv): # Parse the command-line parameters prog = os.path.basename(argv[0]) USAGE = \ '\n' \ '%s [-aiov] [-p EOP_REGEXP] [-e REGEXP] ... [-f EXP_FILE] ... [file] ...\n' \ ' -OR-\n' \ '%s [-iv] [-p EOP_REGEXP] regexp [file] ...' % (prog, prog) parser = CommandLineParser(usage=USAGE, version=FULL_VERSION_STRING) parser.add_option('-a', '--and', action='store_true', dest='anding', help='Logically AND all regular expressions.') parser.add_option('-e', '--regexp', '--expr', action='append', dest='regexps', metavar='regexp', help='Specify a regular expression to find.' \ 'This option may be specified multiple times.') parser.add_option('-f', '--file', action='append', type='string', dest='exprFiles', metavar='exprfile', help='Specify a file full of regular expressions, ' \ 'one per line.') parser.add_option('-i', '--caseblind', action='store_true', dest='caseblind', help='Match without regard to case.') parser.add_option('-o', '--or', action='store_false', dest='anding', help='Logically OR all regular expressions.') parser.add_option('-p', '--eop', action='store', type='string', dest='eop_regexp', default=r'^\s*$', metavar='eop_regexp', help=r'Specify an alternate regular expression ' \ 'to match end-of-paragraph. Default: %default') parser.add_option('-P', '--print-eop', action='store_true', dest='print_eop', default=False, metavar='print_eop', help=r'Print the line that marks the end of each ' \ 'paragraph. Default: %default') parser.add_option('-v', '--negate', action='store_true', dest='negate', help='Negate the sense of the match.') (options, args) = parser.parse_args(argv) # Save the flag options paragrepper.anding = options.anding paragrepper.case_blind = options.caseblind paragrepper.negate = options.negate paragrepper.print_eop = options.print_eop # Figure out where to get the regular expressions to find. uncompiled_regexps = [] if options.regexps != None: uncompiled_regexps += options.regexps if options.exprFiles != None: try: uncompiled_regexps += _load_expr_files(options.exprFiles) except IOError, (errno, msg): parser.error(msg)
def _parse_params(paragrepper, argv): # Parse the command-line parameters prog = os.path.basename(argv[0]) USAGE = \ '\n' \ '%s [-aiov] [-p EOP_REGEXP] [-e REGEXP] ... [-f EXP_FILE] ... [file] ...\n' \ ' -OR-\n' \ '%s [-iv] [-p EOP_REGEXP] regexp [file] ...' % (prog, prog) parser = CommandLineParser(usage=USAGE, version=FULL_VERSION_STRING) parser.add_option('-a', '--and', action='store_true', dest='anding', help='Logically AND all regular expressions.') parser.add_option('-e', '--regexp', '--expr', action='append', dest='regexps', metavar='regexp', help='Specify a regular expression to find.' \ 'This option may be specified multiple times.') parser.add_option('-f', '--file', action='append', type='string', dest='exprFiles', metavar='exprfile', help='Specify a file full of regular expressions, ' \ 'one per line.') parser.add_option('-i', '--caseblind', action='store_true', dest='caseblind', help='Match without regard to case.') parser.add_option('-o', '--or', action='store_false', dest='anding', help='Logically OR all regular expressions.') parser.add_option('-p', '--eop', action='store', type='string', dest='eop_regexp', default=r'^\s*$', metavar='eop_regexp', help=r'Specify an alternate regular expression ' \ 'to match end-of-paragraph. Default: %default') parser.add_option('-P', '--print-eop', action='store_true', dest='print_eop', default=False, metavar='print_eop', help=r'Print the line that marks the end of each ' \ 'paragraph. Default: %default') parser.add_option('-v', '--negate', action='store_true', dest='negate', help='Negate the sense of the match.') (options, args) = parser.parse_args(argv) # Save the flag options paragrepper.anding = options.anding paragrepper.case_blind = options.caseblind paragrepper.negate = options.negate paragrepper.print_eop = options.print_eop # Figure out where to get the regular expressions to find. uncompiled_regexps = [] if options.regexps != None: uncompiled_regexps += options.regexps if options.exprFiles != None: try: uncompiled_regexps += _load_expr_files(options.exprFiles) except IOError as e: parser.error(e.message) # Try to compile the end-of-paragraph regular expression. try: paragrepper.eop_regexp = re.compile(options.eop_regexp) except Exception as e: parser.error('Bad regular expression "%s" to -p option' % \ options.eop_regexp) args = args[1:] if len(uncompiled_regexps) == 0: # No -e or -f seen. Use first non-option parameter. try: uncompiled_regexps += [args[0]] del args[0] except IndexError: parser.error('Not enough arguments.') # Compile the regular expressions and save the compiled results. flags = re.IGNORECASE if paragrepper.case_blind else None for expr in uncompiled_regexps: try: if flags: re_args = (expr, flags) else: re_args = (expr,) paragrepper.regexps += [re.compile(*re_args)] except Exception as e: parser.error('Bad regular expression: "%s"' % expr) # Are there any files, or are we searching standard input? if len(args) > 0: paragrepper.files = args