Skip to content

Commit 6fc6bee

Browse files
committed
Example added demonstrating bash completion bridging argcomplete with AutoCompleter
1 parent 63aa28c commit 6fc6bee

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

examples/bash_completion.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
# coding=utf-8
3+
# PYTHON_ARGCOMPLETE_OK - This is required at the beginning of the file to enable argcomplete support
4+
"""A simple example demonstrating integration with argcomplete"""
5+
import argparse
6+
7+
optional_strs = ['Apple', 'Banana', 'Cranberry', 'Durian', 'Elderberry']
8+
9+
bash_parser = argparse.ArgumentParser(prog='base')
10+
11+
bash_parser.add_argument('option', choices=['load', 'export', 'reload'])
12+
13+
bash_parser.add_argument('-u', '--user', help='User name')
14+
bash_parser.add_argument('-p', '--passwd', help='Password')
15+
16+
input_file = bash_parser.add_argument('-f', '--file', type=str, help='Input File')
17+
18+
if __name__ == '__main__':
19+
from cmd2.argcomplete_bridge import bash_complete
20+
# bash_complete flags this argument telling AutoCompleter to yield to bash to perform
21+
# tab completion of a file path
22+
bash_complete(input_file)
23+
24+
flag_opt = bash_parser.add_argument('-o', '--optional', help='Optional flag with choices')
25+
setattr(flag_opt, 'arg_choices', optional_strs)
26+
27+
# Handle bash completion if it's installed
28+
# This early check allows the script to bail out early to provide tab-completion results
29+
# to the argcomplete library. Putting this at the end of the file would cause the full application
30+
# to load fulfill every tab-completion request coming from bash. This can cause a notable delay
31+
# on the bash prompt.
32+
try:
33+
# only move forward if we can import CompletionFinder and AutoCompleter
34+
from cmd2.argcomplete_bridge import CompletionFinder
35+
from cmd2.argparse_completer import AutoCompleter
36+
import sys
37+
if __name__ == '__main__':
38+
completer = CompletionFinder()
39+
40+
# completer will return results to argcomplete and exit the script
41+
completer(bash_parser, AutoCompleter(bash_parser))
42+
except ImportError:
43+
pass
44+
45+
# Intentionally below the bash completion code to reduce tab completion lag
46+
import cmd2
47+
48+
49+
class DummyApp(cmd2.Cmd):
50+
"""
51+
Dummy cmd2 app
52+
"""
53+
54+
def __init__(self):
55+
super().__init__()
56+
57+
58+
if __name__ == '__main__':
59+
args = bash_parser.parse_args()
60+
61+
# demonstrates some handling of the command line parameters
62+
63+
if args.user is None:
64+
user = input('Username: ')
65+
else:
66+
user = args.user
67+
68+
if args.passwd is None:
69+
import getpass
70+
password = getpass.getpass()
71+
else:
72+
password = args.passwd
73+
74+
if args.file is not None:
75+
print('Loading file: {}'.format(args.file))
76+
77+
# Clear the argumentns so cmd2 doesn't try to parse them
78+
sys.argv = sys.argv[:1]
79+
80+
app = DummyApp()
81+
app.cmdloop()

0 commit comments

Comments
 (0)