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 fileTake it
1Arguments2=========34.. currentmodule:: click56Arguments work similar to options but are positional. They also onlyMerge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd
line 7why, one step back7support a subset of the features of options due to their syntactical nature.Initial commitArmin Ronacherbefore adoption4101de3
lines 8–138Click also will not attempt to document arguments for you and wants you to9document them manually to avoid ugly looking help pages.1011Basic Arguments12---------------13Merge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd
line 14why, one step back14The most basic option is a simple string argument of one value. If noInitial commitArmin Ronacherbefore adoption4101de3
lines 15–1715type is provided the type of the default value is used. If no default16value is provided the type is assumed to be :data:`STRING`.17Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 18why, one step back18Example:19Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 20–21why, one step back20.. click:example::21Initial commitArmin Ronacherbefore adoption4101de3
lines 22–2622 @click.command()23 @click.argument('filename')24 def touch(filename):25 print(filename)26Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 27why, one step back27And what it looks like:28Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 29why, one step back29.. click:run::30Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 31–32why, one step back31 invoke(touch, args=['foo.txt'])32Initial commitArmin Ronacherbefore adoption4101de3
lines 33–4033Variadic Arguments34------------------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.40Merge pull request #21 from julen/doc-fixesArmin Ronacherbefore adoptiona4b7d70
lines 41–42why, one step back41The value is then passed as a tuple. Note that only one argument really42can be set to ``nargs=-1`` and right now only the last.43Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 44why, one step back44Example:45Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 46–47why, one step back46.. click:example::47Initial commitArmin Ronacherbefore adoption4101de3
lines 48–5348 @click.command()49 @click.argument('files', nargs=-1)50 def touch(files):51 for file in files:52 print(file)53Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 54why, one step back54And what it looks like:55Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 56why, one step back56.. click:run::57Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 58–59why, one step back58 invoke(touch, args=['foo.txt', 'bar.txt'])59Added choice supportArmin Ronacherbefore adoption9eacc90
lines 60–61why, one step back60.. _file-args:61Initial commitArmin Ronacherbefore adoption4101de3
lines 62–6662File Arguments63--------------6465Since all the examples have already worked with file names it makes sense66to explain how to deal with files properly. Command line tools are moreMerge pull request #27 from epequeno/docs-editsArmin Ronacherbefore adoptionb2475cd
line 67why, one step back67fun if they work with files the unix way which is to accept ``-`` as aInitial commitArmin Ronacherbefore adoption4101de3
lines 68–7368special 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.73Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 74why, one step back74Example:75Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 76–77why, one step back76.. click:example::77Initial commitArmin Ronacherbefore adoption4101de3
lines 78–8778 @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)87Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 88why, one step back88And what it does:89Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 90why, one step back90.. click:run::91Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 92–95why, one step back92 with isolated_filesystem():93 invoke(inout, args=['-', 'hello.txt'], input=['hello'])94 invoke(inout, args=['hello.txt', '-'])95Initial commitArmin Ronacherbefore adoption4101de3
lines 96–10296Environment Variables97---------------------9899Like options, arguments can also get values from an environment variable.100Unlike options however this is only supported for explicitly named101environment variables.102Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 103why, one step back103Example usage:104Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 105–106why, one step back105.. click:example::106107 @click.command()Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 108why, one step back108 @click.argument('src', envvar='SRC', type=click.File('r'))Initial commitArmin Ronacherbefore adoption4101de3
lines 109–111109 def echo(src):110 print(src.read())111Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 112why, one step back112And from the command line:113Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 114why, one step back114.. click:run::115Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 116–119why, one step back116 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–125120121In 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.