use optparse
authorTimo Savola <tsavola@movial.fi>
Tue, 29 Apr 2008 16:16:40 +0000 (19:16 +0300)
committerTimo Savola <tsavola@movial.fi>
Tue, 29 Apr 2008 16:17:26 +0000 (19:17 +0300)
matrix/matrix.py

index f534f20..d2d4b27 100644 (file)
@@ -4,6 +4,7 @@
 #          Kalle Vahlman <kalle.vahlman@movial.fi>
 #          Tuomas Kulve <tuomas.kulve@movial.fi>
 
+import optparse
 import os
 import sys
 from sets import Set as set
@@ -20,7 +21,7 @@ Error = RuntimeError
 def main():
        config.parse('config.local')
 
-       command, params, targets = parse_args(sys.argv)
+       command, targets = parse_args()
 
        config.parse('config')
        config.parse('boards')
@@ -46,110 +47,108 @@ def main():
        if not targets:
                targets = all_targets
 
-       if command == 'install':
-               build(targets)
-       elif command == 'install-only':
-               build_only(targets)
-       elif command == 'clone':
-               clone(targets)
-       elif command == 'clean':
-               clean(targets)
-       elif command == 'rebase':
-               rebase(targets)
-       elif command == 'pull':
-               pull(targets)
-       elif command == 'changes':
-               changes(targets)
-       elif command == 'source-dist':
-               source_dist(targets)
-       else:
-               raise Error('Invalid command: ' + command)
-
-def help(file, args):
+       command_funcs = {
+               'install':      build,
+               'install-only': build_only,
+               'clone':        clone,
+               'clean':        clean,
+               'rebase':       rebase,
+               'pull':         pull,
+               'changes':      changes,
+               'source-dist':  source_dist,
+       }
+
+       func = command_funcs.get(command)
+       if not func:
+               raise Error('Invalid command: "%s"' % command)
+
+       func(targets)
+
+def print_help(file, parser):
+       parser.print_help(file=file)
        print >>file, '''
-Copyright (C) 2006-2008 Movial Oy
-
-Usage: %(progname)s [<options>] <command> [<params>] [<components>]
-
-If no components are specified, all of them will be targeted.  All component
-metadata will be downloaded regardless of the specified command and components.
-
-Options:
-       -v              Verbose output.
-       -d              Debug output.
-       -r URL          Specify the root for component git repos to clone from.
-                       If this option is not given, the roots specified in the
-                       components file will be used.  This option may be
-                       specified multiple times.
-       -f              Build (or be quiet about) components with dirty files.
-       -k              Build as much as possible after an error.
-       -j N            Execute N build jobs in parallel.
-       -mj N           Run component builds with make -j N.
-       -h, --help      Print this help text.
-
-Commands and parameters:
-       clone           Download the components' git repositories.
-       install         Download, build and install the components.
-       install-only    Download, build and install the specified components
-                       without dependencies.
-       clean           Remove all non-tracked files from the component git
-                       repository directories.
-       rebase          Update repositories from server by rebasing.
-       pull            Update repositories from server by merging.
-       changes         Show commits which are not on the server.
-       source-dist     Download and package the component sources.
-''' % {'progname': args[0]}
-
-def parse_args(args):
-       def parse_jobs(arg):
-               jobs = int(arg)
-               if jobs <= 0:
-                       raise Error('Please specify a valid number of jobs')
-               return jobs
-
-       command = None
-       params = {}
-       targets = []
-
-       i = 1
-       while i < len(args):
-               if args[i].startswith('-'):
-                       if not command:
-                               if args[i] == '-v':
-                                       config.verbose = True
-                               elif args[i] == '-d':
-                                       config.debug = True
-                               elif args[i] == '-r':
-                                       i += 1
-                                       config.roots.append(args[i])
-                               elif args[i] == '-f':
-                                       config.force = True
-                               elif args[i] == '-k':
-                                       config.keep_going = True
-                               elif args[i] == '-j':
-                                       i += 1
-                                       config.jobs = parse_jobs(args[i])
-                               elif args[i] == '-mj':
-                                       i += 1
-                                       config.make_jobs = parse_jobs(args[i])
-                               elif args[i] in ('-h', '--help'):
-                                       help(sys.stdout, args)
-                                       sys.exit(0)
-                               else:
-                                       raise Error('Bad option: ' + args[i])
-                       else:
-                               raise Error('Command takes no parameters')
-               elif not command:
-                       command = args[i]
-               else:
-                       targets.append(args[i])
-               i += 1
-
-       if not command:
-               help(sys.stderr, args)
+commands:
+  clone         download the components' git repositories
+  install       download, build and install the components
+  install-only  download, build and install the specified components without
+                dependencies
+  clean         remove all non-tracked files from the component git repository
+                directories
+  rebase        update repositories from server by rebasing
+  pull          update repositories from server by merging
+  changes       show commits which are not on the server
+  source-dist   download and package the component sources
+'''
+
+def parse_args():
+       def help(option, opt, value, parser):
+               print_help(sys.stdout, parser)
+               sys.exit(0)
+
+       def add_root(option, opt, value, parser):
+               config.roots.append(value)
+
+       def set_jobs(option, opt, value, parser):
+               if value < 1:
+                       raise optparse.OptionValueError(
+                               'option %s: invalid job count: %d' % \
+                               (opt, value))
+               setattr(config, option.dest, value)
+
+       parser = optparse.OptionParser(
+               usage='%prog [<options>] <command> [<components>]',
+               add_help_option=False)
+
+       parser.add_option(
+               '-h', '--help',
+               help='show this help message and exit',
+               action='callback', callback=help)
+
+       parser.add_option(
+               '-v', '--verbose',
+               help='show build output',
+               dest='verbose', action='store_true')
+
+       parser.add_option(
+               '-d', '--debug',
+               help='debug output',
+               dest='debug', action='store_true')
+
+       parser.add_option(
+               '-r', '--root',
+               help='add a location for finding git repos when cloning ' \
+                    '(may be specified multiple times)', metavar='URL',
+               dest='roots', action='callback', callback=add_root,
+               type='string')
+
+       parser.add_option(
+               '-f', '--force',
+               help='build (or be quiet about) components with dirty files',
+               dest='force', action='store_true')
+
+       parser.add_option(
+               '-k', '--keep-going',
+               help='build as much as possible after an error',
+               dest='keep_going', action='store_true')
+
+       parser.add_option(
+               '-j', '--jobs',
+               help='execute up to N build jobs in parallel', metavar='N',
+               dest='jobs', action='callback', callback=set_jobs,
+               type='int')
+
+       parser.add_option(
+               '-J', '--make-jobs',
+               help='run component builds with make -j N', metavar='N',
+               dest='make_jobs', action='callback', callback=set_jobs,
+               type='int')
+
+       opts, args = parser.parse_args(values=config)
+       if not args:
+               print_help(sys.stderr, parser)
                sys.exit(1)
 
-       return command, params, targets
+       return args[0], args[1:]
 
 def execute(args):
        if config.debug: