Changeset 438
- Timestamp:
- 05/27/07 22:35:06 (2 years ago)
- Files:
-
- cly/trunk/cly/interactive.py (modified) (1 diff)
- cly/trunk/cly/parser.py (modified) (1 diff)
- cly/trunk/doc/developers-guide.rst (modified) (4 diffs)
- cly/trunk/doc/tutorial.rst (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cly/trunk/cly/interactive.py
r434 r438 270 270 *args, **kwargs): 271 271 """Start an interactive session with the given grammar or parser object.""" 272 interact = Interact(grammar , *args, **kwargs)272 interact = Interact(grammar_or_parser, *args, **kwargs) 273 273 interact.loop(inhibit_exceptions=inhibit_exceptions, 274 274 with_backtrace=with_backtrace) cly/trunk/cly/parser.py
r434 r438 12 12 __all__ = ['HelpParser', 'Context', 'Parser'] 13 13 __docformat__ = 'restructuredtext en' 14 15 16 from cly.exceptions import * 14 17 15 18 cly/trunk/doc/developers-guide.rst
r437 r438 1 1 CLY Developer's Guide 2 2 ===================== 3 4 .. contents:: 3 5 4 6 Overview … … 56 58 Types of Nodes 57 59 ~~~~~~~~~~~~~~ 58 CLY includes a whole s etof builtin node types, which can be broken down into60 CLY includes a whole suite of builtin node types, which can be broken down into 59 61 the following groups: 60 62 … … 116 118 117 119 For details on what attributes are available for each node class, refer to the 118 API documentation.120 `API documentation`_. 119 121 120 122 Optional Branches … … 131 133 # add <hostname> <ip> [<comment>] 132 134 add=Node('Add a host')( 133 hostname=Variable('Host name')(134 ip=IP('IP address')(135 action=Action('Add host', add_host),136 # Optional comment137 comment=Variable('Comment', pattern=r'.+')(138 Alias('../../action'),139 )140 )141 ),135 hostname=Variable('Host name')( 136 ip=IP('IP address')( 137 action=Action('Add host', add_host), 138 # Optional comment 139 comment=Variable('Comment', pattern=r'.+')( 140 Alias('../../action'), 141 ) 142 ) 143 ), 142 144 ) 143 145 144 A second use ful situation is aliasing another entire block of the grammar at145 the current location:146 A second use of aliases is importing an entire block of the grammar at the 147 current location. Convenient for allowing multiple optional branches: 146 148 147 149 .. code-block:: python 148 150 151 def add_host(hostname, ip, comment=''): 152 ... 149 153 154 grammar = Grammar( 155 add=Node('Add a host')( 156 hostname=Variable('Host name')( 157 Action('Add host', add_host, valid=lambda context: 'ip' in context.vars), 158 Traversals(0)( 159 ip=Node('IP address', traversals=0)( 160 ip=IP('IP address', traversals=0)( 161 Alias('../../../*') 162 ), 163 ), 164 comment=Node('Comment')( 165 comment=String('Comment')( 166 Alias('../../../*') 167 ), 168 ), 169 ), 170 ), 171 ) 172 173 .. _API documentation: http://swapoff.org/cly/docs cly/trunk/doc/tutorial.rst
r423 r438 13 13 14 14 For the purposes of this tutorial I will implement a basic "shell", with the 15 commands `` ls``, ``cat`` and ``quit``. ``ls`` and ``cat`` will have full tab completion16 of files and directories by the end of the tutorial.15 commands ``cat`` and ``quit``. ``cat`` will have full tab completion of files 16 and directories. 17 17 18 18 Step One - Fundamentals … … 26 26 27 27 from cly import * 28 from cly.interactive import Interact29 30 The second import introduces the Interact_ class. This class requires only one31 argument, the CLY grammar that represents your interface.32 33 The first module imported contains the core API of CLY. Some of the more useful34 classes it includes are ``Context``, ``Node``, ``Alias``, ``Variable``, ``Action``,35 ``Help`` and ``Parser``.36 28 37 29 Step Two - Interaction 38 30 ---------------------- 39 31 40 Making your program interactive is as simple as constructing an ``Interact`` 41 object and calling its ``interact_loop()`` method. 42 43 .. code-block:: python 44 45 from cly import * 46 from cly.interactive import Interact 32 Making your program interactive is as simple as calling the ``interact()`` 33 function with a grammar: 34 35 .. code-block:: python 36 37 from cly import * 47 38 48 39 grammar = Grammar() 49 interact = Interact(grammar) 50 interact.interact_loop() 51 52 This calls ``interact_loop()`` with an empty grammar, which won't do much but 40 interact(grammar) 41 42 This calls ``interact()`` with an empty grammar, which won't do much but 53 43 will actually allow interaction. 54 44 … … 62 52 import sys 63 53 from cly import * 64 from cly.interactive import Interact 65 66 def do_quit(): 67 sys.exit(0) 68 69 70 grammar = Grammar( 71 quit=Node('Quit')( 72 Action('Quit', do_quit), 73 ), 74 ) 75 76 interact = Interact(grammar) 77 interact.interact_loop() 54 55 def do_quit(): 56 sys.exit(0) 57 58 59 grammar = Grammar( 60 quit=Node('Quit')( 61 Action('Quit', do_quit), 62 ), 63 ) 64 65 interact(grammar) 78 66 79 67 **Note:** The apparent redundancy of having two *Quit* nodes is explained … … 105 93 import sys 106 94 from cly import * 107 from cly.interactive import Interact108 95 109 96 def do_quit(): … … 124 111 ) 125 112 126 interact = Interact(grammar) 127 interact.interact_loop() 113 interact(grammar) 128 114 129 115 Step Five - Recursion … … 137 123 import sys 138 124 from cly import * 139 from cly.interactive import Interact140 125 141 126 def do_quit(): … … 158 143 ) 159 144 160 interact = Interact(grammar) 161 interact.interact_loop() 145 interact(grammar) 162 146 163 147 Each ``Node`` has a ``traversals`` member which specifies the number of times that … … 183 167 import sys 184 168 from cly import * 185 from cly.variables import File186 from cly.interactive import Interact187 169 188 170 def do_quit(): … … 205 187 ) 206 188 207 interact = Interact(grammar) 208 interact.interact_loop() 189 interact(grammar) 209 190 210 191 Variables can be used to not only validate input, but parse it into a useful … … 220 201 behaviour of your command line applications. 221 202 222 .. _Interact: http://swapoff.org/cly/docs/cly.interactive223 203 .. _other features: http://swapoff.org/cly/docs
