CARVIEW |
Select Language
HTTP/2 200
date: Mon, 13 Oct 2025 22:36:09 GMT
server: Fly/5d9a8537e (2025-10-13)
content-type: text/html; charset=utf-8
content-encoding: gzip
via: 2 fly.io, 2 fly.io
fly-request-id: 01K7FVF1DRK7JZ35NSVBG179F5-bom
Understanding option names in Click | Simon Willison’s TILs
Understanding option names in Click
I hit a bug today where I had defined a Click option called open
but in doing so I replaced the Python bulit-in open()
function:
@click.command()
# ...
@click.option("-o", "--open", is_flag=True, help="Open Datasette in your web browser")
def my_command(open):
# Now open() is no longer available
This inspired me to finally figure out how Click function argument names work. It's documented here: https://click.palletsprojects.com/en/7.x/options/#name-your-options
Short version: you can do this:
@click.command()
# ...
@click.option("-o", "--open", "open_browser", is_flag=True, help="Open Datasette in your web browser")
def my_command(open_browser):
# Now open() can be used safely
Click will use the positional argument without any hyphen prefixes as the name of the argument. If none is provided it will use the first --
one. If that's not defined it will use the -o
one - all with the hypens stripped.
Related
- gpt3 Using ChatGPT Browse to name a Python package - 2023-06-18
- python How to call pip programatically from Python - 2020-08-11
- pytest Using pytest and Playwright to test a JavaScript web application - 2022-07-24
- python Debugging a Click application using pdb - 2020-09-03
- python Introspecting Python function parameters - 2020-05-27
- datasette Writing a Datasette CLI plugin that mostly duplicates an existing command - 2022-10-22
- python Explicit file encodings using click.File - 2020-10-16
- pytest Writing pytest tests against tools written with argparse - 2022-01-08
- awslambda Deploying Python web apps as AWS Lambda functions - 2022-09-18
- python CLI tools hidden in the Python standard library - 2023-06-28
Created 2020-09-22T08:49:08-07:00, updated 2020-09-22T08:49:36-07:00 · History · Edit