Prettify error messages for invalid component names
[matrix.git] / matrix / matrix.py
index 7814d49..4bf796a 100644 (file)
@@ -24,8 +24,8 @@ def merge_config(name, value):
                value = getattr(config, name, []) + value
        setattr(config, name, value)
 
-def main():
-       command, targets, options = parse_args()
+def main(version):
+       command, targets, options = parse_args(version)
 
        for name in ('debug', 'initial_config_dirs', 'pull_config_dirs'):
                if name in options:
@@ -89,13 +89,17 @@ commands:
   source-dist   download and package the component sources
 '''
 
-def parse_args():
+def parse_args(matrix_version):
        options = {}
 
        def help(option, opt, value, parser):
                print_help(sys.stdout, parser)
                sys.exit(0)
 
+       def version(option, opt, value, parser):
+               print matrix_version
+               sys.exit(0)
+
        def set_flag(option, opt, value, parser):
                options[option.dest] = True
 
@@ -122,6 +126,11 @@ def parse_args():
                action='callback', callback=help)
 
        parser.add_option(
+               '--version',
+               help='print Matrix version',
+               action='callback', callback=version)
+
+       parser.add_option(
                '-v', '--verbose',
                help='show build output',
                dest='verbose', action='callback', callback=set_flag)
@@ -189,15 +198,21 @@ def execute(args):
 def remove_tree(path):
        execute(['rm', '-rf', path])
 
+def find_component(name):
+       try:
+               return components.by_name[name]
+       except KeyError:
+               raise Error("Component " + name + " not found");
+
 def meta(targets):
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                if not c.meta.exists():
                        c.meta.clone()
        
 def clone(targets):
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                clone_component(c)
 
 def clone_component(c, overwrite=False):
@@ -223,7 +238,7 @@ def clean(targets):
        changed = False
 
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                c.source.clean()
                cache.remove(c)
 
@@ -237,7 +252,7 @@ def for_each_repository(func, targets=None):
        ret = None
 
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                if c.source.exists():
                        value = func(c.source)
                        if value:
@@ -268,7 +283,7 @@ def changes(targets):
        changed = False
 
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
 
                if c.source.exists():
                        if check_changes(c.source):
@@ -301,7 +316,7 @@ def source_dist(targets):
                os.makedirs(location)
 
        for name in targets:
-               c = components.by_name[name]
+               c = find_component(name)
                dist_changes(c, location)
                dist_sources(c, location)