Skip to content

Commit f9ec319

Browse files
committed
Add documentation for allow_clipboard attribute
1 parent 8779cca commit f9ec319

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

CHANGELOG.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.5.0 (TBD)
2+
* Enhancements
3+
* add `allow_clipboard` initialization parameter and attribute to disable ability to
4+
add output to the operating system clipboard
5+
16
## 2.4.3 (January 27, 2023)
27
* Bug Fixes
38
* Fixed ValueError caused when passing `Cmd.columnize()` strings wider than `display_width`.
@@ -84,7 +89,7 @@
8489
* Added `ap_completer_type` keyword arg to `Cmd2ArgumentParser.__init__()` which saves a call
8590
to `set_ap_completer_type()`. This keyword will also work with `add_parser()` when creating subcommands
8691
if the base command's parser is a `Cmd2ArgumentParser`.
87-
* New function `register_argparse_argument_parameter()` allows developers to specify custom
92+
* New function `register_argparse_argument_parameter()` allows developers to specify custom
8893
parameters to be passed to the argparse parser's `add_argument()` method. These parameters will
8994
become accessible in the resulting argparse Action object when modifying `ArgparseCompleter` behavior.
9095
* Using `SimpleTable` in the output for the following commands to improve appearance.
@@ -257,12 +262,12 @@
257262

258263
## 1.3.7 (August 27, 2020)
259264
* Bug Fixes
260-
* Fixes an issue introduced in 1.3.0 with processing command strings containing terminator/separator
265+
* Fixes an issue introduced in 1.3.0 with processing command strings containing terminator/separator
261266
character(s) that are manually passed to a command that uses argparse.
262267

263268
## 1.3.6 (August 27, 2020)
264269
* Breaking changes
265-
* The functions cmd2 adds to Namespaces (`get_statement()` and `get_handler()`) are now
270+
* The functions cmd2 adds to Namespaces (`get_statement()` and `get_handler()`) are now
266271
`Cmd2AttributeWrapper` objects named `cmd2_statement` and `cmd2_handler`. This makes it
267272
easy to filter out which attributes in an `argparse.Namespace` were added by `cmd2`.
268273
* Deprecations
@@ -288,7 +293,7 @@
288293
* Breaking changes
289294
* CommandSet command functions (do_, complete_, help_) will no longer have the cmd2 app
290295
passed in as the first parameter after `self` since this is already a class member.
291-
* Renamed `install_command_set()` and `uninstall_command_set()` to `register_command_set()` and
296+
* Renamed `install_command_set()` and `uninstall_command_set()` to `register_command_set()` and
292297
`unregister_command_set()` for better name consistency.
293298
* Bug Fixes
294299
* Fixed help formatting bug in `Cmd2ArgumentParser` when `metavar` is a tuple
@@ -298,8 +303,8 @@
298303
* Removed explicit type hints that fail due to a bug in 3.5.2 favoring comment-based hints instead
299304
* When passing a ns_provider to an argparse command, will now attempt to resolve the correct
300305
CommandSet instance for self. If not, it'll fall back and pass in the cmd2 app
301-
* Other
302-
* Added missing doc-string for new cmd2.Cmd __init__ parameters
306+
* Other
307+
* Added missing doc-string for new cmd2.Cmd __init__ parameters
303308
introduced by CommandSet enhancement
304309

305310
## 1.3.2 (August 10, 2020)
@@ -314,8 +319,8 @@
314319
## 1.3.1 (August 6, 2020)
315320
* Bug Fixes
316321
* Fixed issue determining whether an argparse completer function required a reference to a containing
317-
CommandSet. Also resolves issues determining the correct CommandSet instance when calling the argparse
318-
argument completer function. Manifested as a TypeError when using `cmd2.Cmd.path_complete` as a completer
322+
CommandSet. Also resolves issues determining the correct CommandSet instance when calling the argparse
323+
argument completer function. Manifested as a TypeError when using `cmd2.Cmd.path_complete` as a completer
319324
for an argparse-based command defined in a CommandSet
320325

321326
## 1.3.0 (August 4, 2020)
@@ -324,15 +329,15 @@
324329
with your cmd2 application.
325330
* Other
326331
* Marked with_argparser_and_unknown_args pending deprecation and consolidated implementation into
327-
with_argparser
332+
with_argparser
328333

329334
## 1.2.1 (July 14, 2020)
330335
* Bug Fixes
331336
* Relax minimum version of `importlib-metadata` to >= 1.6.0 when using Python < 3.8
332337

