opster
view opster.py @ 101:7507884b8dff
Fix help for options with functions as their default arguments
| author | Alexander Solovyov <piranha@piranha.org.ua> |
|---|---|
| date | Thu Feb 11 18:48:44 2010 +0200 (5 months ago) |
| parents | a77c4a82e836 |
| children |
line source
1 # (c) Alexander Solovyov, 2009, under terms of the new BSD License
2 '''Command line arguments parser
3 '''
18 # --------
19 # Public interface
20 # --------
23 '''Decorator to mark function to be used for command line processing.
25 All arguments are optional:
27 - ``options``: options in format described in docs. If not supplied,
28 will be determined from function.
29 - ``usage``: usage string for function, replaces ``%name`` with name
30 of program or subcommand. In case if it's subcommand and ``%name``
31 is not present, usage is prepended by ``name``
32 - ``name``: used for multiple subcommands. Defaults to wrapped
33 function name
34 - ``shortlist``: if command should be included in shortlist. Used
35 only with multiple subcommands
36 - ``hide``: if command should be hidden from help listing. Used only
37 with multiple subcommands, overrides ``shortlist``
38 '''
47 pass
62 # look if we need to add 'help' option
74 # no catcher here because this is call from Python
96 '''Dispatch command arguments based on subcommands.
98 - ``args``: list of arguments, default: ``sys.argv[1:]``
99 - ``cmdtable``: dict of commands in format described below.
100 If not supplied, will use functions decorated with ``@command``.
101 - ``globaloptions``: list of options which are applied to all
102 commands, will contain ``--help`` option at least.
103 - ``middleware``: global decorator for all commands.
105 cmdtable format description::
107 {'name': (function, options, usage)}
109 - ``name`` is the name used on command-line. Can contain aliases
110 (separate them with ``|``), pointer to a fact that this command
111 should be displayed in short help (start name with ``^``), or to
112 a fact that this command should be hidden (start name with ``~``)
113 - ``function`` is the actual callable
114 - ``options`` is options list in format described in docs
115 - ``usage`` is the short string of usage
116 '''
136 # --------
137 # Help
138 # --------
142 '''Show help for a given help topic or a help overview
144 With no arguments, print a list of commands with short help messages.
146 Given a command name, print help for that command.
147 '''
150 # determine if any command is marked for shortlist
189 '''show help for given command
191 - ``func``: function to generate help for (``func.__doc__`` is taken)
192 - ``usage``: usage string
193 - ``options``: options in usual format
195 >>> def test(*args, **opts):
196 ... """that's a test command
197 ...
198 ... you can do nothing with this command"""
199 ... pass
200 >>> opts = [('l', 'listen', 'localhost',
201 ... 'ip to listen on'),
202 ... ('p', 'port', 8000,
203 ... 'port to listen on'),
204 ... ('d', 'daemonize', False,
205 ... 'daemonize process'),
206 ... ('', 'pid-file', '',
207 ... 'name of file to write process ID to')]
208 >>> help_cmd(test, 'test [-l HOST] [NAME]', opts)
209 test [-l HOST] [NAME]
210 <BLANKLINE>
211 that's a test command
212 <BLANKLINE>
213 you can do nothing with this command
214 <BLANKLINE>
215 options:
216 <BLANKLINE>
217 -l --listen ip to listen on (default: localhost)
218 -p --port port to listen on (default: 8000)
219 -d --daemonize daemonize process
220 --pid-file name of file to write process ID to
221 <BLANKLINE>
222 '''
245 # wrap description at 78 chars
253 # --------
254 # Options parsing
255 # --------
258 '''
259 >>> opts = [('l', 'listen', 'localhost',
260 ... 'ip to listen on'),
261 ... ('p', 'port', 8000,
262 ... 'port to listen on'),
263 ... ('d', 'daemonize', False,
264 ... 'daemonize process'),
265 ... ('', 'pid-file', '',
266 ... 'name of file to write process ID to')]
267 >>> print parse(['-l', '0.0.0.0', '--pi', 'test', 'all'], opts)
268 ({'pid_file': 'test', 'daemonize': False, 'port': 8000, 'listen': '0.0.0.0'}, ['all'])
270 '''
281 # change name to match Python styling
286 # copy defaults to state
295 # getopt wants indication that it takes a parameter
306 # transfer result to state
328 # --------
329 # Subcommand system
330 # --------
343 # command is the first non-option
348 break
369 """
370 Return cmd -> (aliases, command table entry)
371 for each matching command.
372 """
383 break
390 """Return (aliases, command table entry) for command string."""
406 # --------
407 # Helpers
408 # --------
417 pass
434 '''Catches all exceptions and prints human-readable information on them
435 '''
454 raise
457 raise
468 raise
510 # --------
511 # Exceptions
512 # --------
514 # Command exceptions
516 'Base class for command exceptions'
519 'Raised if command is ambiguous'
522 'Raised if command is unknown'
525 'Raised on error in command line parsing'
528 'Abort execution'
531 'Raised on trouble with opster configuration'
