change ext from .rst to .md
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by denuoweb · committed by denuoweb · parent b3a608e · Browse files · 2 files·+206 added−206 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+206 added−206 removed
- other:+206 added−206 removed
Provenance
- Commit
11384eb- Tree
ccaf5d0- Author
denuoweb <jaron@rosenau.info>- Committer
denuoweb <jaron@rosenau.info>- Parent
b3a608e
2 files changed+206 added−206 removed
added:docs/quickstart.md+206 added
1+Quickstart2+==========3+4+.. currentmodule:: click5+6+Install7+----------------------8+Install from PyPI::9+10+ pip install click11+12+Installing into a virtual environment is highly recommended. We suggest :ref:`virtualenv-heading`.13+14+Examples15+-----------------------16+17+Some standalone examples of Click applications are packaged with Click. They are available in the `examples folder <https://github.com/pallets/click/tree/main/examples>`_ of the repo.18+19+* `inout <https://github.com/pallets/click/tree/main/examples/inout>`_ : A very simple example of an application that can read from files and write to files and also accept input from stdin or write to stdout.20+* `validation <https://github.com/pallets/click/tree/main/examples/validation>`_ : A simple example of an application that performs custom validation of parameters in different ways.21+* `naval <https://github.com/pallets/click/tree/main/examples/naval>`_ : Port of the `docopt <http://docopt.org/>`_ naval example.22+* `colors <https://github.com/pallets/click/tree/main/examples/colors>`_ : A simple example that colorizes text. Uses colorama on Windows.23+* `aliases <https://github.com/pallets/click/tree/main/examples/aliases>`_ : An advanced example that implements :ref:`aliases`.24+* `imagepipe <https://github.com/pallets/click/tree/main/examples/imagepipe>`_ : A complex example that implements some :ref:`command-pipelines` . It chains together image processing instructions. Requires pillow.25+* `repo <https://github.com/pallets/click/tree/main/examples/repo>`_ : An advanced example that implements a Git-/Mercurial-like command line interface.26+* `complex <https://github.com/pallets/click/tree/main/examples/complex>`_ : A very advanced example that implements loading subcommands dynamically from a plugin folder.27+* `termui <https://github.com/pallets/click/tree/main/examples/termui>`_ : A simple example that showcases terminal UI helpers provided by click.28+29+Basic Concepts - Creating a Command30+-----------------------------------31+32+Click is based on declaring commands through decorators. Internally, there33+is a non-decorator interface for advanced use cases, but it's discouraged34+for high-level usage.35+36+A function becomes a Click command line tool by decorating it through37+:func:`click.command`. At its simplest, just decorating a function38+with this decorator will make it into a callable script:39+40+.. click:example::41+42+ import click43+44+ @click.command()45+ def hello():46+ click.echo('Hello World!')47+48+What's happening is that the decorator converts the function into a49+:class:`Command` which then can be invoked::50+51+ if __name__ == '__main__':52+ hello()53+54+And what it looks like:55+56+.. click:run::57+58+ invoke(hello, args=[], prog_name='python hello.py')59+60+And the corresponding help page:61+62+.. click:run::63+64+ invoke(hello, args=['--help'], prog_name='python hello.py')65+66+Echoing67+-------68+69+Why does this example use :func:`echo` instead of the regular70+:func:`print` function? The answer to this question is that Click71+attempts to support different environments consistently and to be very72+robust even when the environment is misconfigured. Click wants to be73+functional at least on a basic level even if everything is completely74+broken.75+76+What this means is that the :func:`echo` function applies some error77+correction in case the terminal is misconfigured instead of dying with a78+:exc:`UnicodeError`.79+80+The echo function also supports color and other styles in output. It81+will automatically remove styles if the output stream is a file. On82+Windows, colorama is automatically installed and used. See83+:ref:`ansi-colors`.84+85+If you don't need this, you can also use the `print()` construct /86+function.87+88+Nesting Commands89+----------------90+91+Commands can be attached to other commands of type :class:`Group`. This92+allows arbitrary nesting of scripts. As an example here is a script that93+implements two commands for managing databases:94+95+.. click:example::96+97+ @click.group()98+ def cli():99+ pass100+101+ @click.command()102+ def initdb():103+ click.echo('Initialized the database')104+105+ @click.command()106+ def dropdb():107+ click.echo('Dropped the database')108+109+ cli.add_command(initdb)110+ cli.add_command(dropdb)111+112+As you can see, the :func:`group` decorator works like the :func:`command`113+decorator, but creates a :class:`Group` object instead which can be given114+multiple subcommands that can be attached with :meth:`Group.add_command`.115+116+For simple scripts, it's also possible to automatically attach and create a117+command by using the :meth:`Group.command` decorator instead. The above118+script can instead be written like this:119+120+.. click:example::121+122+ @click.group()123+ def cli():124+ pass125+126+ @cli.command()127+ def initdb():128+ click.echo('Initialized the database')129+130+ @cli.command()131+ def dropdb():132+ click.echo('Dropped the database')133+134+You would then invoke the :class:`Group` in your entry points or other invocations::135+136+ if __name__ == '__main__':137+ cli()138+139+140+Registering Commands Later141+--------------------------142+143+Instead of using the ``@group.command()`` decorator, commands can be144+decorated with the plain ``@click.command()`` decorator and registered145+with a group later with ``group.add_command()``. This could be used to146+split commands into multiple Python modules.147+148+.. code-block:: python149+150+ @click.command()151+ def greet():152+ click.echo("Hello, World!")153+154+.. code-block:: python155+156+ @click.group()157+ def group():158+ pass159+160+ group.add_command(greet)161+162+163+Adding Parameters164+-----------------165+166+To add parameters, use the :func:`option` and :func:`argument` decorators:167+168+.. click:example::169+170+ @click.command()171+ @click.option('--count', default=1, help='number of greetings')172+ @click.argument('name')173+ def hello(count, name):174+ for x in range(count):175+ click.echo(f"Hello {name}!")176+177+What it looks like:178+179+.. click:run::180+181+ invoke(hello, args=['--help'], prog_name='python hello.py')182+183+Switching to Entry Points184+-------------------------185+186+In the code you wrote so far there is a block at the end of the file which187+looks like this: ``if __name__ == '__main__':``. This is traditionally188+how a standalone Python file looks like. With Click you can continue189+doing that, but a better way is to package your app with an entry point.190+191+There are two main (and many more) reasons for this:192+193+The first one is that installers automatically generate executable194+wrappers for Windows so your command line utilities work on Windows too.195+196+The second reason is that entry point scripts work with virtualenv on Unix197+without the virtualenv having to be activated. This is a very useful198+concept which allows you to bundle your scripts with all requirements into199+a virtualenv.200+201+Click is perfectly equipped to work with that and in fact the rest of the202+documentation will assume that you are writing applications as distributed203+packages.204+205+Look at the :doc:`entry-points` chapter before reading the rest as the examples206+assume that you will be using entry points.deleted:docs/quickstart.rst-206 removed
1-Quickstart2-==========3-4-.. currentmodule:: click5-6-Install7-----------------------8-Install from PyPI::9-10- pip install click11-12-Installing into a virtual environment is highly recommended. We suggest :ref:`virtualenv-heading`.13-14-Examples15------------------------16-17-Some standalone examples of Click applications are packaged with Click. They are available in the `examples folder <https://github.com/pallets/click/tree/main/examples>`_ of the repo.18-19-* `inout <https://github.com/pallets/click/tree/main/examples/inout>`_ : A very simple example of an application that can read from files and write to files and also accept input from stdin or write to stdout.20-* `validation <https://github.com/pallets/click/tree/main/examples/validation>`_ : A simple example of an application that performs custom validation of parameters in different ways.21-* `naval <https://github.com/pallets/click/tree/main/examples/naval>`_ : Port of the `docopt <http://docopt.org/>`_ naval example.22-* `colors <https://github.com/pallets/click/tree/main/examples/colors>`_ : A simple example that colorizes text. Uses colorama on Windows.23-* `aliases <https://github.com/pallets/click/tree/main/examples/aliases>`_ : An advanced example that implements :ref:`aliases`.24-* `imagepipe <https://github.com/pallets/click/tree/main/examples/imagepipe>`_ : A complex example that implements some :ref:`command-pipelines` . It chains together image processing instructions. Requires pillow.25-* `repo <https://github.com/pallets/click/tree/main/examples/repo>`_ : An advanced example that implements a Git-/Mercurial-like command line interface.26-* `complex <https://github.com/pallets/click/tree/main/examples/complex>`_ : A very advanced example that implements loading subcommands dynamically from a plugin folder.27-* `termui <https://github.com/pallets/click/tree/main/examples/termui>`_ : A simple example that showcases terminal UI helpers provided by click.28-29-Basic Concepts - Creating a Command30------------------------------------31-32-Click is based on declaring commands through decorators. Internally, there33-is a non-decorator interface for advanced use cases, but it's discouraged34-for high-level usage.35-36-A function becomes a Click command line tool by decorating it through37-:func:`click.command`. At its simplest, just decorating a function38-with this decorator will make it into a callable script:39-40-.. click:example::41-42- import click43-44- @click.command()45- def hello():46- click.echo('Hello World!')47-48-What's happening is that the decorator converts the function into a49-:class:`Command` which then can be invoked::50-51- if __name__ == '__main__':52- hello()53-54-And what it looks like:55-56-.. click:run::57-58- invoke(hello, args=[], prog_name='python hello.py')59-60-And the corresponding help page:61-62-.. click:run::63-64- invoke(hello, args=['--help'], prog_name='python hello.py')65-66-Echoing67--------68-69-Why does this example use :func:`echo` instead of the regular70-:func:`print` function? The answer to this question is that Click71-attempts to support different environments consistently and to be very72-robust even when the environment is misconfigured. Click wants to be73-functional at least on a basic level even if everything is completely74-broken.75-76-What this means is that the :func:`echo` function applies some error77-correction in case the terminal is misconfigured instead of dying with a78-:exc:`UnicodeError`.79-80-The echo function also supports color and other styles in output. It81-will automatically remove styles if the output stream is a file. On82-Windows, colorama is automatically installed and used. See83-:ref:`ansi-colors`.84-85-If you don't need this, you can also use the `print()` construct /86-function.87-88-Nesting Commands89-----------------90-91-Commands can be attached to other commands of type :class:`Group`. This92-allows arbitrary nesting of scripts. As an example here is a script that93-implements two commands for managing databases:94-95-.. click:example::96-97- @click.group()98- def cli():99- pass100-101- @click.command()102- def initdb():103- click.echo('Initialized the database')104-105- @click.command()106- def dropdb():107- click.echo('Dropped the database')108-109- cli.add_command(initdb)110- cli.add_command(dropdb)111-112-As you can see, the :func:`group` decorator works like the :func:`command`113-decorator, but creates a :class:`Group` object instead which can be given114-multiple subcommands that can be attached with :meth:`Group.add_command`.115-116-For simple scripts, it's also possible to automatically attach and create a117-command by using the :meth:`Group.command` decorator instead. The above118-script can instead be written like this:119-120-.. click:example::121-122- @click.group()123- def cli():124- pass125-126- @cli.command()127- def initdb():128- click.echo('Initialized the database')129-130- @cli.command()131- def dropdb():132- click.echo('Dropped the database')133-134-You would then invoke the :class:`Group` in your entry points or other invocations::135-136- if __name__ == '__main__':137- cli()138-139-140-Registering Commands Later141---------------------------142-143-Instead of using the ``@group.command()`` decorator, commands can be144-decorated with the plain ``@click.command()`` decorator and registered145-with a group later with ``group.add_command()``. This could be used to146-split commands into multiple Python modules.147-148-.. code-block:: python149-150- @click.command()151- def greet():152- click.echo("Hello, World!")153-154-.. code-block:: python155-156- @click.group()157- def group():158- pass159-160- group.add_command(greet)161-162-163-Adding Parameters164------------------165-166-To add parameters, use the :func:`option` and :func:`argument` decorators:167-168-.. click:example::169-170- @click.command()171- @click.option('--count', default=1, help='number of greetings')172- @click.argument('name')173- def hello(count, name):174- for x in range(count):175- click.echo(f"Hello {name}!")176-177-What it looks like:178-179-.. click:run::180-181- invoke(hello, args=['--help'], prog_name='python hello.py')182-183-Switching to Entry Points184--------------------------185-186-In the code you wrote so far there is a block at the end of the file which187-looks like this: ``if __name__ == '__main__':``. This is traditionally188-how a standalone Python file looks like. With Click you can continue189-doing that, but a better way is to package your app with an entry point.190-191-There are two main (and many more) reasons for this:192-193-The first one is that installers automatically generate executable194-wrappers for Windows so your command line utilities work on Windows too.195-196-The second reason is that entry point scripts work with virtualenv on Unix197-without the virtualenv having to be activated. This is a very useful198-concept which allows you to bundle your scripts with all requirements into199-a virtualenv.200-201-Click is perfectly equipped to work with that and in fact the rest of the202-documentation will assume that you are writing applications as distributed203-packages.204-205-Look at the :doc:`entry-points` chapter before reading the rest as the examples206-assume that you will be using entry points.