Use current `urllib` API, idiomatic Python and fix option name (#3503)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 9830b60, af54674 · 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
Provenance
- Commit
1050d95- Tree
7bef2ad- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
9830b60- Parent 2
af54674
1 file changed+11 added−15 removed
modified:docs/advanced.md+11 added-15 removed
128 unmodified lines129 130 ```{eval-rst}131 .. click:example::132 133- import urllib133+ import urllib.request134 135 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 value139 140 @click.command()141 @click.option('--url', callback=open_url)16 unmodified lines158 What's more recommended is to pass the information in a wrapper, however:159 160 .. click:example::161 162- import urllib162+ import urllib.request163 164- class URL(object):165-164+ class URL:165 def __init__(self, url, fp):166 self.url = url167 self.fp = fp168 169 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))172 173 @click.command()174 @click.option('--url', callback=open_url)175 def cli(url):18 unmodified lines194 195 ```{eval-rst}196 .. click:example::197 199- CONTEXT_SETTINGS = dict(token_normalize_func=lambda x: x.lower())198+ CONTEXT_SETTINGS = {'token_normalize_func': lambda x: x.lower()}199 200 @click.command(context_settings=CONTEXT_SETTINGS)201 @click.option('--name', default='Pete')202 def cli(name):86 unmodified lines289 290 ```{eval-rst}291 .. click:example::292 294- import sys293 from subprocess import call294 297- @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 lines326 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 arguments69 unmodified lines404 ```python405 @click.group()406 @click.option("--name", default="repo.db")407 @click.pass_context412-def cli(ctx, repo_home):413- ctx.obj = db = open_db(repo_home)408+def cli(ctx, name):409+ ctx.obj = db = open_db(name)410 411 @ctx.call_on_close412 def close_db():413 db.record_use()8 unmodified lines