From 23d3f68126168ce7be34aac162a22c7200ebc657 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 25 Apr 2014 00:07:45 +0200 Subject: Added docs for building complex click applications --- examples/repo/repo.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'examples') 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 '' % 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))) -- cgit v1.2.1