initial revision of pycol
[pycol.git] / example.py
1 #!/usr/bin/python3
2
3 import pycol
4 from clicmd import *
5 import sys
6 import traceback
7 import textwrap
8
9 def callback(cmdline, kvargs):
10     print("match: %s"%(str(kvargs)))
11
12 normal = "\033[0m"
13 bold = "\033[1m"
14
15 ip_list = []
16
17 def help_callback(cmdline, kvargs):
18     wrapper = textwrap.TextWrapper(initial_indent="  ",
19                                    subsequent_indent="  ")
20     wrapper2 = textwrap.TextWrapper(initial_indent="  ",
21                                    subsequent_indent="      ")
22     for c in cmdline.context:
23         # XXX if terminal supports it, use bold and italic
24         print("%s%s%s"%(bold, c.to_expr(), normal))
25         lines = c.get_help().split("\n")
26         print("\n".join(wrapper.wrap(lines[0])))
27         for l in lines[1:]:
28             print("\n".join(wrapper2.wrap(l)))
29         # XXX use a pager
30
31 def ip_callback_add(cmdline, kvargs):
32     global ip_list
33     if kvargs["<ip>"] in ip_list:
34         print("already in list")
35         return
36     ip_list.append(kvargs["<ip>"])
37
38 def ip_callback_del(cmdline, kvargs):
39     global ip_list
40     ip_list.remove(kvargs["<saved-ip>"])
41
42 def ip_callback_info(cmdline, kvargs):
43     global ip_list
44     if "<saved-ip>" in kvargs:
45         print("%s is in list"%kvargs["<saved-ip>"])
46     else:
47         print("%s is not in list"%kvargs["<ip>"])
48
49 ctx = PycolContext([
50     CmdBuilder(
51         expr = "toto a|b [coin] [bar]",
52         token_desc = {
53             "toto": TextCliCmd("toto", help_str = "help for toto"),
54             "a": TextCliCmd("a", help_str = "help for a"),
55             "b": TextCliCmd("b", help_str = "help for b"),
56             "coin": TextCliCmd("coin", help_str = "help for coin"),
57             "bar": TextCliCmd("bar", help_str = "help for bar"),
58         },
59         cb = callback,
60         help_str = "help for command toto. This is a very very " +
61         "long long help to check that the wrapper is able to " +
62         "wrap the text properly."
63     ),
64
65     CmdBuilder(
66         expr = "titi [pouet] <anytext> <count>",
67         token_desc = {
68             "titi": TextCliCmd("titi", help_str = "help for titi"),
69             "pouet": TextCliCmd("pouet", help_str = "help for pouet"),
70             "<anytext>": AnyTextCliCmd(help_str = "help for anytext"),
71             "<count>": IntCliCmd(help_str = "help for count"),
72         },
73         cb = callback,
74         help_str = "help for command titi"
75     ),
76
77     CmdBuilder(
78         expr = "regexp <re>",
79         token_desc = {
80             "regexp": TextCliCmd("regexp", help_str = "help for regexp"),
81             "<re>": RegexpCliCmd("0x([0-9a-fA-F])", help_str = "an hex number"),
82         },
83         cb = callback,
84         help_str = "help for command regexp"
85     ),
86
87     CmdBuilder(
88         expr = "ip add <ip>",
89         token_desc = {
90             "ip": TextCliCmd("ip", help_str = "help for ip"),
91             "add": TextCliCmd("add", help_str = "help for add"),
92             "<ip>": IPv4CliCmd(help_str = "An IPv4 address. The format of " +
93                            "an IPv4 address is A.B.C.D, each letter is a number" +
94                            "between 0 and 255."),
95         },
96         cb = ip_callback_add,
97         help_str = "help for command ip"
98     ),
99
100     CmdBuilder(
101         expr = "ip del <saved-ip>",
102         token_desc = {
103             "ip": TextCliCmd("ip", help_str = "help for ip"),
104             "del": TextCliCmd("del", help_str = "help for del"),
105             "<saved-ip>": ChoiceCliCmd(ip_list, help_str = "an IP previously " +
106                                        "added"),
107         },
108         cb = ip_callback_del,
109         help_str = "help for command ip"
110     ),
111
112     CmdBuilder(
113         expr = "ip info <saved-ip>|<ip>", # XXX talk about prio !
114         token_desc = {
115             "ip": TextCliCmd("ip", help_str = "help for ip"),
116             "info": TextCliCmd("info", help_str = "help for info"),
117             "<saved-ip>": ChoiceCliCmd(ip_list, help_str = "an IP previously " +
118                                        "added"),
119             "<ip>": IPv4CliCmd(help_str = "An IPv4 address"),
120         },
121         cb = ip_callback_info,
122         help_str = "help for command ip"
123     ),
124
125     CmdBuilder(
126         expr = "file <file>",
127         token_desc = {
128             "file": TextCliCmd("file", help_str = "Help for file. The file " +
129                            "can be either a directory or a regular file."),
130             "<file>": FileCliCmd(help_str = "a file"),
131         },
132         cb = callback,
133         help_str = "help for command file"
134     ),
135
136     CmdBuilder(
137         expr = "help",
138         token_desc = {
139             "help": TextCliCmd("help", help_str = "help for help"),
140         },
141         cb = help_callback,
142         help_str = "help for command toto"
143     ),
144 ])
145
146 cmdline = pycol.Cmdline()
147 cmdline.add_context("my_context", ctx)
148 cmdline.set_context("my_context")
149 cmdline.set_prompt("my_context> ")
150 cmdline.input_loop()