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 only7support a subset of the features of options due to their syntactical nature.8Click also will not attempt to document arguments for you and wants you to9document them manually to avoid ugly looking help pages.1011Basic Arguments12---------------1314The most basic option is a simple string argument of one value. If no15type is provided the type of the default value is used. If no default16value is provided the type is assumed to be :data:`STRING`.1718Example:1920.. click:example::2122 @click.command()23 @click.argument('filename')24 def touch(filename):25 print(filename)2627And what it looks like:2829.. click:run::3031 invoke(touch, args=['foo.txt'])3233Variadic 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.4041The value is then passed as a tuple. Note that only one argument really42can be set to ``nargs=-1`` and right now only the last.4344Example:4546.. click:example::4748 @click.command()49 @click.argument('files', nargs=-1)50 def touch(files):51 for file in files:52 print(file)5354And what it looks like:5556.. click:run::5758 invoke(touch, args=['foo.txt', 'bar.txt'])5960.. _file-args:6162File 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 more67fun if they work with files the unix way which is to accept ``-`` as a68special 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.7374Example:7576.. click:example::7778 @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)8788And what it does:8990.. click:run::9192 with isolated_filesystem():93 invoke(inout, args=['-', 'hello.txt'], input=['hello'])94 invoke(inout, args=['hello.txt', '-'])9596Environment Variables97---------------------9899Like options, arguments can also get values from an environment variable.100Unlike options however this is only supported for explicitly named101environment variables.102103Example usage:104105.. click:example::106107 @click.command()108 @click.argument('src', envvar='SRC', type=click.File('r'))109 def echo(src):110 print(src.read())111112And from the command line:113114.. click:run::115116 with isolated_filesystem():117 with open('hello.txt', 'w') as f:118 f.write('Hello World!')119 invoke(echo, env={'SRC': 'hello.txt'})120121In 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.