333338
## 1.2.0 (July 13, 2020)
334339
* Bug Fixes
335-
* Fixed `typing` module compatibility issue with Python 3.5 prior to 3.5.4
340+
* Fixed `typing` module compatibility issue with Python 3.5 prior to 3.5.4
336341
* Enhancements
337342
* Switched to getting version using `importlib.metadata` instead of using `pkg_resources`
338343
* Improves `cmd2` application launch time on systems that have a lot of Python packages on `sys.path`
@@ -341,7 +346,7 @@
341346
## 1.1.0 (June 6, 2020)
342347
* Bug Fixes
343348
* Fixed issue where subcommand usage text could contain a subcommand alias instead of the actual name
344-
* Fixed bug in `ArgparseCompleter` where `fill_width` could become negative if `token_width` was large
349+
* Fixed bug in `ArgparseCompleter` where `fill_width` could become negative if `token_width` was large
345350
relative to the terminal width.
346351
* Enhancements
347352
* Made `ipy` consistent with `py` in the following ways
@@ -365,7 +370,7 @@
365370
after parsing fails, just return instead of raising an exception.
366371
* Added explicit handling of `SystemExit`. If a command raises this exception, the command loop will be
367372
gracefully stopped.
368-
373+
369374
## 1.0.2 (April 06, 2020)
370375
* Bug Fixes
371376
* Ctrl-C now stops a running text script instead of just the current `run_script` command
@@ -388,9 +393,9 @@
388393
* Bug Fixes
389394
* Corrected issue where the actual new value was not always being printed in do_set. This occurred in cases where
390395
the typed value differed from what the setter had converted it to.
391-
* Fixed bug where ANSI style sequences were not correctly handled in `utils.truncate_line()`.
396+
* Fixed bug where ANSI style sequences were not correctly handled in `utils.truncate_line()`.
392397
* Fixed bug where pyscripts could edit `cmd2.Cmd.py_locals` dictionary.
393-
* Fixed bug where cmd2 set `sys.path[0]` for a pyscript to cmd2's working directory instead of the
398+
* Fixed bug where cmd2 set `sys.path[0]` for a pyscript to cmd2's working directory instead of the
394399
script file's directory.
395400
* Fixed bug where `sys.path` was not being restored after a pyscript ran.
396401
* Enhancements

docs/api/cmd.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ cmd2.Cmd
6565
The symbol name which :ref:`features/scripting:Python Scripts` run
6666
using the :ref:`features/builtin_commands:run_pyscript` command can use
6767
to reference the parent ``cmd2`` application.
68+
69+
.. attribute:: allow_clipboard
70+
71+
If ``True``, ``cmd2`` will allow output to be written to or appended to
72+
the operating system pasteboard. If ``False``, this capability will not
73+
be allowed. See :ref:`features/clipboard:Clipboard Integration` for more
74+
information.

docs/features/clipboard.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ contents of the clipboard by ending the command with two greater than symbols:
2525
Developers
2626
----------
2727

28+
You can control whether the above user features of adding output to the
29+
operating system clipboard are allowed for the user by setting the
30+
:attr:`~cmd2.Cmd.allow_clipboard` attribute. The default value is ``True``.
31+
Set it to ``False`` and the above functionality will generate an error
32+
message instead of adding the output to the clipboard.
33+
:attr:`~cmd2.Cmd.allow_clipboard` can be set upon initialization, and you can
34+
change it at any time from within your code.
35+
2836
If you would like your ``cmd2`` based application to be able to use the
2937
clipboard in additional or alternative ways, you can use the following methods
3038
(which work uniformly on Windows, macOS, and Linux).

tests/test_cmd2.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ def test_set(base_app):
171171
out, err = run_cmd(base_app, 'set quiet')
172172
expected = normalize(
173173
"""
174-
Name Value Description
174+
Name Value Description
175175
===================================================================================================
176-
quiet True Don't print nonessential feedback
176+
quiet True Don't print nonessential feedback
177177
"""
178178
)
179179
assert out == expected
@@ -729,6 +729,7 @@ def test_pipe_to_shell_error(base_app):
729729
assert not out
730730
assert "Pipe process exited with code" in err[0]
731731

732+
732733
try:
733734
# try getting the contents of the clipboard
734735
_ = clipboard.get_paste_buffer()
@@ -742,7 +743,8 @@ def test_pipe_to_shell_error(base_app):
742743
else:
743744
can_paste = True
744745

745-
@pytest.mark.skipif(can_paste, reason="Pyperclip could not find a copy/paste mechanism for your system")
746+
747+
@pytest.mark.skipif(not can_paste, reason="Pyperclip could not find a copy/paste mechanism for your system")
746748
def test_send_to_paste_buffer(base_app):
747749
# Test writing to the PasteBuffer/Clipboard
748750
run_cmd(base_app, 'help >')
@@ -755,6 +757,7 @@ def test_send_to_paste_buffer(base_app):
755757
assert appended_contents.startswith(paste_contents)
756758
assert len(appended_contents) > len(paste_contents)
757759

760+
758761
def test_get_paste_buffer_exception(base_app, mocker, capsys):
759762
# Force get_paste_buffer to throw an exception
760763
pastemock = mocker.patch('pyperclip.paste')
@@ -769,11 +772,13 @@ def test_get_paste_buffer_exception(base_app, mocker, capsys):
769772
# this just checks that cmd2 is surfacing whatever error gets raised by pyperclip.paste
770773
assert 'ValueError' in err and 'foo' in err
771774

775+
772776
def test_allow_clipboard_initializer(base_app):
773777
assert base_app.allow_clipboard == True
774778
noclipcmd = cmd2.Cmd(allow_clipboard=False)
775779
assert noclipcmd.allow_clipboard == False
776780

781+
777782
# if clipboard access is not allowed, cmd2 should check that first
778783
# before it tries to do anything with pyperclip, that's why we can
779784
# safely run this test without skipping it if pyperclip doesn't

0 commit comments

Comments
 (0)