Skip to content
Myr

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 file

Take it

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 1why, one step back
1(arguments)=

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 2why, one step back
2

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 3why, one step back
3# Arguments

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 4why, one step back
4

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

lines 5–6why, one step back
5```{currentmodule} click
6```

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

lines 7–9why, one step back
7
8Arguments are:9

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

lines 10–12why, one step back
10* 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 back
13
14Useful and often used kwargs are:15

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

lines 16–17why, one step back
16* `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 back
18

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 19why, one step back
19## Basic Arguments

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 20why, one step back
20

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 21why, one step back
21A 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 back
22
23Example:24

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 25why, one step back
25```{eval-rst}

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

lines 26–38why, one step back
26.. 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 back
39```

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 40why, one step back
40

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 41why, one step back
41An 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 back
42

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

lines 43–44why, one step back
43```{admonition} Note on Required Arguments
44:class: note

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 45why, one step back
45

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

lines 46–47why, one step back
46 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 back
48

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 49why, one step back
49## Multiple Arguments

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 50why, one step back
50

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 51why, one step back
51To 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 back
52

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 53why, one step back
53```{eval-rst}

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

lines 54–68why, one step back
54.. 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 back
69```

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 70why, one step back
70

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

lines 71–72why, one step back
71```{admonition} Note on Handling Files
72:class: note

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 73why, one step back
73

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

lines 74–75why, one step back
74 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 back
76

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 77why, one step back
77## Argument Escape Sequences

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 78why, one step back
78

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 79why, one step back
79If 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 back
80
81Example usage:82

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 83why, one step back
83```{eval-rst}

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

lines 84–97why, one step back
84.. 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 back
98```

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 99why, one step back
99

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 100why, one step back
100If 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 back
101

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 102why, one step back
102```{eval-rst}

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

lines 103–116why, one step back
103.. 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 back
117```

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 118why, one step back
118

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 119why, one step back
119(environment-variables)=

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 120why, one step back
120

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 121why, one step back
121## Environment Variables

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

line 122why, one step back
122

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 123why, one step back
123Arguments 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 back
124
125Checking one environment variable:126

refactor from rest to mystJaron Rosenaubefore adoptionde3e8a3

line 127why, one step back
127```{eval-rst}

rename extension from .rst to .mdJaron Rosenaubefore adoption5aad9b6

lines 128–165why, one step back
128.. 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 back
166```