summaryrefslogtreecommitdiff
path: root/docs/advanced.rst
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2014-08-12 14:28:23 +0200
committerArmin Ronacher <armin.ronacher@active-4.com>2014-08-12 14:28:23 +0200
commita08cc28c59b6562b91c70f543572f304861e57d2 (patch)
treea2cd5a3aeb30035affd9d3a7cebc8658f3086388 /docs/advanced.rst
parentc0b076e7c0284ee4912285bd010cc7c3c1f6f033 (diff)
downloadclick-a08cc28c59b6562b91c70f543572f304861e57d2.tar.gz
Added a chapter for parameter modifications
Diffstat (limited to 'docs/advanced.rst')
-rw-r--r--docs/advanced.rst55
1 files changed, 55 insertions, 0 deletions
diff --git a/docs/advanced.rst b/docs/advanced.rst
index a087c1a..77174e4 100644
--- a/docs/advanced.rst
+++ b/docs/advanced.rst
@@ -62,6 +62,61 @@ And it can then be used like this:
def pop():
pass
+Parameter Modifications
+-----------------------
+
+Parameters (options and arguments) are forwarded to the command callbacks
+as you have seen. One common way to prevent a parameter from being passed
+to the callback is the `expose_value` argument to a parameter which hides
+the parameter entirely. The way this works is that the :class:`Context`
+object has a :attr:`~Context.params` attribute which is a dictionary of
+all parameters. Whatever is in that dictionary is being passed to the
+callbacks.
+
+This can be used to make up addition parameters. Generally this pattern
+is not recommended but in some cases it can be useful. At the very least
+it's good to know that the system works this way.
+
+.. click:example::
+
+ import urllib
+
+ def open_url(ctx, param, value):
+ if value is not None:
+ ctx.params['fp'] = urllib.urlopen(value)
+ return value
+
+ @click.command()
+ @click.option('--url', callback=open_url)
+ def cli(url, fp=None):
+ if fp is not None:
+ click.echo('%s: %s' % (url, fp.code))
+
+In this case the callback returns the URL unchanged but also passes a
+second ``fp`` value to the callback. What's more recommended is to pass
+the information in a wrapper however:
+
+.. click:example::
+
+ import urllib
+
+ class URL(object):
+
+ def __init__(self, url, fp):
+ self.url = url
+ self.fp = fp
+
+ def open_url(ctx, param, value):
+ if value is not None:
+ return URL(value, urllib.urlopen(value))
+
+ @click.command()
+ @click.option('--url', callback=open_url)
+ def cli(url):
+ if url is not None:
+ click.echo('%s: %s' % (url.url, url.fp.code))
+
+
Token Normalization
-------------------