<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import copy
import argparse
from collections import defaultdict

from termcolor import colored

import six

from .. import core, conf, util, exceptions
from .base import BaseCommand


# =============================================================================
# CONSTANTS
# =============================================================================

COMMANDS_MODULE = "{}.commands".format(conf.PACKAGE)

MODE_IN, MODE_TEST, MODE_OUT = "in", "test", "out"


# =============================================================================
# CLASS
# =============================================================================

class CorralArgumentParserError(Exception):
    pass


class CorralHelpFormatter(argparse.HelpFormatter):
    pass


class CorralArgumentParser(argparse.ArgumentParser):

    def error(self, message):
        raise CorralArgumentParserError(message)


class CorralCLIParser(object):

    def __init__(self, description):
        usage = ("run 'python {} <COMMAND> --help'").format(sys.argv[0])

        self.global_parser = CorralArgumentParser(
            description=description, usage=usage,
            formatter_class=CorralHelpFormatter)
        self.global_parser.add_argument(
            "--version", "-v", action="version", version=core.get_version())
        self.global_parser.add_argument(
            "-x", "--stacktrace", dest="stacktrace",
            action="store_true", default=False)

        self.subparsers = self.global_parser.add_subparsers()
        self.help_texts = defaultdict(list)

    def  _defaut_scmd_fmt(self, scmd, htext, max_line_length, color):
        scmd = "[{}]".format(scmd)
        line = "{} {}".format(color(scmd, "green"), htext)
        if max_line_length and len(line) > max_line_length:
            line = line[:max_line_length-3] + "..."
        return line

    def main_help_text(self, max_line_length=80, color=True):
        color = colored if color else (lambda t: t)
        scmd_fmt = self._defaut_scmd_fmt
        usage = ["", self.global_parser.usage, "", "Available subcommands", ""]
        usage.extend([color("CORRAL", "red")])
        usage.extend(
                scmd_fmt(hparts[0], hparts[1], max_line_length, color)
                for hparts in sorted(self.help_texts["corral"]))

        pkgs = [k for k in self.help_texts.keys() if k != "corral"]
        for pkg in pkgs:
            usage.extend(["", color(pkg.upper(), "red")])
            usage.extend(
                scmd_fmt(hparts[0], hparts[1], max_line_length, color)
                for hparts in sorted(self.help_texts[pkg]))

        return "\n".join(usage)

    def add_subparser(self, title, command, mode, **kwargs):
        if mode not in (MODE_IN, MODE_OUT, MODE_TEST):
            msg = "Command mode must be '{}', '{}', or '{}'. Found '{}'"
            raise ValueError(MODE_IN, MODE_OUT, MODE_TEST, msg.format(mode))
        parser = self.subparsers.add_parser(title, **kwargs)
        parser.set_defaults(command=command)
        parser.set_defaults(mode=mode)

        project = command.__module__.split(".", 1)[0]
        description = " ".join(
            p.strip() for p in parser.description.split() if p.strip())
        help_parts = (title, description or "-")
        self.help_texts[project].append(help_parts)
        return parser

    def extract_func(self, ns):
        kwargs = dict(ns._get_kwargs())
        command = kwargs.pop("command")
        mode = kwargs.pop("mode")
        func_kwargs, global_kwargs = {}, {}
        for k, v in kwargs.items():
            if k in ("stacktrace",):<commit_msg>better bormat in help<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import copy
import argparse
from collections import defaultdict

from termcolor import colored

import six

from .. import core, conf, util, exceptions
from .base import BaseCommand


# =============================================================================
# CONSTANTS
# =============================================================================

COMMANDS_MODULE = "{}.commands".format(conf.PACKAGE)

MODE_IN, MODE_TEST, MODE_OUT = "in", "test", "out"


# =============================================================================
# CLASS
# =============================================================================

class CorralArgumentParserError(Exception):
    pass


class CorralHelpFormatter(argparse.HelpFormatter):
    pass


class CorralArgumentParser(argparse.ArgumentParser):

    def error(self, message):
        raise CorralArgumentParserError(message)


class CorralCLIParser(object):

    def __init__(self, description):
        usage = ("run 'python {} <COMMAND> --help'").format(sys.argv[0])

        self.global_parser = CorralArgumentParser(
            description=description, usage=usage,
            formatter_class=CorralHelpFormatter)
        self.global_parser.add_argument(
            "--version", "-v", action="version", version=core.get_version())
        self.global_parser.add_argument(
            "-x", "--stacktrace", dest="stacktrace",
            action="store_true", default=False)

        self.subparsers = self.global_parser.add_subparsers()
        self.help_texts = defaultdict(list)

    def  _defaut_scmd_fmt(self, scmd, htext, max_line_length, color):
        line = "  {} - {}".format(color(scmd, "green"), htext)
        if max_line_length and len(line) > max_line_length:
            line = line[:max_line_length-3] + "..."
        return line

    def main_help_text(self, max_line_length=80, color=True):
        color = colored if color else (lambda t: t)
        scmd_fmt = self._defaut_scmd_fmt
        usage = ["", self.global_parser.usage, "", "Available subcommands", ""]
        usage.extend([color("[corral]", "red")])
        usage.extend(
                scmd_fmt(hparts[0], hparts[1], max_line_length, color)
                for hparts in sorted(self.help_texts["corral"]))

        pkgs = [k for k in self.help_texts.keys() if k != "corral"]
        for pkg in pkgs:
            pkg_title = "[{}]".format(pkg)
            usage.extend(["", color(pkg_title, "red")])
            usage.extend(
                scmd_fmt(hparts[0], hparts[1], max_line_length, color)
                for hparts in sorted(self.help_texts[pkg]))

        return "\n".join(usage)

    def add_subparser(self, title, command, mode, **kwargs):
        if mode not in (MODE_IN, MODE_OUT, MODE_TEST):
            msg = "Command mode must be '{}', '{}', or '{}'. Found '{}'"
            raise ValueError(MODE_IN, MODE_OUT, MODE_TEST, msg.format(mode))
        parser = self.subparsers.add_parser(title, **kwargs)
        parser.set_defaults(command=command)
        parser.set_defaults(mode=mode)

        project = command.__module__.split(".", 1)[0]
        description = " ".join(
            p.strip() for p in parser.description.split() if p.strip())
        help_parts = (title, description or "-")
        self.help_texts[project].append(help_parts)
        return parser

    def extract_func(self, ns):
        kwargs = dict(ns._get_kwargs())
        command = kwargs.pop("command")
        mode = kwargs.pop("mode")
        func_kwargs, global_kwargs = {}, {}
        for k, v in kwargs.items():
            if k in ("stacktrace",):
{'fraction_non_alphanumeric': 0.2075061191188469, 'fraction_numerical': 0.002991569214033179, 'mean_word_length': 3.102063580591188, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1320404', 'n_tokens_mistral': 2160, 'n_tokens_neox': 2079, 'n_words': 460}