# Buggy code
def main(argv=None):  # noqa: C901
    """Run dvc CLI command.

    Args:
        argv: optional list of arguments to parse. sys.argv is used by default.

    Returns:
        int: command's return code.
    """
    args = None
    disable_other_loggers()

    outerLogLevel = logger.level
    try:
        args = parse_args(argv)

        level = None
        if args.quiet:
            level = logging.CRITICAL
        elif args.verbose == 1:
            level = logging.DEBUG
        elif args.verbose > 1:
            level = logging.TRACE

        if level is not None:
            logger.setLevel(level)

        logger.trace(args)

        with profile(enable=args.cprofile, dump=args.cprofile_dump):
            cmd = args.func(args)
            ret = cmd.run()
    except ConfigError:
        logger.exception("configuration error")
        ret = 251
    except KeyboardInterrupt:
        logger.exception("interrupted by the user")
        ret = 252
    except NotDvcRepoError:
        logger.exception("")
        ret = 253
    except DvcParserError:
        ret = 254
    except DvcException:
        ret = 255
        logger.exception("")
    except Exception as exc:  # noqa, pylint: disable=broad-except
        # pylint: disable=no-member
        if isinstance(exc, OSError) and exc.errno == errno.EMFILE:
            logger.exception(
                "too many open files, please visit "
                "{} to see how to handle this "
                "problem".format(error_link("many-files")),
                extra={"tb_only": True},
            )
        else:
            from dvc.info import get_dvc_info

            logger.exception("unexpected error")
            logger.info(FOOTER)
            dvc_info = get_dvc_info()
            logger.info(dvc_info)
        ret = 255

    try:
        if analytics.is_enabled():
            analytics.collect_and_send_report(args, ret)

        return ret
    finally:
        logger.setLevel(outerLogLevel)

        # Closing pools by-hand to prevent weird messages when closing SSH
        # connections. See https://github.com/iterative/dvc/issues/3248 for
        # more info.
        close_pools()

        # Remove cached repos in the end of the call, these are anonymous
        # so won't be reused by any other subsequent run anyway.
        clean_repos()


# Traceback (ruamel.yaml.constructor.DuplicateKeyError)
$ dvc metrics diff -v
2020-10-05 09:00:38,005 DEBUG: Check for update is enabled.
2020-10-05 09:00:38,039 DEBUG: fetched: [(3,)]
2020-10-05 09:00:38,048 DEBUG: fetched: [(682,)]
2020-10-05 09:00:38,049 ERROR: unexpected error - while constructing a mapping
in "<unicode string>", line 3, column 5
found duplicate key "metrics" with value "[]" (original value: "[]")
in "<unicode string>", line 20, column 5

To suppress this check see:
http://yaml.readthedocs.io/en/latest/api.html#duplicate-keys

Duplicate keys will become an error in future releases, and are errors
by default when using the new API.

------------------------------------------------------------
Traceback (most recent call last):
File "/Users/dmitry/src/dvc/dvc/main.py", line 76, in main
ret = cmd.run()
File "/Users/dmitry/src/dvc/dvc/command/metrics.py", line 119, in run
all=self.args.all,
File "/Users/dmitry/src/dvc/dvc/repo/metrics/__init__.py", line 13, in diff
return diff(self.repo, *args, **kwargs)
File "/Users/dmitry/src/dvc/dvc/repo/metrics/diff.py", line 22, in diff
old = _get_metrics(repo, *args, **kwargs, rev=(a_rev or "HEAD"))
File "/Users/dmitry/src/dvc/dvc/repo/metrics/diff.py", line 9, in _get_metrics
*args, **kwargs, revs=[rev] if rev else None
File "/Users/dmitry/src/dvc/dvc/repo/metrics/__init__.py", line 8, in show
return show(self.repo, *args, **kwargs)
File "/Users/dmitry/src/dvc/dvc/repo/__init__.py", line 51, in wrapper
return f(repo, *args, **kwargs)
File "/Users/dmitry/src/dvc/dvc/repo/metrics/show.py", line 108, in show
metrics = _collect_metrics(repo, targets, recursive)
File "/Users/dmitry/src/dvc/dvc/repo/metrics/show.py", line 33, in _collect_metrics
for stage in repo.stages:
File "/Users/dmitry/virtenvs/ml/lib/python3.7/site-packages/funcy/objects.py", line 28, in __get__
res = instance.__dict__[self.fget.__name__] = self.fget(instance)
File "/Users/dmitry/src/dvc/dvc/repo/__init__.py", line 554, in stages
return self._collect_stages()
File "/Users/dmitry/src/dvc/dvc/repo/__init__.py", line 562, in _collect_stages
new_stages = self.get_stages(os.path.join(root, file_name))
File "/Users/dmitry/src/dvc/dvc/repo/__init__.py", line 254, in get_stages
return list(dvcfile.stages.values())
File "/Users/dmitry/src/dvc/dvc/dvcfile.py", line 228, in stages
data, _ = self._load()
File "/Users/dmitry/src/dvc/dvc/dvcfile.py", line 107, in _load
d = parse_yaml(stage_text, self.path)
File "/Users/dmitry/src/dvc/dvc/utils/serialize/_yaml.py", line 24, in parse_yaml
return yaml.load(text) or {}
File "/Users/dmitry/virtenvs/ml/lib/python3.7/site-packages/ruamel/yaml/main.py", line 343, in load
return constructor.get_single_data()
File "/Users/dmitry/virtenvs/ml/lib/python3.7/site-packages/ruamel/yaml/constructor.py", line 113, in get_single_data
return self.construct_document(node)
File "/Users/dmitry/virtenvs/ml/lib/python3.7/site-packages/ruamel/yaml/constructor.py", line 123, in construct_document
for _dummy in generator:
File "/Users/dmitry/virtenvs/ml/lib/python3.7/site-packages/ruamel/yaml/constructor.py", line 723, in construct_yaml_map
value = self.construct_mapping(node)
File "/Users/dmitry/virtenvs/ml/lib/python3.7/site-packages/ruamel/yaml/constructor.py", line 440, in construct_mapping
return BaseConstructor.construct_mapping(self, node, deep=deep)
File "/Users/dmitry/virtenvs/ml/lib/python3.7/site-packages/ruamel/yaml/constructor.py", line 257, in construct_mapping
if self.check_mapping_key(node, key_node, mapping, key, value):
File "/Users/dmitry/virtenvs/ml/lib/python3.7/site-packages/ruamel/yaml/constructor.py", line 295, in check_mapping_key
raise DuplicateKeyError(*args)
ruamel.yaml.constructor.DuplicateKeyError: while constructing a mapping
in "<unicode string>", line 3, column 5
found duplicate key "metrics" with value "[]" (original value: "[]")
in "<unicode string>", line 20, column 5

