Skip to content
Myr

arguments.rst

This file

Path

docs/arguments.rst

125 lines · 3.2 KiB

Last changed by

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

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–26
22 @click.command()
23 @click.argument('filename')24 def touch(filename):25 print(filename)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

Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70

lines 41–42why, one step back
41The value is then passed as a tuple. Note that only one argument really
42can be set to ``nargs=-1`` and right now only the last.

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

lines 48–53
48 @click.command()
49 @click.argument('files', nargs=-1)50 def touch(files):51 for file in files:52 print(file)53

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 55
55

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 57
57

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

lines 58–59why, one step back
58 invoke(touch, args=['foo.txt', 'bar.txt'])
59

Added choice supportArmin Ronacherbefore adoption9eacc90

lines 60–61why, one step back
60.. _file-args:
61

Initial commitArmin Ronacherbefore adoption4101de3

lines 62–66
62File Arguments
63--------------6465Since all the examples have already worked with file names it makes sense66to explain how to deal with files properly. Command line tools are more

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

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

Initial commitArmin Ronacherbefore adoption4101de3

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

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

line 74why, one step back
74Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 75
75

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

lines 76–77why, one step back
76.. click:example::
77

Initial commitArmin Ronacherbefore adoption4101de3

lines 78–87
78 @click.command()
79 @click.argument('input', type=click.File('rb'))80 @click.argument('output', type=click.File('wb'))81 def inout(input, output):82 while True:83 chunk = input.read(1024)84 if not chunk:85 break86 output.write(chunk)87

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 89
89

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 91
91

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

lines 92–95why, one step back
92 with isolated_filesystem():
93 invoke(inout, args=['-', 'hello.txt'], input=['hello'])94 invoke(inout, args=['hello.txt', '-'])95

Initial commitArmin Ronacherbefore adoption4101de3

lines 96–102
96Environment Variables
97---------------------9899Like options, arguments can also get values from an environment variable.100Unlike options however this is only supported for explicitly named101environment variables.102

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

line 103why, one step back
103Example usage:

Initial commitArmin Ronacherbefore adoption4101de3

line 104
104

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

lines 105–106why, one step back
105.. click:example::
106

Initial commitArmin Ronacherbefore adoption4101de3

line 107
107 @click.command()

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 109–111
109 def echo(src):
110 print(src.read())111

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 113
113

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 115
115

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

lines 116–119why, one step back
116 with isolated_filesystem():
117 with open('hello.txt', 'w') as f:118 f.write('Hello World!')119 invoke(echo, env={'SRC': 'hello.txt'})

Initial commitArmin Ronacherbefore adoption4101de3

lines 120–125
120
121In that case it can also be a list of different environment variables122where the first one is picked.123124Generally this feature is not recommended because it can cause a lot of125confusion to the user.