More automated docs
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 fcd6d32 · Browse files · 6 files·+67 added−68 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+67 added−68 removed
- other:+67 added−68 removed
Provenance
- Commit
8254b79- Tree
05b30a2- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
fcd6d32
6 files changed+67 added−68 removed
modified:docs/advanced.rst+6 added-2 removed
23 unmodified lines24 aliases on the help page to avoid confusion.25 26 This following example implements a subclass of :class:`Group` that27 accepts a prefix for a command. So if there is a command called28-``push`` it would accept ``pus`` as alias if it's unique::28+``push`` it would accept ``pus`` as alias if it's unique:29 30+.. click:example::31+32 class AliasedGroup(click.Group):33 34 def get_command(self, ctx, cmd_name):35 rv = click.Group.get_command(self, ctx, cmd_name)6 unmodified lines42 elif len(matches) == 1:43 return click.Group.get_command(self, ctx, matches[0])44 ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))45 44-And it can be used like this then::46+And it can be used like this then:47 48+.. click:example::49+50 @click.command(cls=AliasedGroup)51 def cli():52 pass53 7 unmodified linesmodified:docs/clickdoctools.py+1 added
116 unmodified lines117 118 def __init__(self):119 self.namespace = {120 'click': click,121+ '__file__': 'dummy.py',122 }123 124 def declare(self, source):125 code = compile(source, '<docs>', 'exec')127 unmodified linesmodified:docs/commands.rst+20 added-29 removed
16 unmodified lines17 18 For groups and multi commands the situation looks different. In that case19 the callback fires whenever a subcommand fires (unless this behavior is20 changed). What this means in practice is that an outer command runs21-when an inner command runs::21+when an inner command runs:22 23- import click23+.. click:example::24 25 @click.group()26 @click.option('--debug/--no-debug', default=False)27 def cli(debug):2 unmodified lines30 @cli.command()31 def sync():32 print('Synching')33 34- if __name__ == '__main__':35- cli()34+Here is what this looks like:35 37-Here is what this looks like::36+.. click:run::37 39- $ python tool.py40- Usage: tool.py [OPTIONS] COMMAND [ARGS]...38+ invoke(cli, prog_name='tool.py')39+ println()40+ invoke(cli, prog_name='tool.py', args=['--debug', 'sync'])41 42- Commands:43- sync 44-45- Options:46- --debug / --no-debug 47- --help Show this message and exit.48- 49- $ python tool.py --debug sync50- Debug mode is on51- Synching 52-42 Nested Handling and Contexts43 ----------------------------44 45 As you can see from the earlier example the basic command group accepts a12 unmodified lines58 case the context is passed as first argument.59 60 The context can also carry a program specified object that can be61 used for the program's purposes. What this means is that you can build a73-script like this::62+script like this:63 75- import click64+.. click:example::65 66 @click.group()67 @click.option('--debug/--no-debug', default=False)68 @click.pass_context30 unmodified lines99 state and then storing it on the context and then to use a custom100 decorator to find the most recent object of this sort and pass it as first101 argument.102 114-For instance the :func:`pass_obj` decorator can be implemented like this::103+For instance the :func:`pass_obj` decorator can be implemented like this:104 116- import click105+.. click:example::106+107 from functools import update_wrapper108 109 def pass_obj(f):110 @click.pass_context20 unmodified lines131 always invoked instead of showing the help page. The context object also132 includes information about if the invocation would go to a subcommand or133 not.134 145-Example::135+Example:136 147- import click137+.. click:example::138 139 @click.group(invoke_without_command=True)140 @click.pass_context141 def cli(ctx):5 unmodified lines147 @cli.command()148 def sync():149 print('The subcommand')150 161- if __name__ == '__main__':162- cli()163-151 And how it works in practice::152 153 $ python tool.py 154 I was invoked without subcommand9 unmodified lines164 In addition to using :func:`click.group` you can also build your own165 custom multi commands. This is useful when you want to support commands166 being loaded lazily from plugins.167 181-A custom multi command just needs to implement a list and load method::168+A custom multi command just needs to implement a list and load method:169 170+.. click:example::171+172 import click173 import os174 175 plugin_folder = os.path.join(os.path.dirname(__file__), 'commands')21 unmodified lines197 198 if __name__ == '__main__':199 cli()200 212-These custom classes can also be used with decorators::201+These custom classes can also be used with decorators:202 203+.. click:example::204+205 @click.command(cls=MyCLI)206 def cli():207 passmodified:docs/complex.rst+15 added-5 removed
71 unmodified lines72 case we use the basic :func:`click.group` which allows us to register73 other click commands below it.74 75 For this command we also want to accept some parameters that configure the76-state of our tool::76+state of our tool:77 78+.. click:example::79+80 import os81 import click82 83 29 unmodified lines113 The First Child Command114 ```````````````````````115 116 So let's add our first child command to it. Let's go with the clone115-command::117+command:118 119+.. click:example::120+121 @cli.command()122 @click.argument('src')123 @click.argument('dest', required=False)124 def clone(src, dest):3 unmodified lines128 you can imagine one way is to use the :func:`pass_context` function which129 again will make our callback also get the context passed on which we130 memorized the repo. Even better there is a second version of this131 decorator called :func:`pass_obj` which will just pass the stored object,128-in our case the repo::132+in our case the repo:133 134+.. click:example::135+136 @cli.command()137 @click.argument('src')138 @click.argument('dest', required=False)139 @click.pass_obj20 unmodified lines160 161 Built-in support for this is provided by the :func:`make_pass_decorator`162 factory which will create decorators for us that find objects. So in our163 case we know that we want to find the closest ``Repo`` object. So let's158-make a decorator for this::164+make a decorator for this:165 166+.. click:example::167+168 pass_repo = click.make_pass_decorator(Repo)169 170 If we now use ``pass_repo`` instead of ``pass_obj`` we will always get a163-repo instead of something else::171+repo instead of something else:172 173+.. click:example::174+175 @cli.command()176 @click.argument('src')177 @click.argument('dest', required=False)178 @pass_repo2 unmodified linesmodified:docs/documentation.rst+18 added-28 removed
9 unmodified lines10 11 Commands and options accept help arguments. In case of commands the doc12 string of the function is automatically used if provided.13 14-Simple example::14+Simple example:15 16+.. click:example::17+18 @click.command()19 @click.option('--count', default=1, help='number of greetings')20 @click.argument('name')21 def hello(count, name):22 """This script prints hello NAME COUNT times."""23 for x in range(count):24 print('Hello %s!' % name)25 24-And what it looks like::26+And what it looks like:27 26- $ python hello.py --help27- Usage: hello.py [OPTIONS] NAME28+.. click:run::29 29- This script prints hello NAME COUNT times.30+ invoke(hello, args=['--help'])31 31- Options:32- --count=COUNT number of greetings33- --help Show this message and exit.34-32 Arguments cannot be documented this way. This is to follow general33 convention of Unix tools to use arguments only for the most necessary34 things and to document them in the introduction text by referring to them35 by name.3 unmodified lines39 40 Options and parameters accept a ``metavar`` argument that can change the41 meta variable in the help page. The default version is the parameter name42 in uppercase with underscores and sometimes annotated differently if46-optional. This can be customized at all levels::43+optional. This can be customized at all levels:44 45+.. click:example::46+47 @click.command(options_metavar='<options>')48 @click.option('--count', default=1, help='number of greetings',49 metavar='<int>')50 @click.argument('name', metavar='<name>')1 unmodified line52 """This script prints hello <name> <int> times."""53 for x in range(count):54 print('Hello %s!' % name)55 57-Example::56+Example:57 59- Usage: hello.py <options> <name>58+.. click:run::59 61- This script prints hello <name> <int> times.62-63- Options:64- --count=<int> number of greetings65- --help Show this message and exit.60+ invoke(hello, args=['--help'])61 62 63 Command Short Help64 ------------------65 66 For commands a short help is generated. By default it's the first part67 (until the first dot) of the help message of the command unless it's too73-long. This can also be overridden::68+long. This can also be overridden:69 70+.. click:example::71+72 @click.group()73 def cli():74 """A simple command line tool."""75 4 unmodified lines80 @cli.command('delete', short_help='delete the repo')81 def delete():82 """Deletes the repository."""83 87-And what it looks like::84+And what it looks like:85 89- Usage: repo.py [OPTIONS] COMMAND [ARGS]...86+.. click:run::87 91- A simple command line tool.92-93- Commands:94- delete delete the repo95- init init the repo96-97- Options:98- --help Show this message and exit.88+ invoke(cli, prog_name='repo.py')modified:docs/setuptools.rst+7 added-4 removed
23 unmodified lines24 yourpackage/25 cli.py26 setup.py27 28-Contents of ``cli.py``::28+Contents of ``cli.py``:29 30+.. click:example::31+32 import click33 34 @click.command()35 def cli():35 unmodified lines71 $ virtualenv venv72 $ . venv/bin/activate73 $ pip install --editable .74 73-Afterwards your command should be available::75+Afterwards your command should be available:76 75- $ yourscript76- Hello World!77+.. click:run::78+79+ invoke(cli, prog_name='yourscript', prog_prefix='')