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
Provenance
- Commit
c0b076e- Tree
fbf4f00- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1318a20
1 file changed+96 added−0 removed
modified:docs/commands.rst+96 added
271 unmodified lines272 invoke(cli, prog_name='cli', args=['--help'])273 274 In case a command exists in more than one source, the first source wins.275 276+277 Multi Command Chaining278 ----------------------279 280 .. versionadded:: 3.038 unmodified lines319 :attr:`Context.invoked_subcommands` attribute instead which is a list320 (note the trailing "s").321 322 323+Multi Command Pipelines324+-----------------------325+326+.. versionadded:: 3.0327+328+A very common usecase of multi command chaining is to have one command329+process the result of the previous command. There are various ways in330+which this can be facilitated. The most obvious way is to store a value331+on the context object and process it from function to function. This332+works by decorating a function with :func:`pass_context` after which the333+context object is provided and a subcommand can store it's data there.334+335+Another way to accomplish this is to setup pipelines by returning336+processing functions. Think of it like this: when a subcommand gets337+invoked it processes all of it's parameters and comes up with a plan of338+how to do it's processing. At that point it then returns a processing339+function and returns.340+341+Where do the returned functions go? The chained multicommand can register342+a callback with :meth:`MultiCommand.resultcallback` that goes over all343+these functions and then invoke them.344+345+To make this a bit more concrete consider this example:346+347+.. click:example::348+349+ @click.group(chain=True, invoke_without_command=True)350+ @click.option('-i', '--input', type=click.File('r'))351+ def cli(input):352+ pass353+354+ @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)361+362+ @cli.command('uppercase')363+ def make_uppercase():364+ def processor(iterator):365+ for line in iterator:366+ yield line.upper()367+ return processor368+369+ @cli.command('lowercase')370+ def make_lowercase():371+ def processor(iterator):372+ for line in iterator:373+ yield line.lower()374+ return processor375+376+ @cli.command('strip')377+ def make_strip():378+ def processor(iterator):379+ for line in iterator:380+ yield line.strip()381+ return processor382+383+That's a lot in one go, so let's go through it step by step.384+385+1. 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.390+2. 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.395+3. 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.399+400+After that point we can register as many subcommands as we want and each401+subcommand can return a processor function to modify the stream of lines.402+403+One important thing of note is that Click shuts down the context after404+each callback has been run. This means that for instance file types405+cannot be accessed in the `processor` functions as the files will already406+be closed there. This limitation is unlikely to change because it would407+make resource handling much more complicated. For such it's recommended408+to not use the file type and manually open the file through409+:func:`open_file`.410+411+For a more complex example that also improves upon handling of the412+pipelines have a look at the `imagepipe multi command chaining demo413+<https://github.com/mitsuhiko/click/tree/master/examples/imagepipe>`__ in414+the Click repository. It implements a pipeline based image editing tool415+that has a nice internal structure for the pipelines.416+417+418 Overriding Defaults419 -------------------420 421 By default, the default value for a parameter is pulled from the83 unmodified lines