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
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
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.
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