/usr/share/doc/python-optcomplete/conditional.html is in python-optcomplete 1.2-11.1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | <?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="Docutils 0.3.0: http://docutils.sourceforge.net/" />
<title>Using optcomplete Conditionally</title>
<meta name="author" content="Martin Blais <blais@furius.ca>" />
<meta name="date" content="2004-01-28" />
<link rel="stylesheet" href="/docutils-style.css" type="text/css" />
</head>
<body>
<div class="document" id="using-optcomplete-conditionally">
<h1 class="title">Using optcomplete Conditionally</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Martin Blais <<a class="reference" href="mailto:blais@furius.ca">blais@furius.ca</a>></td></tr>
<tr><th class="docinfo-name">Date:</th>
<td>2004-01-28</td></tr>
</tbody>
</table>
<div class="abstract topic">
<p class="topic-title">Abstract</p>
<p>Notes on adding conditionals for scripts to keep on working even without
optcomplete.</p>
</div>
<div class="section" id="motivation">
<h1><a name="motivation">Motivation</a></h1>
<p>Sometimes it is important for a particular script to be able to work without the
presence of the non-standard <tt class="literal"><span class="pre">optcomplete</span></tt> module. After that, if that is the
only thing that is missing, the script is still able to do its work without
completion.</p>
<p>This document contains little notes on how to do this but still take advantage
of auto-generated completion if the module is available.</p>
<div class="section" id="note">
<h2><a name="note">Note</a></h2>
<p>Note that if your shell binding hooks a program to the autocomplete function,
the program will be invoked without arguments and you will see the output that
calling it as such would generate, unless the program is written to handle this
case by manually recognizing completion is being called for, e.g.:</p>
<pre class="literal-block">
....
elif 'COMP_LINE' in os.environ:
return -1
</pre>
</div>
</div>
<div class="section" id="simple">
<h1><a name="simple">Simple</a></h1>
<p>You can import like this:</p>
<pre class="literal-block">
try:
import optcomplete
except ImportError:
optcomplete = None
</pre>
<p>Then, further one, you can use a global conditional for the completion code:</p>
<pre class="literal-block">
if optcomplete:
...
</pre>
<p>e.g.:</p>
<pre class="literal-block">
if optcomplete:
optcomplete.autocomplete(parser, ['.*\.tar.*'])
</pre>
</div>
<div class="section" id="with-subcommands">
<h1><a name="with-subcommands">With Subcommands</a></h1>
<p>Importing, with a base class for commands:</p>
<pre class="literal-block">
try:
import optcomplete
CmdComplete = optcomplete.CmdComplete
except ImportError:
optcomplete, CmdComplete = None, object
</pre>
<p>Then you can use the same conditional:</p>
<pre class="literal-block">
class CmdCompleting(CmdFoo):
...
if optcomplete:
completer = .......
def addopts( self, parser ):
CmdFoo.addopts(self, parser)
opt = parser.add_option(....
if optcomplete:
opt.completer = .....
</pre>
<p>You can also check for completions manually and return nothing if the invocation
is from completion:</p>
<pre class="literal-block">
# subcommand completions
if optcomplete:
scmap = {}
for sc in subcmds:
for n in sc.names:
scmap[n] = sc
listcter = optcomplete.ListCompleter(scmap.keys())
subcter = optcomplete.ListCompleter(
['some', 'default', 'commands', 'completion'])
optcomplete.autocomplete(
gparser, listcter, None, subcter, subcommands=scmap)
elif 'COMP_LINE' in os.environ:
return -1
</pre>
</div>
</div>
</body>
</html>
|