Skip to content

Commit

Permalink
- osc.cli.description: warn if a command tries to redefine an existin…
Browse files Browse the repository at this point in the history
…g command

The new definition is ignored. A (sub)command can be replaced by inheriting from the
(sub)command.
  • Loading branch information
marcus-h committed Oct 12, 2012
1 parent e3e424c commit c508549
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions osc/cli/description.py
Expand Up @@ -13,6 +13,7 @@

import textwrap
import inspect
import logging
import re

import argparse
Expand All @@ -30,6 +31,11 @@ def commands():
return commands.subcmds


def logger():
"""Returns a logging.Logger object."""
return logging.getLogger(__name__)


class SubcommandFilterMeta(type):
"""Determine the "parent" command for a CommandDescription subclass."""

Expand Down Expand Up @@ -284,7 +290,17 @@ def _add_subcommands(cls, parser):
subcmds = commands().get(cls.__name__, [])
if subcmds:
subparsers = parser.add_subparsers()
seen = {}
for sub_cls in subcmds:
if sub_cls.cmd in seen:
new_loc = inspect.getfile(sub_cls)
old_loc = inspect.getfile(seen[sub_cls.cmd][-1])
msg = ("\"%s\" already defined in %s (ignoring "
"definition in %s") % (sub_cls.cmd, old_loc,
new_loc)
logger().warn(msg)
continue
seen.setdefault(sub_cls.cmd, []).append(sub_cls)
descr = sub_cls.description()
kw = {'description': sub_cls.description(),
# keep indention and newlines in docstr
Expand Down

0 comments on commit c508549

Please sign in to comment.