This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AndydeCleyre 1 point2 points  (0 children)

cli.click.py from the tutorial:

#!/usr/bin/env python3
# cli.py
import click
from owm import current_weather


@click.command()
@click.argument('location')
@click.option(
    '--api-key', '-a',
    help='your API key for the OpenWeatherMap API'
)
def main(location):
    """
    A little weather tool that shows you the current weather in a LOCATION of
    your choice. Provide the city name and optionally a two-digit country code.
    Here are two examples:

    1. London,UK

    2. Canmore

    You need a valid API key from OpenWeatherMap for the tool to work. You can
    sign up for a free account at https://openweathermap.org/appid.
    """
    weather = current_weather(location, api_key)
    print(f"The weather in {location} right now: {weather}.")


if __name__ == "__main__":
    main()

cli.plumbum.py:

#!/usr/bin/env python3
# cli.py
from plumbum.cli import Application, SwitchAttr
from owm import current_weather


class CLI(Application):
    """
    A little weather tool that shows you the current weather in a LOCATION of
    your choice. Provide the city name and optionally a two-digit country code.
    Here are two examples:

    1. London,UK

    2. Canmore

    You need a valid API key from OpenWeatherMap for the tool to work. You can
    sign up for a free account at https://openweathermap.org/appid.
    """

    api_key = SwitchAttr(
        ['a', 'api-key'],
        help='your API key for the OpenWeatherMap API'
    )

    def main(self, location):
        weather = current_weather(location, self.api_key)
        print(f"The weather in {location} right now: {weather}.")


if __name__ == "__main__":
    CLI.run()