def main(): """ Main program. """ usage = 'Usage: %s [OPTIONS] fortune_file' % os.path.basename(sys.argv[0]) arg_parser = CommandLineParser(usage=usage) arg_parser.add_option('-q', '--quiet', action='store_true', dest='quiet', help="When updating the index file, don't emit " \ "messages.") arg_parser.add_option('-u', '--update', action='store_true', dest='update', help='Update the index file, instead of printing a ' 'fortune.') arg_parser.add_option('-V', '--version', action='store_true', dest='show_version', help='Show version and exit.') arg_parser.epilogue = 'If <fortune_file> is omitted, fortune looks at the ' \ 'FORTUNE_FILE environment variable for the path.' options, args = arg_parser.parse_args(sys.argv) if len(args) == 2: fortune_file = args[1] else: try: fortune_file = os.environ['FORTUNE_FILE'] except KeyError: arg_parser.show_usage('Missing fortune file.') try: if options.show_version: print 'fortune, version %s' % __version__ elif options.update: make_fortune_data_file(fortune_file) else: sys.stdout.write(get_random_fortune(fortune_file)) except ValueError, msg: print >> sys.stderr, msg sys.exit(1)
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