To suppress this check see:
http://yaml.readthedocs.io/en/latest/api.html#duplicate-keys

Duplicate keys will become an error in future releases, and are errors
by default when using the new API.

------------------------------------------------------------

Having any troubles? Hit us up at https://dvc.org/support, we are always happy to help!
DVC version: 1.7.9+7b590e
---------------------------------
Platform: Python 3.7.3 on Darwin-19.6.0-x86_64-i386-64bit
Supports: azure, gdrive, gs, hdfs, http, https, s3, ssh, oss
Cache types: reflink, hardlink, symlink
Cache directory: apfs on /dev/disk1s5
Workspace directory: apfs on /dev/disk1s5
Repo: dvc, git
2020-10-05 09:00:38,885 DEBUG: Analytics is enabled.
2020-10-05 09:00:39,063 DEBUG: Trying to spawn '['daemon', '-q', 'analytics', '/var/folders/hq/h82mgrhx37nc89_1rcg6gm280000gn/T/tmpivw63jns']'
2020-10-05 09:00:39,065 DEBUG: Spawned '['daemon', '-q', 'analytics', '/var/folders/hq/h82mgrhx37nc89_1rcg6gm280000gn/T/tmpivw63jns']

# Fixed code
def main(argv=None):  # noqa: C901
    """Run dvc CLI command.

    Args:
        argv: optional list of arguments to parse. sys.argv is used by default.

    Returns:
        int: command's return code.
    """
    args = None
    disable_other_loggers()

    outerLogLevel = logger.level
    try:
        args = parse_args(argv)

        level = None
        if args.quiet:
            level = logging.CRITICAL
        elif args.verbose == 1:
            level = logging.DEBUG
        elif args.verbose > 1:
            level = logging.TRACE

        if level is not None:
            logger.setLevel(level)

        logger.trace(args)

        with profile(enable=args.cprofile, dump=args.cprofile_dump):
            cmd = args.func(args)
            ret = cmd.run()
    except ConfigError:
        logger.exception("configuration error")
        ret = 251
    except KeyboardInterrupt:
        logger.exception("interrupted by the user")
        ret = 252
    except NotDvcRepoError:
        logger.exception("")
        ret = 253
    except DvcParserError:
        ret = 254
    except DvcException:
        ret = 255
        logger.exception("")
    except Exception as exc:  # noqa, pylint: disable=broad-except
        # pylint: disable=no-member
        if isinstance(exc, OSError) and exc.errno == errno.EMFILE:
            logger.exception(
                "too many open files, please visit "
                "{} to see how to handle this "
                "problem".format(error_link("many-files")),
                extra={"tb_only": True},
            )
        else:
            from dvc.info import get_dvc_info

            logger.exception("unexpected error")

            dvc_info = get_dvc_info()
            logger.debug("Version info for developers:\n%s", dvc_info)

            logger.info(FOOTER)
        ret = 255

    try:
        if analytics.is_enabled():
            analytics.collect_and_send_report(args, ret)

        return ret
    finally:
        logger.setLevel(outerLogLevel)

        # Closing pools by-hand to prevent weird messages when closing SSH
        # connections. See https://github.com/iterative/dvc/issues/3248 for
        # more info.
        close_pools()

        # Remove cached repos in the end of the call, these are anonymous
        # so won't be reused by any other subsequent run anyway.
        clean_repos()