parameters.md
This file
Convert parameters file to md. (#2953)Rowlando13before adoptiona6cc378
lines 1–21why, one step back1(parameters)=23# Parameters45```{currentmodule} click6```78Click supports only two principle types of parameters for scripts (by design): options and arguments.910## Options1112- Are optional.13- Recommended to use for everything except subcommands, urls, or files.14- Can take a fixed number of arguments. The default is 1. They may be specified multiple times using {ref}`multiple-options`.15- Are fully documented by the help page.16- Have automatic prompting for missing input.17- Can act as flags (boolean or otherwise).18- Can be pulled from environment variables.1920## Arguments21Merge `stable` into `main` after `CHANGES` rST to Markdown conversion (#3563)Kevin Deldyckebefore adoption8a1b1a3
line 22why, one step back22- Are optional within reason, but not entirely so.Convert parameters file to md. (#2953)Rowlando13before adoptiona6cc378
lines 23–33why, one step back23- Recommended to use for subcommands, urls, or files.24- Can take an arbitrary number of arguments.25- Are not fully documented by the help page since they may be too specific to be automatically documented. For more see {ref}`documenting-arguments`.26- Can be pulled from environment variables but only explicitly named ones. For more see {ref}`environment-variables`.2728On each principle type you can specify {ref}`parameter-types`. Specifying these types helps Click add details to your help pages and help with the handling of those types.2930(parameter-names)=3132## Parameter Names3334Three distinct strings designate a parameter, and each one has its own audience:Convert parameters file to md. (#2953)Rowlando13before adoptiona6cc378
line 35why, one step back3536- `decls` are the declarations you pass to the decorator. Click parses them to derive every other string in this section.37- `spec` is what the user reads in `--help` and types on the command line. Read it from {attr}`Parameter.spec`, or from {meth}`Parameter.get_help_spec` for the longer form the help page lays out in its left column.38- `name` is what Click uses internally to identify the parameter. It is used as a Python argument and is passed to the decorated function. Read it from {attr}`Parameter.name`.Convert parameters file to md. (#2953)Rowlando13before adoptiona6cc378
line 39why, one step back3940Click also keeps the parsed declarations on the parameter. {attr}`Parameter.opts` holds every spelling an option answers to, and {attr}`Parameter.secondary_opts` the ones that set a boolean flag to false.41Convert parameters file to md. (#2953)Rowlando13before adoptiona6cc378
lines 42–45why, one step back42```{eval-rst}43.. click:example::4445 @click.command()46 @click.argument('recipe')47 @click.option('-g', '--gluten-free/--no-gluten-free', default=False)48 def bake(recipe, gluten_free):49 """Bake the RECIPE, with or without gluten."""50 note = 'no gluten' if gluten_free else 'with gluten'51 click.echo(f'{recipe}: {note}')Convert parameters file to md. (#2953)Rowlando13before adoptiona6cc378
lines 52–54why, one step back5253.. click:run::5455 invoke(bake, ['--gluten-free', 'brioche'], prog_name='bake')565758.. click:run::5960 invoke(bake, ['--help'], prog_name='bake')Convert parameters file to md. (#2953)Rowlando13before adoptiona6cc378
line 61why, one step back61```6263The example holds one argument and one option. The table lists all five strings for each of them:6465| | `@click.argument('recipe')` | `@click.option('-g', '--gluten-free/--no-gluten-free')` |66|---|---|---|67| `decls` | `('recipe',)` | `('-g', '--gluten-free/--no-gluten-free')` |68| `spec` | `RECIPE` | `--gluten-free` |69| `name` | `recipe` | `gluten_free` |70| `opts` | `['recipe']` | `['-g', '--gluten-free']` |71| `secondary_opts` | `[]` | `['--no-gluten-free']` |7273Click derives an option's `name` from the declaration with the longest prefix, so it picks `--gluten-free` over `-g`. It then lowercases that declaration and replaces its dashes with underscores, which gives `gluten_free`. The name must match the Python argument name of the decorated function. More declarations are available for options and are covered in {ref}`options`.7475An argument has a single declaration. It becomes the only entry in `opts`, and `name` is that declaration lowercased, with dashes replaced by underscores. The `spec` is the name in upper case. `secondary_opts` stays empty, since the parser binds an argument by name and never matches a spelling. To choose the spec an argument shows in help text, see {ref}`doc-meta-variables`.7677The help page above shows each `spec` where the user reads it: `RECIPE` in the usage line, and `-g, --gluten-free / --no-gluten-free` in the left column of the options list, which is the longer form {meth}`Parameter.get_help_spec` returns.