diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2014-04-25 00:07:45 +0200 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2014-04-25 00:07:45 +0200 |
commit | 23d3f68126168ce7be34aac162a22c7200ebc657 (patch) | |
tree | c014c841538002caef88a2aaae2925c16ef15428 /examples | |
parent | 8259846071cbd9d34ac0b4259a9ace374ef32ffb (diff) | |
download | click-23d3f68126168ce7be34aac162a22c7200ebc657.tar.gz |
Added docs for building complex click applications
Diffstat (limited to 'examples')
-rw-r--r-- | examples/repo/repo.py | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/examples/repo/repo.py b/examples/repo/repo.py index f6065cc..d9d8e92 100644 --- a/examples/repo/repo.py +++ b/examples/repo/repo.py @@ -1,13 +1,28 @@ +import os import click +class Repo(object): + + def __init__(self, home): + self.home = home + + def __repr__(self): + return '<Repo %r>' % self.home + + +pass_repo = click.make_pass_decorator(Repo) + + @click.group() -@click.option('--repo-home', envvar='REPO_HOME') -def cli(repo_home): +@click.option('--repo-home', envvar='REPO_HOME', default='.repo') +@click.pass_context +def cli(ctx, repo_home): """Repo is a command line tool that showcases how to build complex command line interfaces with Click. """ - print('Repo home: %s' % repo_home) + # Create a repo object and remember it as as the context object. + ctx.obj = Repo(os.path.abspath(repo_home)) @cli.command() @@ -15,13 +30,15 @@ def cli(repo_home): @click.argument('dest', required=False) @click.option('--shallow/--deep', default=False, help='Makes a checkout shallow or deep. Deep by default.') -def clone(src, dest, shallow): +@pass_repo +def clone(repo, src, dest, shallow): """Clones a repository. This will clone the repository at SRC into the folder DEST. If DEST is not provided this will automatically use the last path component of SRC and create that folder. """ + print('Repo: %s' % repo) print('Source: %s' % src) print('Destination: %s' % dest) print('Shallow: %s' % shallow) @@ -29,11 +46,13 @@ def clone(src, dest, shallow): @cli.command() @click.confirmation_option() -def delete(): +@pass_repo +def delete(repo): """Deletes a repository. This will throw away the current repository. """ + print('Repo: %s' % repo) print('Deleted') @@ -41,11 +60,13 @@ def delete(): @click.option('--username', prompt=True, required=True) @click.option('--email', prompt='E-Mail', required=True) @click.password_option() -def setuser(username, email, password): +@pass_repo +def setuser(repo, username, email, password): """Sets the user credentials. This will override the current user config. """ + print('Repo: %s' % repo) print('Username: %s' % username) print('E-Mail: %s' % email) print('Password: %s' % ('*' * len(password))) |