comparison profilemanager/command.py @ 79:145e111903d2 default tip

add MPL license
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 May 2010 13:11:38 -0700
parents 18f16bd1ba6b
children
comparison
equal deleted inserted replaced
78:553f5783999f 79:145e111903d2
1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 #
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
8 #
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
12 # License.
13 #
14 # The Original Code is mozilla.org code.
15 #
16 # The Initial Developer of the Original Code is
17 # Mozilla.org.
18 # Portions created by the Initial Developer are Copyright (C) 2010
19 # the Initial Developer. All Rights Reserved.
20 #
21 # Contributor(s):
22 # Jeff Hammel <jhammel@mozilla.com> (Original author)
23 #
24 # Alternatively, the contents of this file may be used under the terms of
25 # either of the GNU General Public License Version 2 or later (the "GPL"),
26 # or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 # in which case the provisions of the GPL or the LGPL are applicable instead
28 # of those above. If you wish to allow use of your version of this file only
29 # under the terms of either the GPL or the LGPL, and not to allow others to
30 # use your version of this file under the terms of the MPL, indicate your
31 # decision by deleting the provisions above and replace them with the notice
32 # and other provisions required by the GPL or the LGPL. If you do not delete
33 # the provisions above, a recipient may use your version of this file under
34 # the terms of any one of the MPL, the GPL or the LGPL.
35 #
36 # ***** END LICENSE BLOCK *****
1 """ 37 """
2 a command-line interface to the command line, a la pythonpaste 38 a command-line interface to the command line, a la pythonpaste
3 """ 39 """
4 40
5 import inspect 41 import inspect
7 from optparse import OptionParser 43 from optparse import OptionParser
8 from pprint import pprint 44 from pprint import pprint
9 45
10 if 'commands' not in globals(): 46 if 'commands' not in globals():
11 commands = {} 47 commands = {}
12 48
13 def command(function): 49 def command(function):
14 # XXX should get bound/unbound state from function (how?) 50 # XXX should get bound/unbound state from function (how?)
15 global commands 51 global commands
16 name = function.func_name 52 name = function.func_name
17 doc = inspect.cleandoc(function.__doc__) 53 doc = inspect.cleandoc(function.__doc__)
22 optional = dict(zip(argspec.args[-len(defaults):], defaults)) 58 optional = dict(zip(argspec.args[-len(defaults):], defaults))
23 else: 59 else:
24 args = argspec.args[1:] 60 args = argspec.args[1:]
25 optional = None 61 optional = None
26 commands[name] = { 'doc': doc, 62 commands[name] = { 'doc': doc,
27 'args': args, 63 'args': args,
28 'optional': optional, 64 'optional': optional,
29 'varargs': argspec.varargs 65 'varargs': argspec.varargs
30 } 66 }
31 return function 67 return function
32 68
88 elif value is False: 124 elif value is False:
89 parser.add_option('--%s' % key, action='store_true', 125 parser.add_option('--%s' % key, action='store_true',
90 default=False, help=help) 126 default=False, help=help)
91 else: 127 else:
92 parser.add_option('--%s' % key, help=help) 128 parser.add_option('--%s' % key, help=help)
93 129
94 return parser 130 return parser
95 131
96 class CommandParser(OptionParser): 132 class CommandParser(OptionParser):
97 def __init__(self, commands, description=None, setup=None): 133 def __init__(self, commands, description=None, setup=None):
98 usage = '%prog [options] command [command-options]' 134 usage = '%prog [options] command [command-options]'
115 print 151 print
116 print description 152 print description
117 153
118 def parse(self, args=sys.argv[1:]): 154 def parse(self, args=sys.argv[1:]):
119 """global parse step""" 155 """global parse step"""
120 156
121 self.options, args = self.parse_args(args) 157 self.options, args = self.parse_args(args)
122 158
123 # help/sanity check -- should probably be separated 159 # help/sanity check -- should probably be separated
124 if not len(args): 160 if not len(args):
125 self.print_help() 161 self.print_help()