Create a Python console application easily with Click

If you want to create a Python program that can be executed from the command line, I think it is common to use ʻargparse or ʻoptparse to process the command line arguments.

Using the Click library is convenient because you can easily create a console application simply by wrapping an arbitrary function with a decorator.

Let's try a simple implementation to get the URL.

http_fetcher.py


import click
import requests

@click.command()
@click.argument('url')
def fetch(url):
    click.echo(requests.get(url).text.encode('utf-8'))

if __name__ == '__main__':
    fetch()
% python http_fetcher.py http://www.qiita.com/
<!DOCTYPE html>...

If you specify an argument with * -o *, it will be written to the file.

http_fetcher2.py


import click
import requests
import sys

@click.command()
@click.argument('url')
@click.option('-o', '--out-file', type=click.File('w'))
def fetch(url, out_file):
    if not out_file:
        out_file = sys.stdout
    click.echo(requests.get(url).text.encode('utf-8'), file=out_file)

if __name__ == '__main__':
    fetch()
% python http_fetcher2.py -o index.html http://qiita.com
% cat index.html
<!DOCTYPE html>...

The file is also convenient because Click automatically opens it and returns it as a File Object.

There are many other nice features such as command grouping and progress bar display. The documentation is here.

Recommended Posts

Create a Python console application easily with Click
You can easily create a GUI with Python
Create a directory with python
Why not create a stylish table easily with Python?
Easily implement subcommands with python click
Create a virtual environment with Python!
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
[Python] A quick web application with Bottle!
Create a word frequency counter with Python 3.4
Run a Python web application with Docker
Create a frame with transparent background with tkinter [Python]
I made a GUI application with Python + PyQt5
Create a Python module
Create a virtual environment with conda in Python
[Note] Create a one-line timezone class with python
Create a python3 build environment with Sublime Text3
Create a color bar with Python + Qt (PySide)
Steps to create a Twitter bot with python
Easily beep with python
Create a decision tree from 0 with Python (1. Overview)
Create a color-specified widget with Python + Qt (PySide)
Create a one-file hello world application with django
Create a Photoshop format file (.psd) with python
[Python] Create a ValueObject with a complete constructor using dataclasses
Create a python development environment with vagrant + ansible + fabric
Launch a Python web application with Nginx + Gunicorn with Docker
Build a machine learning application development environment with Python
Create a Layer for AWS Lambda Python with Docker
[python] Create a date array with arbitrary increments with np.arange
A memo about building a Django (Python) application with Docker
[Python] How to create a 2D histogram with Matplotlib
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a fake Minecraft server in Python with Quarry
Create a company name extractor with python using JCLdic
Create a 2d CAD file ".dxf" with python [ezdxf]
Create a Wox plugin (Python)
Easily serverless with Python with chalice
Create a function in Python
Create a dictionary in Python
Create a homepage with django
Create a python numpy array
Make a fortune with Python
Create a heatmap with pyqtgraph
Web application with Python + Flask ② ③
Easily cProfile with a decorator
Web application with Python + Flask ④
Create a web application that recognizes numbers with a neural network
[Python] Create a file & folder path specification screen with tkinter
Create a list in Python with all followers on twitter
How to convert an array to a dictionary with Python [Application]
Create a Mastodon bot with a function to automatically reply with Python
Create a child account for connect with Stripe in Python
Probably the easiest way to create a pdf with Python3
Let's create a PRML diagram with Python, Numpy and matplotlib.
I made a simple book application with python + Flask ~ Introduction ~
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a simple video analysis tool with python wxpython + openCV
Creating a GUI as easily as possible with python [tkinter edition]