{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using [`click`](https://github.com/pallets/click) interactively\n", "\n", "We all know that `argparse` sucks, and `click` is a common replacement.\n", "\n", "See the `click` documentation for the more information about [__Complex Arguments__](http://click.pocoo.org/5/complex/#calling-convention). " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ " import click" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ " @click.command()\n", " @click.option('--count', default=1, help='Number of greetings.')\n", " @click.option('--name', prompt='Your name',\n", " help='The person to greet.')\n", " def hello(count, name):\n", " \"\"\"Simple program that greets NAME for a total of COUNT times.\"\"\"\n", " for x in range(count):\n", " print('Hello! %s!' % name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Create an a `click.Context` " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ " from pytest import fixture\n", " ctx = fixture(lambda: click.Context(hello))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `click.Context.invoke` the command." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello! ❤ deathbeds!\n" ] } ], "source": [ " _test_interactive_invocation = lambda ctx: ctx.invoke(hello, name=\"❤ deathbeds\")\n", " __name__ == '__main__' and _test_interactive_invocation(ctx())" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }