<commit_before>import argparse

from . import diff_routines, help


RUN_ALL = 'all'
RUN_FAILED = 'failed'
RUN_FAILING = 'failing'
RUN_PASSED = 'passed'
RUN_PASSING = 'passing'
RUN_GLOB = '<glob>'
RUN_PIPE = '-'
ONE_ARG_COMMANDS = (
    RUN_ALL, RUN_FAILED, RUN_FAILING, RUN_PASSED, RUN_PASSING, RUN_PIPE
)

RUN_PHASE = 'phase'
RUN_CHECK = 'check'
RUN_SUITE = 'suite'
RUN_FILES = 'files'

RUN_PHASE_AND_CHECK = 'phase <#> check <#>'


class OverloadedArg(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        num_values = len(values)
        command = None

        # Let's not get all polymorphic here.
        # Explicit is better.

        if num_values == 1:
            if values[0] in ONE_ARG_COMMANDS:
                command = values[0]
            else: # it's a glob
                command = RUN_GLOB
                setattr(namespace, 'glob', values[0])
        elif num_values == 2:
            if values[0] == RUN_PHASE:
                command = RUN_PHASE
                setattr(namespace, 'phase', values[1])
            elif values[0] == RUN_CHECK:
                command = RUN_CHECK
                setattr(namespace, 'check', values[1])
            elif values[0] == RUN_FILES:
                command = RUN_FILES
                setattr(namespace, 'files', values[1])
            elif values[0] == RUN_SUITE:
                command = RUN_SUITE
                setattr(namespace, 'suite', values[1])
        elif num_values == 4:
            if values[0] == RUN_PHASE and values[2] == RUN_CHECK:
                command = RUN_PHASE_AND_CHECK
                setattr(namespace, 'phase', values[1])
                setattr(namespace, 'check', values[3])

        setattr(namespace, 'command', command)


def build_arg_parser():
    parser = argparse.ArgumentParser(
        usage=help.usage,
        description=help.description,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument(
        'command',<commit_msg>Improved syntax-error messages for Run.<commit_after>import argparse

from . import diff_routines, help


RUN_ALL = 'all'
RUN_FAILED = 'failed'
RUN_FAILING = 'failing'
RUN_PASSED = 'passed'
RUN_PASSING = 'passing'
RUN_GLOB = '<glob>'
RUN_PIPE = '-'
ONE_ARG_COMMANDS = (
    RUN_ALL, RUN_FAILED, RUN_FAILING, RUN_PASSED, RUN_PASSING, RUN_PIPE
)

RUN_PHASE = 'phase'
RUN_CHECK = 'check'
RUN_SUITE = 'suite'
RUN_FILES = 'files'

RUN_PHASE_AND_CHECK = 'phase <#> check <#>'


class OverloadedArg(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        num_values = len(values)
        command = None

        if num_values == 1:
            if values[0] in ONE_ARG_COMMANDS:
                command = values[0]
            else:
                # It's a glob.
                command = RUN_GLOB
                setattr(namespace, 'glob', values[0])
        elif num_values == 2:
            # Example:
            #
            # `yuno run phase 1` will make a namespace like this:
            #     (command='run', phase='1', ...)
            #
            # where <command> is <values[0]> and a field named <values[0]> is
            # <values[1]>. The <command> is used for routing to a handler
            # which will read <values[1]> from a key named <values[0]>.
            #
            # TODO: This maybe shouldn't need a comment to be clear.
            argument, value = values
            setattr(namespace, argument, value)

            command = values[0]
        elif num_values == 4:
            if values[0] == RUN_PHASE and values[2] == RUN_CHECK:
                command = RUN_PHASE_AND_CHECK
                setattr(namespace, 'phase', values[1])
                setattr(namespace, 'check', values[3])
            else:
                parser.error("Expected `phase <#> check <#>`; got `%s`" % (
                    ' '.join(values)))
        else:
            num_shown = 6
            num_hidden = num_values - num_shown
            shown_values = values[:num_shown]

            message = "Expected 1, 2, or 4 arguments; got %d: %s" % (
                num_values, ' '.join(shown_values))

            if num_hidden > 0:
                message += " [... %d more]" % num_hidden

            parser.error(message)

        setattr(namespace, 'command', command)


def build_arg_parser():
    parser = argparse.ArgumentParser(
        usage=help.usage,
        description=help.description,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument(
        'command',
{'fraction_non_alphanumeric': 0.11016206745510293, 'fraction_numerical': 0.008979413053000438, 'mean_word_length': 2.069220430107527, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, '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': '2045731', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1350, 'n_words': 337}