Skip to content
Myr

Added pipeline documentation example

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 1318a20 · Browse files · 1 file+96 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+96 added−0 removed

  • other:+96 added−0 removed

1 file+96 added−0 removed

modified:docs/commands.rst+96 added
271 unmodified lines
272 invoke(cli, prog_name='cli', args=['--help'])273274In case a command exists in more than one source, the first source wins.275276277Multi Command Chaining278----------------------279280.. versionadded:: 3.0
38 unmodified lines
319:attr:`Context.invoked_subcommands` attribute instead which is a list320(note the trailing "s").321322323Multi Command Pipelines324-----------------------325326.. versionadded:: 3.0327328A very common usecase of multi command chaining is to have one command329process the result of the previous command. There are various ways in330which this can be facilitated. The most obvious way is to store a value331on the context object and process it from function to function. This332works by decorating a function with :func:`pass_context` after which the333context object is provided and a subcommand can store it's data there.334335Another way to accomplish this is to setup pipelines by returning336processing functions. Think of it like this: when a subcommand gets337invoked it processes all of it's parameters and comes up with a plan of338how to do it's processing. At that point it then returns a processing339function and returns.340341Where do the returned functions go? The chained multicommand can register342a callback with :meth:`MultiCommand.resultcallback` that goes over all343these functions and then invoke them.344345To make this a bit more concrete consider this example:346347.. click:example::348349 @click.group(chain=True, invoke_without_command=True)350 @click.option('-i', '--input', type=click.File('r'))351 def cli(input):352 pass353354 @cli.resultcallback()355 def process_pipeline(processors, input):356 iterator = (x.rstrip('\r\n') for x in input)357 for processor in processors:358 iterator = processor(iterator)359 for item in iterator:360 click.echo(item)361362 @cli.command('uppercase')363 def make_uppercase():364 def processor(iterator):365 for line in iterator:366 yield line.upper()367 return processor368369 @cli.command('lowercase')370 def make_lowercase():371 def processor(iterator):372 for line in iterator:373 yield line.lower()374 return processor375376 @cli.command('strip')377 def make_strip():378 def processor(iterator):379 for line in iterator:380 yield line.strip()381 return processor382383That's a lot in one go, so let's go through it step by step.3843851. The first thing is to make a :func:`group` that is chainable. In386 addition to that we also instruct Click to invoke even if no387 subcommand is defined. If this would not be done, then invoking an388 empty pipeline would produce the help page instead of running the389 result callbacks.3902. The next thing we do is to register a result callback on our group.391 This callback will be invoked with an argument which is the list of392 all return values of all subcommands and then the same keyword393 parameters as our group itself. This means we can access the input394 file easily there without having to use the context object.3953. In this result callback we create an iterator of all the lines in the396 input file and then pass this iterator through all the returned397 callbacks from all subcommands and finally we print all lines to398 stdout.399400After that point we can register as many subcommands as we want and each401subcommand can return a processor function to modify the stream of lines.402403One important thing of note is that Click shuts down the context after404each callback has been run. This means that for instance file types405cannot be accessed in the `processor` functions as the files will already406be closed there. This limitation is unlikely to change because it would407make resource handling much more complicated. For such it's recommended408to not use the file type and manually open the file through409:func:`open_file`.410411For a more complex example that also improves upon handling of the412pipelines have a look at the `imagepipe multi command chaining demo413<https://github.com/mitsuhiko/click/tree/master/examples/imagepipe>`__ in414the Click repository. It implements a pipeline based image editing tool415that has a nice internal structure for the pipelines.416417418Overriding Defaults419-------------------420421By default, the default value for a parameter is pulled from the
83 unmodified lines