Skip to content
Myr

arguments.rst

This file

Path

docs/arguments.rst

165 lines · 5.1 KiB

Last changed by

Added notes on missing "+" from argparseArmin Ronacherbefore adoption1ad7df7

History of this file

Take it

Initial commitArmin Ronacherbefore adoption4101de3

lines 1–6
1Arguments
2=========34.. currentmodule:: click56Arguments work similar to options but are positional. They also only

Merge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd

line 7why, one step back
7support a subset of the features of options due to their syntactical nature.

Initial commitArmin Ronacherbefore adoption4101de3

lines 8–13
8Click also will not attempt to document arguments for you and wants you to
9document them manually to avoid ugly looking help pages.1011Basic Arguments12---------------13

Merge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd

line 14why, one step back
14The most basic option is a simple string argument of one value. If no

Initial commitArmin Ronacherbefore adoption4101de3

lines 15–17
15type is provided the type of the default value is used. If no default
16value is provided the type is assumed to be :data:`STRING`.17

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 18why, one step back
18Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 19
19

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 20–21why, one step back
20.. click:example::
21

Initial commitArmin Ronacherbefore adoption4101de3

lines 22–24
22 @click.command()
23 @click.argument('filename')24 def touch(filename):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 25why, one step back
25 click.echo(filename)

Initial commitArmin Ronacherbefore adoption4101de3

line 26
26

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 27why, one step back
27And what it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 28
28

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 29why, one step back
29.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 30
30

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 31–32why, one step back
31 invoke(touch, args=['foo.txt'])
32

Initial commitArmin Ronacherbefore adoption4101de3

lines 33–40
33Variadic Arguments
34------------------3536The second most common version is variadic arguments where a specific (or37unlimited) number of arguments is accepted. This can be controlled with38the ``nargs`` parameter. If it's set to ``-1`` then an unlimited number39of arguments is accepted.40

Added support for nargs in other placesArmin Ronacherbefore adoption2e8354a

lines 41–42why, one step back
41The value is then passed as a tuple. Note that only one argument can be
42set to ``nargs=-1`` as this will eat up all arguments.

Initial commitArmin Ronacherbefore adoption4101de3

line 43
43

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 44why, one step back
44Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 45
45

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 46–47why, one step back
46.. click:example::
47

Initial commitArmin Ronacherbefore adoption4101de3

line 48
48 @click.command()

Added support for nargs in other placesArmin Ronacherbefore adoption2e8354a

lines 49–53why, one step back
49 @click.argument('src', nargs=-1)
50 @click.argument('dst', nargs=1)51 def copy(src, dst):52 for fn in src:53 click.echo('move %s to folder %s' % (fn, dst))

Initial commitArmin Ronacherbefore adoption4101de3

line 54
54

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 55why, one step back
55And what it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 56
56

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 57why, one step back
57.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 58
58

Fixed a broken example in the docsArmin Ronacherbefore adoption20e8ffa

line 59why, one step back
59 invoke(copy, args=['foo.txt', 'bar.txt', 'my_folder'])

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 60why, one step back
60

Added notes on missing "+" from argparseArmin Ronacherbefore adoption1ad7df7

lines 61–71why, one step back
61.. admonition:: Note on Non-Empty Variadic Arguments
6263 If you come from ``argparse`` you might be missing support for setting64 ``nargs`` to ``+`` to indicate that at least one argument is required.6566 Support for this is very intentionally not included as we believe67 scripts should gracefully degrate into becomming noops if a variadic68 argument is empty. The reason for this is that very often scripts are69 invoked with wildcard inputs from the command line and they should not70 error out if the wildcard is empty.71

Added choice supportArmin Ronacherbefore adoption9eacc90

lines 72–73why, one step back
72.. _file-args:
73

Initial commitArmin Ronacherbefore adoption4101de3

lines 74–78
74File Arguments
75--------------7677Since all the examples have already worked with file names it makes sense78to explain how to deal with files properly. Command line tools are more

Merge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd

