Exemple #1
0
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)