Skip to content

Commit 09985f6

Browse files
committed
Start of new argparse completion example
1 parent 41019de commit 09985f6

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

examples/argparse_completion.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
"""
4+
A simple example demonstrating how to integrate tab completion with argparse-based commands.
5+
"""
6+
import argparse
7+
from typing import List
8+
9+
from cmd2 import Cmd, Cmd2ArgumentParser, with_argparser
10+
from cmd2.utils import basic_complete
11+
12+
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
13+
14+
# This data is used to demonstrate delimiter_complete
15+
file_strs = \
16+
[
17+
'/home/user/file.db',
18+
'/home/user/file space.db',
19+
'/home/user/another.db',
20+
'/home/other user/maps.db',
21+
'/home/other user/tests.db'
22+
]
23+
24+
25+
def choices_function() -> List[str]:
26+
"""Choices functions are useful when the choice list is dynamically generated (e.g. from data in a database)"""
27+
return ['a', 'dynamic', 'list']
28+
29+
30+
def completer_function(text: str, line: str, begidx: int, endidx: int) -> List[str]:
31+
"""
32+
A tab completion function not dependent on instance data. Since custom tab completion operations commonly
33+
need to modify cmd2's instance variables related to tab completion, it will be rare to need a completer
34+
function. completer_method should be used in those cases.
35+
"""
36+
return basic_complete(text, line, begidx, endidx, food_item_strs)
37+
38+
39+
class ArgparseCompletion(Cmd):
40+
def __init__(self, *args, **kwargs):
41+
super().__init__(*args, **kwargs)
42+
self.sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
43+
44+
def choices_method(self) -> List[str]:
45+
"""Choices methods are useful when the choice list is based on instance data of your application"""
46+
return self.sport_item_strs
47+
48+
# Parser for complete command
49+
complete_parser = Cmd2ArgumentParser(description="Command demonstrating tab completion with argparse\n"
50+
"Notice even the flags of this command tab complete")
51+
52+
# Tab complete from a list using argparse choices. Set metavar if you don't
53+
# want the entire choices list showing in the usage text for this command.
54+
complete_parser.add_argument('--choices', choices=food_item_strs, metavar="CHOICE")
55+
56+
# Tab complete from choices provided by a choices function and choices method
57+
complete_parser.add_argument('--choices_function', choices_function=choices_function)
58+
complete_parser.add_argument('--choices_method', choices_method=choices_method)
59+
60+
# Tab complete using a completer function and completer method
61+
complete_parser.add_argument('--completer_function', completer_function=completer_function)
62+
complete_parser.add_argument('--completer_method', completer_method=Cmd.path_complete)
63+
64+
@with_argparser(complete_parser)
65+
def do_complete(self, _: argparse.Namespace) -> None:
66+
"""The complete command"""
67+
self.poutput("I do nothing")
68+
69+
70+
if __name__ == '__main__':
71+
import sys
72+
app = ArgparseCompletion()
73+
sys.exit(app.cmdloop())

0 commit comments

Comments
 (0)