Python click tips

Tips for using Python click

Click is better than using argparse It's very convenient when creating subcommands or applying callback validation. Record it because there is a subtle addiction in the callback

Precautions when using callback

To use callback with click here The article is very well organized, so please refer to here for how to use it. ..

By the way, the official document is here

callback validation usage example

import click
import ipaddress

def validate_ipaddress(ctx, param, value):
    try:
        addr = ipaddress.ip_address(value)
    except ValueError:
        msg = f"addr must be ip address format as 'x.x.x.x'"
        raise click.BadParameter(msg)
    return str(addr)

@click.command()
@click.option('--addr', required=False,
              type=str, callback=validate_ipaddress,
              help='address')
def cmd(addr):
    set_loglevel(debug)

For example, write this code and check if the addr option is in ipaddress notation. If you write code that returns a value, you have to return it properly with return to def cmd Note that the addr argument does not come.

A magic that will allow you to complete bash

Basically here However, if it is .py as it is, it can not be executed, so whether to binary it using pyinstaller To "install" by writing setuptool.

Recommended Posts

Python click tips
python tips
python tips
Python Tips
Python tips
Python debugging tips
Unexpectedly (?) Python tips
Python and numpy tips
Python Tips (my memo)
Python PyTorch install tips
[Blender x Python] Blender Python tips (11/100)
Python
Receiving standard input tips @ python
[Tips] Handle Athena with Python
[Python + Selenium] Tips for scraping
Google Drive Api Tips (Python)
~ Tips for beginners to Python ③ ~
Tips on Python file input / output
LaTeX, Python tips in master's thesis
[TouchDesigner] Tips for for statements using python
Tips for calling Python from C
Write python list fast vim tips
[VS Code] ~ Tips when using python ~
kafka python
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python FlowFishMaster
Python service
Tips to make Python here-documents easier to read
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
Jupyter Tips 4
install python
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
numpy tips
Tips for dealing with binaries in Python
Installing Python 3.4.3.
Python memo
Python iterative
Jupyter Tips 5
Python algorithm
Python2 + word2vec
Scapy Tips
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial