arguments.md
This file
Path
docs/arguments.md
166 lines · 4.7 KiB · Markdown
Last changed by
refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
History of this fileTake it
refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 1why, one step back1(arguments)=rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 2why, one step back2refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 3why, one step back3# Argumentsrename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 4why, one step back4refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
lines 5–6why, one step back5```{currentmodule} click6```rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 7–9why, one step back78Arguments are:9refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
lines 10–12why, one step back10* Are positional in nature.11* Similar to a limited version of {ref}`options <options>` that can take an arbitrary number of inputs12* {ref}`Documented manually <documenting-arguments>`.rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 13–15why, one step back1314Useful and often used kwargs are:15refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
lines 16–17why, one step back16* `default`: Passes a default.17* `nargs`: Sets the number of arguments. Set to -1 to take an arbitrary number.rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 18why, one step back18refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 19why, one step back19## Basic Argumentsrename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 20why, one step back20refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 21why, one step back21A minimal {class}`click.Argument` solely takes one string argument: the name of the argument. This will assume the argument is required, has no default, and is of the type `str`.rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 22–24why, one step back2223Example:24refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 25why, one step back25```{eval-rst}rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 26–38why, one step back26.. click:example::2728 @click.command()29 @click.argument('filename')30 def touch(filename: str):31 """Print FILENAME."""32 click.echo(filename)3334And from the command line:3536.. click:run::3738 invoke(touch, args=['foo.txt'])refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 39why, one step back39```rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 40why, one step back40refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 41why, one step back41An argument may be assigned a {ref}`parameter type <parameter-types>`. If no type is provided, the type of the default value is used. If no default value is provided, the type is assumed to be {data}`STRING`.rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 42why, one step back42refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
lines 43–44why, one step back43```{admonition} Note on Required Arguments44:class: noterename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 45why, one step back45refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
lines 46–47why, one step back46 It is possible to make an argument required by setting `required=True`. It is not recommended since we think command line tools should gracefully degrade into becoming no ops. We think this because command line tools are often invoked with wildcard inputs and they should not error out if the wildcard is empty.47```rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 48why, one step back48refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 49why, one step back49## Multiple Argumentsrename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 50why, one step back50refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 51why, one step back51To set the number of argument use the `nargs` kwarg. It can be set to any positive integer and -1. Setting it to -1, makes the number of arguments arbitrary (which is called variadic) and can only be used once. The arguments are then packed as a tuple and passed to the function.rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 52why, one step back52refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 53why, one step back53```{eval-rst}rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 54–68why, one step back54.. click:example::5556 @click.command()57 @click.argument('src', nargs=1)58 @click.argument('dsts', nargs=-1)59 def copy(src: str, dsts: tuple[str, ...]):60 """Move file SRC to DST."""61 for destination in dsts:62 click.echo(f"Copy {src} to folder {destination}")6364And from the command line:6566.. click:run::6768 invoke(copy, args=['foo.txt', 'usr/david/foo.txt', 'usr/mitsuko/foo.txt'])refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 69why, one step back69```rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 70why, one step back70refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
lines 71–72why, one step back71```{admonition} Note on Handling Files72:class: noterename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 73why, one step back73refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
lines 74–75why, one step back74 This is not how you should handle files and files paths. This merely used as a simple example. See {ref}`handling-files` to learn more about how to handle files in parameters.75```rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 76why, one step back76refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 77why, one step back77## Argument Escape Sequencesrename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 78why, one step back78refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 79why, one step back79If you want to process arguments that look like options, like a file named `-foo.txt` or `--foo.txt` , you must pass the `--` separator first. After you pass the `--`, you may only pass arguments. This is a common feature for POSIX command line tools.rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 80–82why, one step back8081Example usage:82refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 83why, one step back83```{eval-rst}rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 84–97why, one step back84.. click:example::8586 @click.command()87 @click.argument('files', nargs=-1, type=click.Path())88 def touch(files):89 """Print all FILES file names."""90 for filename in files:91 click.echo(filename)9293And from the command line:9495.. click:run::9697 invoke(touch, ['--', '-foo.txt', 'bar.txt'])refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 98why, one step back98```rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 99why, one step back99refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 100why, one step back100If you don't like the `--` marker, you can set ignore_unknown_options to True to avoid checking unknown options:rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 101why, one step back101refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 102why, one step back102```{eval-rst}rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 103–116why, one step back103.. click:example::104105 @click.command(context_settings={"ignore_unknown_options": True})106 @click.argument('files', nargs=-1, type=click.Path())107 def touch(files):108 """Print all FILES file names."""109 for filename in files:110 click.echo(filename)111112And from the command line:113114.. click:run::115116 invoke(touch, ['-foo.txt', 'bar.txt'])refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 117why, one step back117```rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 118why, one step back118refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 119why, one step back119(environment-variables)=rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 120why, one step back120refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 121why, one step back121## Environment Variablesrename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
line 122why, one step back122refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 123why, one step back123Arguments can use environment variables. To do so, pass the name(s) of the environment variable(s) via `envvar` in `click.argument`.rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 124–126why, one step back124125Checking one environment variable:126refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 127why, one step back127```{eval-rst}rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6
lines 128–165why, one step back128.. click:example::129130 @click.command()131 @click.argument('src', envvar='SRC', type=click.File('r'))132 def echo(src):133 """Print value of SRC environment variable."""134 click.echo(src.read())135136And from the command line:137138.. click:run::139140 with isolated_filesystem():141 # Writing the file in the filesystem.142 with open('hello.txt', 'w') as f:143 f.write('Hello World!')144 invoke(echo, env={'SRC': 'hello.txt'})145146147Checking multiple environment variables:148149.. click:example::150151 @click.command()152 @click.argument('src', envvar=['SRC', 'SRC_2'], type=click.File('r'))153 def echo(src):154 """Print value of SRC environment variable."""155 click.echo(src.read())156157And from the command line:158159.. click:run::160161 with isolated_filesystem():162 # Writing the file in the filesystem.163 with open('hello.txt', 'w') as f:164 f.write('Hello World from second variable!')165 invoke(echo, env={'SRC_2': 'hello.txt'})refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3
line 166why, one step back166```