8 sys.path.insert(0, "@abs_top_srcdir@/ovsdb")
9 import simplejson as json
13 class Error(Exception):
14 def __init__(self, msg):
15 Exception.__init__(self)
18 def getMember(json, name, validTypes, description, default=None):
21 if type(member) not in validTypes:
22 raise Error("%s: type mismatch for '%s' member"
23 % (description, name))
27 def mustGetMember(json, name, expectedType, description):
28 member = getMember(json, name, expectedType, description)
30 raise Error("%s: missing '%s' member" % (description, name))
34 def __init__(self, name, comment, tables, idlPrefix, idlHeader):
36 self.comment = comment
38 self.idlPrefix = idlPrefix
39 self.idlHeader = idlHeader
43 name = mustGetMember(json, 'name', [unicode], 'database')
44 comment = getMember(json, 'comment', [unicode], 'database')
45 tablesJson = mustGetMember(json, 'tables', [dict], 'database')
47 for tableName, tableJson in tablesJson.iteritems():
48 tables[tableName] = TableSchema.fromJson(tableJson,
49 "%s table" % tableName)
50 idlPrefix = mustGetMember(json, 'idlPrefix', [unicode], 'database')
51 idlHeader = mustGetMember(json, 'idlHeader', [unicode], 'database')
52 return DbSchema(name, comment, tables, idlPrefix, idlHeader)
55 def __init__(self, comment, columns):
56 self.comment = comment
57 self.columns = columns
60 def fromJson(json, description):
61 comment = getMember(json, 'comment', [unicode], description)
62 columnsJson = mustGetMember(json, 'columns', [dict], description)
64 for name, json in columnsJson.iteritems():
65 columns[name] = ColumnSchema.fromJson(
66 json, "column %s in %s" % (name, description))
67 return TableSchema(comment, columns)
70 def __init__(self, comment, type, persistent):
71 self.comment = comment
73 self.persistent = persistent
76 def fromJson(json, description):
77 comment = getMember(json, 'comment', [unicode], description)
78 type = Type.fromJson(mustGetMember(json, 'type', [dict, unicode],
80 'type of %s' % description)
81 ephemeral = getMember(json, 'ephemeral', [bool], description)
82 persistent = ephemeral != True
83 return ColumnSchema(comment, type, persistent)
85 def escapeCString(src):
106 dst += '\\%03o' % ord(c)
112 def __init__(self, type, refTable=None, minInteger=None, maxInteger=None,
113 minReal=None, maxReal=None, reMatch=None, reComment=None,
114 minLength=None, maxLength=None):
116 self.refTable = refTable
117 self.minInteger = minInteger
118 self.maxInteger = maxInteger
119 self.minReal = minReal
120 self.maxReal = maxReal
121 self.reMatch = reMatch
122 self.reComment = reComment
123 self.minLength = minLength
124 self.maxLength = maxLength
127 def fromJson(json, description):
128 if type(json) == unicode:
129 return BaseType(json)
131 atomicType = mustGetMember(json, 'type', [unicode], description)
132 refTable = getMember(json, 'refTable', [unicode], description)
133 minInteger = getMember(json, 'minInteger', [int, long], description)
134 maxInteger = getMember(json, 'maxInteger', [int, long], description)
135 minReal = getMember(json, 'minReal', [int, long, float], description)
136 maxReal = getMember(json, 'maxReal', [int, long, float], description)
137 reMatch = getMember(json, 'reMatch', [unicode], description)
138 reComment = getMember(json, 'reComment', [unicode], description)
139 minLength = getMember(json, 'minLength', [int], description)
140 maxLength = getMember(json, 'minLength', [int], description)
141 return BaseType(atomicType, refTable, minInteger, maxInteger, minReal, maxReal, reMatch, reComment, minLength, maxLength)
144 if self.type == 'uuid' and self.refTable:
149 def toCType(self, prefix):
151 return "struct %s%s *" % (prefix, self.refTable.lower())
153 return {'integer': 'int64_t ',
155 'uuid': 'struct uuid ',
157 'string': 'char *'}[self.type]
159 def copyCValue(self, dst, src):
160 args = {'dst': dst, 'src': src}
162 return ("%(dst)s = %(src)s->header_.uuid;") % args
163 elif self.type == 'string':
164 return "%(dst)s = xstrdup(%(src)s);" % args
166 return "%(dst)s = %(src)s;" % args
168 def initCDefault(self, var, isOptional):
170 return "%s = NULL;" % var
171 elif self.type == 'string' and not isOptional:
172 return "%s = \"\";" % var
174 return {'integer': '%s = 0;',
176 'uuid': 'uuid_zero(&%s);',
177 'boolean': '%s = false;',
178 'string': '%s = NULL;'}[self.type] % var
180 def cInitBaseType(self, indent, var):
182 stmts.append('ovsdb_base_type_init(&%s, OVSDB_TYPE_%s);' % (
183 var, self.type.upper()),)
184 if self.type == 'integer':
185 if self.minInteger != None:
186 stmts.append('%s.u.integer.min = %d;' % (var, self.minInteger))
187 if self.maxInteger != None:
188 stmts.append('%s.u.integer.max = %d;' % (var, self.maxInteger))
189 elif self.type == 'real':
190 if self.minReal != None:
191 stmts.append('%s.u.real.min = %d;' % (var, self.minReal))
192 if self.maxReal != None:
193 stmts.append('%s.u.real.max = %d;' % (var, self.maxReal))
194 elif self.type == 'string':
195 if self.reMatch != None:
196 if self.reComment != None:
197 reComment = '"%s"' % escapeCString(self.reComment)
200 stmts.append('do_set_regex(&%s, "%s", %s);' % (
201 var, escapeCString(self.reMatch), reComment))
202 if self.minLength != None:
203 stmts.append('%s.u.string.minLen = %d;' % (var, self.minLength))
204 if self.maxLength != None:
205 stmts.append('%s.u.string.maxLen = %d;' % (var, self.maxLength))
206 elif self.type == 'uuid':
207 if self.refTable != None:
208 stmts.append('%s.u.uuid.refTableName = "%s";' % (var, escapeCString(self.refTable)))
209 return '\n'.join([indent + stmt for stmt in stmts])
212 def __init__(self, key, value=None, min=1, max=1):
219 def fromJson(json, description):
220 if type(json) == unicode:
221 return Type(BaseType(json))
223 keyJson = mustGetMember(json, 'key', [dict, unicode], description)
224 key = BaseType.fromJson(keyJson, 'key in %s' % description)
226 valueJson = getMember(json, 'value', [dict, unicode], description)
228 value = BaseType.fromJson(valueJson,
229 'value in %s' % description)
233 min = getMember(json, 'min', [int], description, 1)
234 max = getMember(json, 'max', [int, unicode], description, 1)
235 return Type(key, value, min, max)
238 return self.min == 1 and self.max == 1 and not self.value
240 def isOptional(self):
241 return self.min == 0 and self.max == 1
243 def isOptionalPointer(self):
244 return (self.min == 0 and self.max == 1 and not self.value
245 and (self.key.type == 'string' or self.key.refTable))
248 keyName = self.key.toEnglish()
250 valueName = self.value.toEnglish()
254 elif self.isOptional():
256 return "optional %s-%s pair" % (keyName, valueName)
258 return "optional %s" % keyName
260 if self.max == "unlimited":
262 quantity = "%d or more " % self.min
266 quantity = "%d to %d " % (self.min, self.max)
268 quantity = "up to %d " % self.max
271 return "map of %s%s-%s pairs" % (quantity, keyName, valueName)
273 return "set of %s%s" % (quantity, keyName)
275 def cDeclComment(self):
276 if self.min == 1 and self.max == 1 and self.key.type == "string":
277 return "\t/* Always nonnull. */"
281 def cInitType(self, indent, var):
282 initKey = self.key.cInitBaseType(indent, "%s.key" % var)
284 initValue = self.value.cInitBaseType(indent, "%s.value" % var)
286 initValue = ('%sovsdb_base_type_init(&%s.value, '
287 'OVSDB_TYPE_VOID);' % (indent, var))
288 initMin = "%s%s.n_min = %s;" % (indent, var, self.min)
289 if self.max == "unlimited":
293 initMax = "%s%s.n_max = %s;" % (indent, var, max)
294 return "\n".join((initKey, initValue, initMin, initMax))
296 def parseSchema(filename):
297 return DbSchema.fromJson(json.load(open(filename, "r")))
299 def annotateSchema(schemaFile, annotationFile):
300 schemaJson = json.load(open(schemaFile, "r"))
301 execfile(annotationFile, globals(), {"s": schemaJson})
302 json.dump(schemaJson, sys.stdout)
304 def constify(cType, const):
306 and cType.endswith('*') and not cType.endswith('**')
307 and (cType.startswith('struct uuid') or cType.startswith('char'))):
308 return 'const %s' % cType
312 def cMembers(prefix, columnName, column, const):
314 if type.min == 1 and type.max == 1:
319 if type.isOptionalPointer():
325 key = {'name': "key_%s" % columnName,
326 'type': constify(type.key.toCType(prefix) + pointer, const),
328 value = {'name': "value_%s" % columnName,
329 'type': constify(type.value.toCType(prefix) + pointer, const),
331 members = [key, value]
333 m = {'name': columnName,
334 'type': constify(type.key.toCType(prefix) + pointer, const),
335 'comment': type.cDeclComment()}
338 if not singleton and not type.isOptionalPointer():
339 members.append({'name': 'n_%s' % columnName,
344 def printCIDLHeader(schemaFile):
345 schema = parseSchema(schemaFile)
346 prefix = schema.idlPrefix
348 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
350 #ifndef %(prefix)sIDL_HEADER
351 #define %(prefix)sIDL_HEADER 1
356 #include "ovsdb-idl-provider.h"
357 #include "uuid.h"''' % {'prefix': prefix.upper()}
359 for tableName, table in sorted(schema.tables.iteritems()):
360 structName = "%s%s" % (prefix, tableName.lower())
363 print "/* %s table. */" % tableName
364 print "struct %s {" % structName
365 print "\tstruct ovsdb_idl_row header_;"
366 for columnName, column in sorted(table.columns.iteritems()):
367 print "\n\t/* %s column. */" % columnName
368 for member in cMembers(prefix, columnName, column, False):
369 print "\t%(type)s%(name)s;%(comment)s" % member
373 printEnum(["%s_COL_%s" % (structName.upper(), columnName.upper())
374 for columnName in sorted(table.columns)]
375 + ["%s_N_COLUMNS" % structName.upper()])
378 for columnName in table.columns:
379 print "#define %(s)s_col_%(c)s (%(s)s_columns[%(S)s_COL_%(C)s])" % {
381 'S': structName.upper(),
383 'C': columnName.upper()}
385 print "\nextern struct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (structName, structName.upper())
388 const struct %(s)s *%(s)s_first(const struct ovsdb_idl *);
389 const struct %(s)s *%(s)s_next(const struct %(s)s *);
390 #define %(S)s_FOR_EACH(ROW, IDL) for ((ROW) = %(s)s_first(IDL); (ROW); (ROW) = %(s)s_next(ROW))
392 void %(s)s_delete(const struct %(s)s *);
393 struct %(s)s *%(s)s_insert(struct ovsdb_idl_txn *);
394 ''' % {'s': structName, 'S': structName.upper()}
396 for columnName, column in sorted(table.columns.iteritems()):
397 print 'void %(s)s_verify_%(c)s(const struct %(s)s *);' % {'s': structName, 'c': columnName}
400 for columnName, column in sorted(table.columns.iteritems()):
402 print 'void %(s)s_set_%(c)s(const struct %(s)s *,' % {'s': structName, 'c': columnName},
403 args = ['%(type)s%(name)s' % member for member
404 in cMembers(prefix, columnName, column, True)]
405 print '%s);' % ', '.join(args)
408 printEnum(["%sTABLE_%s" % (prefix.upper(), tableName.upper()) for tableName in sorted(schema.tables)] + ["%sN_TABLES" % prefix.upper()])
410 for tableName in schema.tables:
411 print "#define %(p)stable_%(t)s (%(p)stable_classes[%(P)sTABLE_%(T)s])" % {
414 't': tableName.lower(),
415 'T': tableName.upper()}
416 print "\nextern struct ovsdb_idl_table_class %stable_classes[%sN_TABLES];" % (prefix, prefix.upper())
418 print "\nextern struct ovsdb_idl_class %sidl_class;" % prefix
419 print "\nvoid %sinit(void);" % prefix
420 print "\n#endif /* %(prefix)sIDL_HEADER */" % {'prefix': prefix.upper()}
422 def printEnum(members):
423 if len(members) == 0:
427 for member in members[:-1]:
428 print " %s," % member
429 print " %s" % members[-1]
432 def printCIDLSource(schemaFile):
433 schema = parseSchema(schemaFile)
434 prefix = schema.idlPrefix
436 /* Generated automatically -- do not modify! -*- buffer-read-only: t -*- */
442 #include "ovsdb-data.h"
443 #include "ovsdb-error.h"
448 do_set_regex(struct ovsdb_base_type *base, const char *reMatch,
449 const char *reComment)
451 struct ovsdb_error *error;
453 error = ovsdb_base_type_set_regex(base, reMatch, reComment);
455 char *s = ovsdb_error_to_string(error);
456 ovs_error(0, "%%s", s);
458 ovsdb_error_destroy(error);
460 }''' % schema.idlHeader
463 for tableName, table in sorted(schema.tables.iteritems()):
464 structName = "%s%s" % (prefix, tableName.lower())
466 static struct %(s)s *
467 %(s)s_cast(const struct ovsdb_idl_row *row)
469 return row ? CONTAINER_OF(row, struct %(s)s, header_) : NULL;
471 ''' % {'s': structName}
474 for tableName, table in sorted(schema.tables.iteritems()):
475 structName = "%s%s" % (prefix, tableName.lower())
477 if table.comment != None:
478 print "/* %s table (%s). */" % (tableName, table.comment)
480 print "/* %s table. */" % (tableName)
483 for columnName, column in sorted(table.columns.iteritems()):
486 %(s)s_parse_%(c)s(struct ovsdb_idl_row *row_, const struct ovsdb_datum *datum)
488 struct %(s)s *row = %(s)s_cast(row_);''' % {'s': structName,
493 keyVar = "row->key_%s" % columnName
494 valueVar = "row->value_%s" % columnName
496 keyVar = "row->%s" % columnName
499 if (type.min == 1 and type.max == 1) or type.isOptionalPointer():
501 print " assert(inited);"
502 print " if (datum->n >= 1) {"
503 if not type.key.refTable:
504 print " %s = datum->keys[0].%s;" % (keyVar, type.key.type)
506 print " %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[0].uuid));" % (keyVar, prefix, type.key.refTable.lower(), prefix, prefix.upper(), type.key.refTable.upper())
509 if type.value.refTable:
510 print " %s = datum->values[0].%s;" % (valueVar, type.value.type)
512 print " %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[0].uuid));" % (valueVar, prefix, type.value.refTable.lower(), prefix, prefix.upper(), type.value.refTable.upper())
514 print " %s" % type.key.initCDefault(keyVar, type.min == 0)
516 print " %s" % type.value.initCDefault(valueVar, type.min == 0)
519 if type.max != 'unlimited':
520 print " size_t n = MIN(%d, datum->n);" % type.max
526 print " assert(inited);"
527 print " %s = NULL;" % keyVar
529 print " %s = NULL;" % valueVar
530 print " row->n_%s = 0;" % columnName
531 print " for (i = 0; i < %s; i++) {" % nMax
533 if type.key.refTable:
534 print " struct %s%s *keyRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->keys[i].uuid));" % (prefix, type.key.refTable.lower(), prefix, type.key.refTable.lower(), prefix, prefix.upper(), type.key.refTable.upper())
536 refs.append('keyRow')
538 keySrc = "datum->keys[i].%s" % type.key.type
539 if type.value and type.value.refTable:
540 print " struct %s%s *valueRow = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_classes[%sTABLE_%s], &datum->values[i].uuid));" % (prefix, type.value.refTable.lower(), prefix, type.value.refTable.lower(), prefix, prefix.upper(), type.value.refTable.upper())
541 valueSrc = "valueRow"
542 refs.append('valueRow')
544 valueSrc = "datum->values[i].%s" % type.value.type
546 print " if (%s) {" % ' && '.join(refs)
550 print "%sif (!row->n_%s) {" % (indent, columnName)
551 print "%s %s = xmalloc(%s * sizeof *%s);" % (indent, keyVar, nMax, keyVar)
553 print "%s %s = xmalloc(%s * sizeof %s);" % (indent, valueVar, nMax, valueVar)
555 print "%s%s[row->n_%s] = %s;" % (indent, keyVar, columnName, keySrc)
557 print "%s%s[row->n_%s] = %s;" % (indent, valueVar, columnName, valueSrc)
558 print "%srow->n_%s++;" % (indent, columnName)
565 for columnName, column in sorted(table.columns.iteritems()):
567 if (type.min != 1 or type.max != 1) and not type.isOptionalPointer():
570 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row_)
572 struct %(s)s *row = %(s)s_cast(row_);
574 assert(inited);''' % {'s': structName, 'c': columnName}
576 keyVar = "row->key_%s" % columnName
577 valueVar = "row->value_%s" % columnName
579 keyVar = "row->%s" % columnName
581 print " free(%s);" % keyVar
583 print " free(%s);" % valueVar
588 %(s)s_unparse_%(c)s(struct ovsdb_idl_row *row UNUSED)
591 }''' % {'s': structName, 'c': columnName}
593 # First, next functions.
596 %(s)s_first(const struct ovsdb_idl *idl)
598 return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
602 %(s)s_next(const struct %(s)s *row)
604 return %(s)s_cast(ovsdb_idl_next_row(&row->header_));
605 }''' % {'s': structName,
608 'T': tableName.upper()}
612 %(s)s_delete(const struct %(s)s *row)
614 ovsdb_idl_txn_delete(&row->header_);
618 %(s)s_insert(struct ovsdb_idl_txn *txn)
620 return %(s)s_cast(ovsdb_idl_txn_insert(txn, &%(p)stable_classes[%(P)sTABLE_%(T)s]));
622 ''' % {'s': structName,
625 'T': tableName.upper()}
628 for columnName, column in sorted(table.columns.iteritems()):
631 %(s)s_verify_%(c)s(const struct %(s)s *row)
634 ovsdb_idl_txn_verify(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s]);
635 }''' % {'s': structName,
636 'S': structName.upper(),
638 'C': columnName.upper()}
641 for columnName, column in sorted(table.columns.iteritems()):
644 members = cMembers(prefix, columnName, column, True)
645 keyVar = members[0]['name']
649 valueVar = members[1]['name']
651 nVar = members[2]['name']
654 nVar = members[1]['name']
655 print '%(s)s_set_%(c)s(const struct %(s)s *row, %(args)s)' % \
656 {'s': structName, 'c': columnName,
657 'args': ', '.join(['%(type)s%(name)s' % m for m in members])}
659 print " struct ovsdb_datum datum;"
660 if type.min == 1 and type.max == 1:
662 print " assert(inited);"
663 print " datum.n = 1;"
664 print " datum.keys = xmalloc(sizeof *datum.keys);"
665 print " " + type.key.copyCValue("datum.keys[0].%s" % type.key.type, keyVar)
667 print " datum.values = xmalloc(sizeof *datum.values);"
668 print " "+ type.value.copyCValue("datum.values[0].%s" % type.value.type, valueVar)
670 print " datum.values = NULL;"
671 elif type.isOptionalPointer():
673 print " assert(inited);"
674 print " if (%s) {" % keyVar
675 print " datum.n = 1;"
676 print " datum.keys = xmalloc(sizeof *datum.keys);"
677 print " " + type.key.copyCValue("datum.keys[0].%s" % type.key.type, keyVar)
679 print " datum.n = 0;"
680 print " datum.keys = NULL;"
682 print " datum.values = NULL;"
686 print " assert(inited);"
687 print " datum.n = %s;" % nVar
688 print " datum.keys = xmalloc(%s * sizeof *datum.keys);" % nVar
690 print " datum.values = xmalloc(%s * sizeof *datum.values);" % nVar
692 print " datum.values = NULL;"
693 print " for (i = 0; i < %s; i++) {" % nVar
694 print " " + type.key.copyCValue("datum.keys[i].%s" % type.key.type, "%s[i]" % keyVar)
696 print " " + type.value.copyCValue("datum.values[i].%s" % type.value.type, "%s[i]" % valueVar)
698 print " ovsdb_idl_txn_write(&row->header_, &%(s)s_columns[%(S)s_COL_%(C)s], &datum);" \
700 'S': structName.upper(),
701 'C': columnName.upper()}
705 print "\nstruct ovsdb_idl_column %s_columns[%s_N_COLUMNS];" % (
706 structName, structName.upper())
708 static void\n%s_columns_init(void)
710 struct ovsdb_idl_column *c;\
712 for columnName, column in sorted(table.columns.iteritems()):
713 cs = "%s_col_%s" % (structName, columnName)
714 d = {'cs': cs, 'c': columnName, 's': structName}
716 print " /* Initialize %(cs)s. */" % d
717 print " c = &%(cs)s;" % d
718 print " c->name = \"%(c)s\";" % d
719 print column.type.cInitType(" ", "c->type")
720 print " c->parse = %(s)s_parse_%(c)s;" % d
721 print " c->unparse = %(s)s_unparse_%(c)s;" % d
726 print "struct ovsdb_idl_table_class %stable_classes[%sN_TABLES] = {" % (prefix, prefix.upper())
727 for tableName, table in sorted(schema.tables.iteritems()):
728 structName = "%s%s" % (prefix, tableName.lower())
729 print " {\"%s\"," % tableName
730 print " %s_columns, ARRAY_SIZE(%s_columns)," % (
731 structName, structName)
732 print " sizeof(struct %s)}," % structName
736 print "\nstruct ovsdb_idl_class %sidl_class = {" % prefix
737 print " \"%s\", %stable_classes, ARRAY_SIZE(%stable_classes)" % (
738 schema.name, prefix, prefix)
741 # global init function
751 for tableName, table in sorted(schema.tables.iteritems()):
752 structName = "%s%s" % (prefix, tableName.lower())
753 print " %s_columns_init();" % structName
756 def ovsdb_escape(string):
760 raise Error("strings may not contain null bytes")
774 return '\\x%02x' % ord(c)
775 return re.sub(r'["\\\000-\037]', escape, string)
777 def printDoc(schemaFile):
778 schema = parseSchema(schemaFile)
783 for tableName, table in sorted(schema.tables.iteritems()):
784 title = "%s table" % tableName
787 print '-' * len(title)
791 for columnName, column in sorted(table.columns.iteritems()):
793 print "%s (%s)" % (columnName, column.type.toEnglish())
795 print "\t%s" % column.comment
799 %(argv0)s: ovsdb schema compiler
800 usage: %(argv0)s [OPTIONS] COMMAND ARG...
802 The following commands are supported:
803 annotate SCHEMA ANNOTATIONS print SCHEMA combined with ANNOTATIONS
804 c-idl-header IDL print C header file for IDL
805 c-idl-source IDL print C source file for IDL implementation
806 doc IDL print schema documentation
808 The following options are also available:
809 -h, --help display this help message
810 -V, --version display version information\
811 """ % {'argv0': argv0}
814 if __name__ == "__main__":
817 options, args = getopt.gnu_getopt(sys.argv[1:], 'C:hV',
821 except getopt.GetoptError, geo:
822 sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
825 for key, value in options:
826 if key in ['-h', '--help']:
828 elif key in ['-V', '--version']:
829 print "ovsdb-idlc (Open vSwitch) @VERSION@"
830 elif key in ['-C', '--directory']:
835 optKeys = [key for key, value in options]
838 sys.stderr.write("%s: missing command argument "
839 "(use --help for help)\n" % argv0)
842 commands = {"annotate": (annotateSchema, 2),
843 "c-idl-header": (printCIDLHeader, 1),
844 "c-idl-source": (printCIDLSource, 1),
845 "doc": (printDoc, 1)}
847 if not args[0] in commands:
848 sys.stderr.write("%s: unknown command \"%s\" "
849 "(use --help for help)\n" % (argv0, args[0]))
852 func, n_args = commands[args[0]]
853 if len(args) - 1 != n_args:
854 sys.stderr.write("%s: \"%s\" requires %d arguments but %d "
856 % (argv0, args[0], n_args, len(args) - 1))
861 sys.stderr.write("%s: %s\n" % (argv0, e.msg))