#!/usr/bin/python
from l3tlib.command import L3tBugCommand
from l3tlib.template import format_bug
from l3tlib.util import paged_output


class GetBug(L3tBugCommand):

    descr = """\
Shows a given bug

It relies on the credentials from osc to authenticate on the Bugzilla API.
"""
    usage = "%prog [-b BUG | BUG | -i INCIDENT]"

    def init_parser(self, parser):
        super(GetBug, self).init_parser(parser)
        parser.add_option("-n", "--nlatest", type="int", default=None,
                          help="Number of latest comments to be shown")
        parser.add_option("-W", "--no-wrap-comments", default=False,
                          action="store_true",
                          help="Do not wrap comment text blocks")

    def parse_args(self, parser, args):
        opts, values = super(GetBug, self).parse_args(parser, args)
        if opts.no_wrap_comments:
            self.set_runtime_config("bugzilla", "wrap-comments", "false")
        return opts, args

    def run(self):
        (bug, incidents) = self.l3t.get_bug_incidents(self.opts.incident,
                                                      self.opts.bug)
        output = format_bug(self.config, bug, self.opts.nlatest,
                            incidents=incidents)
        paged_output(output, self.config)


GetBug().main()
