Skip to content
Myr

Use current `urllib` API, idiomatic Python and fix option name

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #3489

authored by Kevin Deldycke · committed by Kevin Deldycke · parent a42300a · Browse files · 1 file+11 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+11 added−15 removed

  • other:+11 added−15 removed

1 file+11 added−15 removed

modified:docs/advanced.md+11 added-15 removed
128 unmodified lines
129130```{eval-rst}131.. click:example::132133 import urllib133 import urllib.request134135 def open_url(ctx, param, value):136 if value is not None:137 ctx.params['fp'] = urllib.urlopen(value)137 ctx.params['fp'] = urllib.request.urlopen(value)138 return value139140 @click.command()141 @click.option('--url', callback=open_url)
16 unmodified lines
158What's more recommended is to pass the information in a wrapper, however:159160.. click:example::161162 import urllib162 import urllib.request163164 class URL(object):165164 class URL:165 def __init__(self, url, fp):166 self.url = url167 self.fp = fp168169 def open_url(ctx, param, value):170 if value is not None:172 return URL(value, urllib.urlopen(value))171 return URL(value, urllib.request.urlopen(value))172173 @click.command()174 @click.option('--url', callback=open_url)175 def cli(url):
18 unmodified lines
194195```{eval-rst}196.. click:example::197199 CONTEXT_SETTINGS = dict(token_normalize_func=lambda x: x.lower())198 CONTEXT_SETTINGS = {'token_normalize_func': lambda x: x.lower()}199200 @click.command(context_settings=CONTEXT_SETTINGS)201 @click.option('--name', default='Pete')202 def cli(name):
86 unmodified lines
289290```{eval-rst}291.. click:example::292294 import sys293 from subprocess import call294297 @click.command(context_settings=dict(298 ignore_unknown_options=True,299 ))295 @click.command(context_settings={'ignore_unknown_options': True})296 @click.option('-v', '--verbose', is_flag=True, help='Enables verbose mode')297 @click.argument('timeit_args', nargs=-1, type=click.UNPROCESSED)298 def cli(verbose, timeit_args):299 """A fake wrapper around Python's timeit."""304 cmdline = ['echo', 'python', '-mtimeit'] + list(timeit_args)300 cmdline = ['echo', 'python', '-mtimeit', *timeit_args]301 if verbose:302 click.echo(f"Invoking: {' '.join(cmdline)}")303 call(cmdline)304
21 unmodified lines
326 handled as an argument.327- Unknown short options might be partially handled and reassembled if328 necessary. For instance in the above example there is an option329 called `-v` which enables verbose mode. If the command would be334 ignored with `-va` then the `-v` part would be handled by Click330 invoked with `-va` then the `-v` part would be handled by Click331 (as it is known) and `-a` would end up in the leftover parameters332 for further processing.333- Depending on what you plan on doing you might have some success by334 disabling interspersed arguments
69 unmodified lines
404```python405@click.group()406@click.option("--name", default="repo.db")407@click.pass_context412def cli(ctx, repo_home):413 ctx.obj = db = open_db(repo_home)408def cli(ctx, name):409 ctx.obj = db = open_db(name)410411 @ctx.call_on_close412 def close_db():413 db.record_use()
8 unmodified lines