Merge branch '7.x'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parents 7d11997, 68c9328 · Browse files · 9 files·+109 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+109 added−24 removed
- other:+109 added−24 removed
Provenance
- Commit
ffcce4f- Tree
94e3d14- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
7d11997- Parent 2
68c9328
9 files changed+109 added−24 removed
modified:click/_winconsole.py+3 added-3 removed
22 unmodified lines23 PyObject_GetBuffer = pythonapi.PyObject_GetBuffer24 PyBuffer_Release = pythonapi.PyBuffer_Release25 except ImportError:26 pythonapi = None27-from ctypes.wintypes import LPWSTR, LPCWSTR27+from ctypes.wintypes import LPWSTR, LPCWSTR, HANDLE28 29 30 c_ssize_p = POINTER(c_ssize_t)31 90 unmodified lines122 buffer = get_buffer(b, writable=True)123 code_units_to_be_read = bytes_to_be_read // 2124 code_units_read = c_ulong()125 126- rv = ReadConsoleW(self.handle, buffer, code_units_to_be_read,126+ rv = ReadConsoleW(HANDLE(self.handle), buffer, code_units_to_be_read,127 byref(code_units_read), None)128 if GetLastError() == ERROR_OPERATION_ABORTED:129 # wait for KeyboardInterrupt130 time.sleep(0.1)24 unmodified lines155 code_units_to_be_written = min(bytes_to_be_written,156 MAX_BYTES_WRITTEN) // 2157 code_units_written = c_ulong()158 159- WriteConsoleW(self.handle, buf, code_units_to_be_written,159+ WriteConsoleW(HANDLE(self.handle), buf, code_units_to_be_written,160 byref(code_units_written), None)161 bytes_written = 2 * code_units_written.value162 163 if bytes_written == 0 and bytes_to_be_written > 0:144 unmodified linesmodified:click/core.py+14 added-4 removed
890 unmodified lines891 def __repr__(self):892 return '<%s %s>' % (self.__class__.__name__, self.name)893 894 def get_usage(self, ctx):895+ """Formats the usage line into a string and returns it.896+897+ Calls :meth:`format_usage` internally.898+ """899 formatter = ctx.make_formatter()900 self.format_usage(ctx, formatter)901 return formatter.getvalue().rstrip('\n')902 4 unmodified lines907 rv = rv + [help_option]908 return rv909 910 def format_usage(self, ctx, formatter):907- """Writes the usage line into the formatter."""911+ """Writes the usage line into the formatter.912+913+ This is a low-level method called by :meth:`get_usage`.914+ """915 pieces = self.collect_usage_pieces(ctx)916 formatter.write_usage(ctx.command_path, ' '.join(pieces))917 918 def collect_usage_pieces(self, ctx):35 unmodified lines954 param.add_to_parser(parser, ctx)955 return parser956 957 def get_help(self, ctx):951- """Formats the help into a string and returns it. This creates a952- formatter and will call into the following formatting methods:958+ """Formats the help into a string and returns it.959+960+ Calls :meth:`format_help` internally.961 """962 formatter = ctx.make_formatter()963 self.format_help(ctx, formatter)964 return formatter.getvalue().rstrip('\n')4 unmodified lines969 970 def format_help(self, ctx, formatter):971 """Writes the help into the formatter if it exists.972 965- This calls into the following methods:973+ This is a low-level method called by :meth:`get_help`.974 975+ This calls the following methods:976+977 - :meth:`format_usage`978 - :meth:`format_help_text`979 - :meth:`format_options`980 - :meth:`format_epilog`975 unmodified linesmodified:docs/advanced.rst+5 added-3 removed
10 unmodified lines11 12 Command Aliases13 ---------------14 15-Many tools support aliases for commands. For instance, you can configure16-``git`` to accept ``git ci`` as alias for ``git commit``. Other tools17-also support auto-discovery for aliases by automatically shortening them.15+Many tools support aliases for commands (see `Command alias example16+<https://github.com/pallets/click/tree/master/examples/aliases>`_).17+For instance, you can configure ``git`` to accept ``git ci`` as alias for18+``git commit``. Other tools also support auto-discovery for aliases by19+automatically shortening them.20 21 Click does not support this out of the box, but it's very easy to customize22 the :class:`Group` or any other :class:`MultiCommand` to provide this23 functionality.385 unmodified linesmodified:docs/arguments.rst+12 added-5 removed
6 unmodified lines7 8 Arguments work similarly to :ref:`options <options>` but are positional.9 They also only support a subset of the features of options due to their10 syntactical nature. Click will also not attempt to document arguments for11-you and wants you to document them manually in order to avoid ugly help12-pages.11+you and wants you to :ref:`document them manually <documenting-arguments>`12+in order to avoid ugly help pages.13 14 Basic Arguments15 ---------------16 7 unmodified lines24 25 @click.command()26 @click.argument('filename')27 def touch(filename):28+ """Print FILENAME."""29 click.echo(filename)30 31 And what it looks like:32 19 unmodified lines52 @click.command()53 @click.argument('src', nargs=-1)54 @click.argument('dst', nargs=1)55 def copy(src, dst):56+ """Move file SRC to DST."""57 for fn in src:58 click.echo('move %s to folder %s' % (fn, dst))59 60 And what it looks like:41 unmodified lines102 @click.command()103 @click.argument('input', type=click.File('rb'))104 @click.argument('output', type=click.File('wb'))105 def inout(input, output):106+ """Copy contents of INPUT to OUTPUT."""107 while True:108 chunk = input.read(1024)109 if not chunk:110 break27 unmodified lines138 139 .. click:example::140 141 @click.command()139- @click.argument('f', type=click.Path(exists=True))140- def touch(f):141- click.echo(click.format_filename(f))142+ @click.argument('filename', type=click.Path(exists=True))143+ def touch(filename):144+ """Print FILENAME if the file exists."""145+ click.echo(click.format_filename(filename))146 147 And what it does:148 149 .. click:run::52 unmodified lines202 203 @click.command()204 @click.argument('src', envvar='SRC', type=click.File('r'))205 def echo(src):206+ """Print value of SRC environment variable."""207 click.echo(src.read())208 209 And from the command line:210 28 unmodified lines239 240 @click.command()241 @click.argument('files', nargs=-1, type=click.Path())242 def touch(files):243+ """Print all FILES file names."""244 for filename in files:245 click.echo(filename)246 247 And from the command line:9 unmodified lines257 258 @click.command(context_settings={"ignore_unknown_options": True})259 @click.argument('files', nargs=-1, type=click.Path())260 def touch(files):261+ """Print all FILES file names."""262 for filename in files:263 click.echo(filename)264 265 And from the command line:5 unmodified linesmodified:docs/documentation.rst+48 added-4 removed
30 unmodified lines31 .. click:run::32 33 invoke(hello, args=['--help'])34 35-Arguments cannot be documented this way. This is to follow the general36-convention of Unix tools of using arguments for only the most necessary37-things and to document them in the introduction text by referring to them38-by name.35 36+.. _documenting-arguments:37+38+Documenting Arguments39+~~~~~~~~~~~~~~~~~~~~~40+41+:func:`click.argument` does not take a ``help`` parameter. This is to42+follow the general convention of Unix tools of using arguments for only43+the most necessary things, and to document them in the command help text44+by referring to them by name.45+46+You might prefer to reference the argument in the description:47+48+.. click:example::49+50+ @click.command()51+ @click.argument('filename')52+ def touch(filename):53+ """Print FILENAME."""54+ click.echo(filename)55+56+And what it looks like:57+58+.. click:run::59+60+ invoke(touch, args=['--help'])61+62+Or you might prefer to explicitly provide a description of the argument:63+64+.. click:example::65+66+ @click.command()67+ @click.argument('filename')68+ def touch(filename):69+ """Print FILENAME.70+71+ FILENAME is the name of the file to check.72+ """73+ click.echo(filename)74+75+And what it looks like:76+77+.. click:run::78+79+ invoke(touch, args=['--help'])80+81+For more examples, see the examples in :doc:`/arguments`.82+83+84 Preventing Rewrapping85 ---------------------86 87 The default behavior of Click is to rewrap text based on the width of the154 unmodified linesmodified:docs/parameters.rst+2 added-2 removed
21 unmodified lines22 * automatic prompting for missing input23 * act as flags (boolean or otherwise)24 * option values can be pulled from environment variables, arguments can not25 * options are fully documented in the help page, arguments are not26- (this is intentional as arguments might be too specific to be27- automatically documented)26+ (:ref:`this is intentional <documenting-arguments>` as arguments27+ might be too specific to be automatically documented)28 29 On the other hand arguments, unlike options, can accept an arbitrary number30 of arguments. Options can strictly ever only accept a fixed number of31 arguments (defaults to 1).118 unmodified linesmodified:docs/python3.rst-3 removed
6 unmodified lines7 it suffers from the Unicode text model in Python 3. All examples in the8 documentation were written so that they could run on both Python 2.x and9 Python 3.4 or higher.10 11-At the moment, it is strongly recommended to use Python 2 for Click12-utilities unless Python 3 is a hard requirement.13-11 .. _python3-limitations:12 13 Python 3 Limitations14 --------------------171 unmodified linesmodified:examples/aliases/aliases.py+24 added
12 unmodified lines13 def __init__(self):14 self.path = os.getcwd()15 self.aliases = {}16 17+ def add_alias(self, alias, cmd):18+ self.aliases.update({alias: cmd})19+20 def read_config(self, filename):21 parser = configparser.RawConfigParser()22 parser.read([filename])23 try:24 self.aliases.update(parser.items('aliases'))25 except configparser.NoSectionError:26 pass27 28+ def write_config(self, filename):29+ parser = configparser.RawConfigParser()30+ parser.add_section('aliases')31+ for key, value in self.aliases.items():32+ parser.set('aliases', key, value)33+ with open(filename, 'wb') as file:34+ parser.write(file)35 36+37 pass_config = click.make_pass_decorator(Config, ensure=True)38 39 40 class AliasedGroup(click.Group):78 unmodified lines119 @pass_config120 def status(config):121 """Shows the status."""122 click.echo('Status for %s' % config.path)123+124+125+@cli.command()126+@pass_config127+@click.argument("alias_", metavar='ALIAS', type=click.STRING)128+@click.argument("cmd", type=click.STRING)129+@click.option('--config_file', type=click.Path(exists=True, dir_okay=False),130+ default="aliases.ini")131+def alias(config, alias_, cmd, config_file):132+ """Adds an alias to the specified configuration file."""133+ config.add_alias(alias_, cmd)134+ config.write_config(config_file)135+ click.echo('Added %s as alias for %s' % (alias_, cmd))modified:CHANGES.rst+1 added
29 unmodified lines30 - Fix link in ``unicode_literals`` error message. (`#1151`_)31 - Add support for colored output on UNIX Jupyter notebooks. (`#1185`_)32 - Remove unused compat shim for ``bytes``. (`#1195`_)33 - Expand unit testing around termui, especially getchar (Windows). (`#1116`_)34+- Fix output on Windows Python 2.7 built with MSVC 14. #134235 36 .. _#1167: https://github.com/pallets/click/pull/116737 .. _#1151: https://github.com/pallets/click/pull/115138 .. _#1185: https://github.com/pallets/click/issues/1185634 unmodified lines