def parse_arguments(arguments):
    """Parse command line arguments.  Returns the tuple (options, race_id)."""
    option_parser = OptionParser()
    option_parser.set_usage("%prog [options] RACE_ID")
    option_parser.add_option("-y", "--year", default=date.today().year,
                             help="Year in which the race occurred.",
                             type="int")
    options, arguments = option_parser.parse_args(arguments[1:])
    try:
        connect(options.server)
        race = Races.get(int(arguments[0]))
    except ConnectionError, error:
        option_parser.error(error)
def parse_arguments(arguments):
    """Parse command line arguments.  Returns the tuple (options, race_id)."""
    option_parser = OptionParser()
    option_parser.set_usage("%prog [options] RACE_ID")
    option_parser.add_option("-y", "--year", default=date.today().year,
                             help="Year in which the race occurred.",
                             type="int")
    options, arguments = option_parser.parse_args(arguments[1:])
    try:
        connect(options.server)
        race = Races.get(int(arguments[0]))
    except ConnectionError, error:
        option_parser.error(error)
    except ValueError:
        race = Races.get(arguments[0], options.year)
    except IndexError:
        option_parser.error("Required positional argument missing.")
    return options, race

@main_function(parse_arguments)
def main(options, race):
    """Read YAML race results from stdin and save them to the specified
    race."""
    try:
        results = load(stdin.read())
    except KeyboardInterrupt:
        return 1
    except ScannerError, error:
        print >> stderr, error
        return 1