line 79why, one step back
79fun if they work with files the unix way which is to accept ``-`` as a

Initial commitArmin Ronacherbefore adoption4101de3

lines 80–85
80special file that refers to stdin/stdout.
8182Click supports this through the :class:`click.File` type which83intelligently handles files for you. It also deals with unicode and bytes84correctly for all versions of Python so your script stays very portable.85

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 86why, one step back
86Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 87
87

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 88–89why, one step back
88.. click:example::
89

Initial commitArmin Ronacherbefore adoption4101de3

lines 90–99
90 @click.command()
91 @click.argument('input', type=click.File('rb'))92 @click.argument('output', type=click.File('wb'))93 def inout(input, output):94 while True:95 chunk = input.read(1024)96 if not chunk:97 break98 output.write(chunk)99

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 100why, one step back
100And what it does:

Initial commitArmin Ronacherbefore adoption4101de3

line 101
101

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 102why, one step back
102.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 103
103

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 104why, one step back
104 with isolated_filesystem():

Split up click into a packageArmin Ronacherbefore adoption0ed40c8

lines 105–106why, one step back
105 invoke(inout, args=['-', 'hello.txt'], input=['hello'],
106 terminate_input=True)

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 107–108why, one step back
107 invoke(inout, args=['hello.txt', '-'])
108

Added lazy file openingArmin Ronacherbefore adoptionf270dd2

lines 109–127why, one step back
109File Opening Safety
110-------------------111112The :class:`FileType` type has one problem it needs to deal with and that113is to decide when to open a file. The default behavior is to be114"intelligent" about it. What this means is that it will open stdin/stdout115and files opened for reading immediately. This will directly give the116user feedback when a file cannot be opened. But it will only open files117for writing the first time an IO operation is performed by wrapping the118file automatically in a special wrapper.119120This behavior can be forced by passing ``lazy=True`` or ``lazy=False`` to121the constructor. If the file is openened lazily it will fail on first IO122operation by raising an :exc:`FileError`.123124Since files opened for writing will typically immediatley empty the file,125the lazy mode should really only be disabled if the developer is 100% sure126that this is intended behavior.127

Documentation updates for file handlingArmin Ronacherbefore adoption72ae8a2

lines 128–135why, one step back
128Forcing on lazy mode is also very useful to avoid resource handling
129confusion. If a file is opened in lazy mode it will get a130``close_intelligently`` method that can help you figuring out if the file131needs closing or not. This is not needed for parameters, but it is132necessary for manually prompting with the :func:`prompt` function as you133do not know if a stream like stdout was openend (which was already open134before) or a real file that needs closing.135

Initial commitArmin Ronacherbefore adoption4101de3

lines 136–142
136Environment Variables
137---------------------138139Like options, arguments can also get values from an environment variable.140Unlike options however this is only supported for explicitly named141environment variables.142

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 143why, one step back
143Example usage:

Initial commitArmin Ronacherbefore adoption4101de3

line 144
144

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 145–146why, one step back
145.. click:example::
146

Initial commitArmin Ronacherbefore adoption4101de3

line 147
147 @click.command()

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 148why, one step back
148 @click.argument('src', envvar='SRC', type=click.File('r'))

Initial commitArmin Ronacherbefore adoption4101de3

line 149
149 def echo(src):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 150why, one step back
150 click.echo(src.read())

Initial commitArmin Ronacherbefore adoption4101de3

line 151
151

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 152why, one step back
152And from the command line:

Initial commitArmin Ronacherbefore adoption4101de3

line 153
153

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 154why, one step back
154.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 155
155

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 156–159why, one step back
156 with isolated_filesystem():
157 with open('hello.txt', 'w') as f:158 f.write('Hello World!')159 invoke(echo, env={'SRC': 'hello.txt'})

Initial commitArmin Ronacherbefore adoption4101de3

lines 160–165
160
161In that case it can also be a list of different environment variables162where the first one is picked.163164Generally this feature is not recommended because it can cause a lot of165confusion to the user.