Skip to content

Instantly share code, notes, and snippets.

@berdario
Created October 31, 2014 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berdario/c9c501f8c58ea41c6bfc to your computer and use it in GitHub Desktop.
Save berdario/c9c501f8c58ea41c6bfc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import argparse
def mkvirtualenv_argparser():
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--python')
parser.add_argument('-i', action='append', dest='packages', help='Install \
a package after the environment is created. This option may be repeated.')
parser.add_argument('-r', dest='requirements', help='Provide a pip \
requirements file to install a base set of packages into the new environment.')
parser.add_argument('-d', '--dont-activate', action='store_false',
default=True, dest='activate', help="After \
creation, continue with the existing shell (don't \
activate the new environment).")
return parser
def new_cmd(argv):
parser = mkvirtualenv_argparser()
parser.add_argument('-a', dest='project', help='Provide a full path to a \
project directory to associate with the new environment.')
parser.add_argument('envname')
args, rest = parser.parse_known_args(argv)
print(args)
def mkproject_cmd(argv):
parser = mkvirtualenv_argparser()
parser.add_argument('envname')
parser.add_argument(
'-t', action='append', default=[], dest='templates', help='Multiple \
templates may be selected. They are applied in the order specified on the \
command line.')
parser.add_argument(
'-l', '--list', action='store_true', help='List available templates.')
args, rest = parser.parse_known_args(argv)
print(args)
def mktmpenv_cmd(argv):
parser = mkvirtualenv_argparser()
args, rest = parser.parse_known_args(argv)
print(args)
if __name__ == '__main__':
cmds = dict((cmd[:-4], fun)
for cmd, fun in globals().items() if cmd.endswith('_cmd'))
if sys.argv[1:]:
cmds[sys.argv[1]](sys.argv[2:])
#!/usr/bin/env python
import click
@click.group()
def pew():
pass
mkvenv_command = lambda f: pew.command()(
click.option('--python', '-p')(
click.option('-i', multiple=True, help='Install \
a package after the environment is created. This option may be repeated.')(
click.option('--requirements', '-r', help='Provide a pip \
requirements file to install a base set of packages into the new environment.')(
click.option('activate', '--dont-activate', '-d', is_flag=True, default=True, help="After \
creation, continue with the existing shell (don't \
activate the new environment).")(f)))))
@mkvenv_command
@click.option('project', '-a', help='Provide a full path to a \
project directory to associate with the new environment.')
@click.argument('envname')
def new(**kwargs):
print(kwargs)
@mkvenv_command
@click.argument('envname')
@click.option('templates', '-t', multiple=True, help='Multiple \
templates may be selected. They are applied in the order specified on the \
command line.')
@click.option('--list', '-l', is_flag=True, help='List available templates.')
def mkproject(**kwargs):
print(kwargs)
@mkvenv_command
def mktmpenv(**kwargs):
print(kwargs)
if __name__ == '__main__':
pew()
#!/usr/bin/env python
import click
@click.group()
def pew():
pass
@pew.command()
@click.option('--python', '-p')
@click.option('-i', multiple=True, help='Install \
a package after the environment is created. This option may be repeated.')
@click.option('--requirements', '-r', help='Provide a pip \
requirements file to install a base set of packages into the new environment.')
@click.option('activate', '--dont-activate', '-d', is_flag=True, default=True, help="After \
creation, continue with the existing shell (don't \
activate the new environment).")
@click.option('project', '-a', help='Provide a full path to a \
project directory to associate with the new environment.')
@click.argument('envname')
def new(**kwargs):
print(kwargs)
@pew.command()
@click.option('--python', '-p')
@click.option('-i', multiple=True, help='Install \
a package after the environment is created. This option may be repeated.')
@click.option('--requirements', '-r', help='Provide a pip \
requirements file to install a base set of packages into the new environment.')
@click.option('activate', '--dont-activate', '-d', is_flag=True, default=True, help="After \
creation, continue with the existing shell (don't \
activate the new environment).")
@click.argument('envname')
@click.option('templates', '-t', multiple=True, help='Multiple \
templates may be selected. They are applied in the order specified on the \
command line.')
@click.option('--list', '-l', is_flag=True, help='List available templates.')
def mkproject(**kwargs):
print(kwargs)
@pew.command()
@click.option('--python', '-p')
@click.option('-i', multiple=True, help='Install \
a package after the environment is created. This option may be repeated.')
@click.option('--requirements', '-r', help='Provide a pip \
requirements file to install a base set of packages into the new environment.')
@click.option('activate', '--dont-activate', '-d', is_flag=True, default=True, help="After \
creation, continue with the existing shell (don't \
activate the new environment).")
def mktmpenv(**kwargs):
print(kwargs)
if __name__ == '__main__':
pew()
#!/usr/bin/env python
from functools import reduce
import click
@click.group()
def pew():
pass
def compose(f, g):
return lambda x: f(g(x))
def arguments(*args):
return reduce(compose, args, lambda x:x)
def pew_command(*args):
return compose(pew.command(), arguments(*args))
mkvenv_args = arguments(
click.option('--python', '-p')
, click.option('-i', multiple=True, help='Install \
a package after the environment is created. This option may be repeated.')
, click.option('--requirements', '-r', help='Provide a pip \
requirements file to install a base set of packages into the new environment.')
, click.option('activate', '--dont-activate', '-d', is_flag=True, default=True, help="After \
creation, continue with the existing shell (don't \
activate the new environment).")
)
@pew_command(
mkvenv_args,
click.option('project', '-a', help='Provide a full path to a \
project directory to associate with the new environment.'),
click.argument('envname')
)
def new(**kwargs):
print(kwargs)
@pew_command(
mkvenv_args,
click.argument('envname'),
click.option('templates', '-t', multiple=True, help='Multiple \
templates may be selected. They are applied in the order specified on the \
command line.'),
click.option('--list', '-l', is_flag=True, help='List available templates.')
)
def mkproject(**kwargs):
print(kwargs)
@pew_command(mkvenv_args)
def mktmpenv(**kwargs):
print(kwargs)
if __name__ == '__main__':
pew()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment