<commit_before>#
# Copyright (c) 2004-2005 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#
"""
Command-line option handling
"""

import inspect
import optparse
import StringIO

from conary.lib import log, util
import sys


(NO_PARAM,   # arg may occur, no parameter
 ONE_PARAM,  # arg may occur once, req'd parameter
 OPT_PARAM,  # arg may occur once, optional parameter
 MULT_PARAM, # arg may occur N times, w/ parameter
 COUNT_PARAM, # arg may occur N times, value is the count
 STRICT_OPT_PARAM, # arg may occur once, optional parameter, stricter parsing
 ) = range(0,6)

(NORMAL_HELP,
 VERBOSE_HELP,  # only display in usage messages if -v is used
) = range(0,2)

class OptionError(Exception):
    val = 1
    def __init__(self, msg, parser):
        Exception.__init__(self, msg)
        self.parser = parser

class HelpFormatter(optparse.IndentedHelpFormatter):
    def format_option(self, option):
        if option.help_level == VERBOSE_HELP:
            return ''
        return optparse.IndentedHelpFormatter.format_option(self, option)

class Option(optparse.Option):
    ATTRS = optparse.Option.ATTRS[:]
    ATTRS.append('help_level')

    def _set_attrs(self, attrs):
        return optparse.Option._set_attrs(self, attrs)

class OptionParser(optparse.OptionParser):
    forbiddenOpts = set(str(x) for x in range(0,9))
    matchPartialOptions = False

    def __init__(self, *args, **kw):
        self.hobbleShortOpts = kw.pop('hobbleShortOpts', False)
        optparse.OptionParser.__init__(self, *args, **kw)

    def _match_long_opt(self, opt):
        match = optparse._match_abbrev(opt, self._long_opt)
        if not self.matchPartialOptions and opt != match:
            raise optparse.BadOptionError("no such option: %s" % opt)
        return match

    def error(self, msg):
        raise OptionError(msg, self)

    def _process_short_opts(self, rargs, values):
        if (self.hobbleShortOpts and 
            (len(self.rargs[0]) > 2 or self.rargs[0][1] in self.forbiddenOpts)):
            self.largs.append(self.rargs.pop(0))
        else:
            return optparse.OptionParser._process_short_opts(self, rargs,
                                                             values)

    def _process_long_opt(self, rargs, values):
        opt = self._match_long_opt(rargs[0].split('=')[0])

        option = self._long_opt[opt]

        if '=' in rargs[0]:
            had_explicit_value = True
            if option.callback in (strictOptParamCallback, optParamCallback):
                rargs[:] = rargs[0].split('=', 1) + rargs[1:]
                self.rargs = rargs
        else:
            had_explicit_value = False

        option.had_explicit_value = had_explicit_value
        return optparse.OptionParser._process_long_opt(self, rargs, values)

def optParamCallback(option, opt_str, value, parser, *args, **kw):
    strict = kw.pop('strictOpt', False)

    value = True
    if option.had_explicit_value:
        newValue = parser.rargs[0]
        del parser.rargs[0]
        if newValue: # handle --opt= - treat like --opt
            value = newValue
    elif (not strict and parser.rargs 
          and parser.rargs[0] and parser.rargs[0][0] != '-'):
        newValue = parser.rargs[0]
        del parser.rargs[0]
        if newValue: # handle --opt= - treat like --opt
            value = newValue
    setattr(parser.values, option.dest, value)

def strictOptParamCallback(*args, **kw):
    kw['strictOpt'] = True
    return optParamCallback(*args, **kw)


def addOptions(parser, argDef, skip=None):
    for name, data in sorted(argDef.iteritems()):
        if name == skip:
            continue
        if isinstance(data, dict):
            group = optparse.OptionGroup(parser, name)
            addOptions(group, data)
            parser.add_option_group(group)
            continue
        help = ''
        help_level = NORMAL_HELP
        shortOpt = None
        meta = None
        if isinstance(data, (list, tuple)):
            if len(data) == 3:
                shortOpt = data[0]
                data = data[1:]
            if len(data) >= 2:
                help = data[1]
                if isinstance(help, (list, tuple)):
                    if isinstance(help[0], int):
                        help_level = help[0]
                        help = help[1:]
                    if len(help) == 2:
                        help, meta = help
                    else:
                        help = help[0]
            paramType = data[0]
        else:
            paramType = data
        flagNames = ['--' + name]
        if shortOpt:
            flagNames.append(shortOpt)

        if paramType == NO_PARAM:
            parser.add_option(action='store_true', dest=name, help=help,
                              help_level=help_level,
                              metavar=meta, *flagNames)
        elif paramType == ONE_PARAM:
            parser.add_option(dest=name, help=help, metavar=meta,
                              help_level=help_level, *flagNames)
        elif paramType == STRICT_OPT_PARAM:
            parser.add_option(action='callback',
                              callback=strictOptParamCallback, dest=name,
                              type='string', nargs=0, help=help,
                              help_level=help_level,
                              metavar=meta, *flagNames)
        elif paramType == OPT_PARAM:
            parser.add_option(action='callback',
                              callback=optParamCallback, dest=name,
                              type='string', nargs=0, help=help,
                              help_level=help_leve,
                              metavar=meta, *flagNames)
        elif paramType == MULT_PARAM:
            parser.add_option(action='append', dest=name, help=help,
                              help_level=help_level,
                              metavar=meta, *flagNames)
        elif paramType == COUNT_PARAM:<commit_msg>correct typo<commit_after>#
# Copyright (c) 2004-2005 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#
"""
Command-line option handling
"""

import inspect
import optparse
import StringIO

from conary.lib import log, util
import sys


(NO_PARAM,   # arg may occur, no parameter
 ONE_PARAM,  # arg may occur once, req'd parameter
 OPT_PARAM,  # arg may occur once, optional parameter
 MULT_PARAM, # arg may occur N times, w/ parameter
 COUNT_PARAM, # arg may occur N times, value is the count
 STRICT_OPT_PARAM, # arg may occur once, optional parameter, stricter parsing
 ) = range(0,6)

(NORMAL_HELP,
 VERBOSE_HELP,  # only display in usage messages if -v is used
) = range(0,2)

class OptionError(Exception):
    val = 1
    def __init__(self, msg, parser):
        Exception.__init__(self, msg)
        self.parser = parser

class HelpFormatter(optparse.IndentedHelpFormatter):
    def format_option(self, option):
        if option.help_level == VERBOSE_HELP:
            return ''
        return optparse.IndentedHelpFormatter.format_option(self, option)

class Option(optparse.Option):
    ATTRS = optparse.Option.ATTRS[:]
    ATTRS.append('help_level')

    def _set_attrs(self, attrs):
        return optparse.Option._set_attrs(self, attrs)

class OptionParser(optparse.OptionParser):
    forbiddenOpts = set(str(x) for x in range(0,9))
    matchPartialOptions = False

    def __init__(self, *args, **kw):
        self.hobbleShortOpts = kw.pop('hobbleShortOpts', False)
        optparse.OptionParser.__init__(self, *args, **kw)

    def _match_long_opt(self, opt):
        match = optparse._match_abbrev(opt, self._long_opt)
        if not self.matchPartialOptions and opt != match:
            raise optparse.BadOptionError("no such option: %s" % opt)
        return match

    def error(self, msg):
        raise OptionError(msg, self)

    def _process_short_opts(self, rargs, values):
        if (self.hobbleShortOpts and 
            (len(self.rargs[0]) > 2 or self.rargs[0][1] in self.forbiddenOpts)):
            self.largs.append(self.rargs.pop(0))
        else:
            return optparse.OptionParser._process_short_opts(self, rargs,
                                                             values)

    def _process_long_opt(self, rargs, values):
        opt = self._match_long_opt(rargs[0].split('=')[0])

        option = self._long_opt[opt]

        if '=' in rargs[0]:
            had_explicit_value = True
            if option.callback in (strictOptParamCallback, optParamCallback):
                rargs[:] = rargs[0].split('=', 1) + rargs[1:]
                self.rargs = rargs
        else:
            had_explicit_value = False

        option.had_explicit_value = had_explicit_value
        return optparse.OptionParser._process_long_opt(self, rargs, values)

def optParamCallback(option, opt_str, value, parser, *args, **kw):
    strict = kw.pop('strictOpt', False)

    value = True
    if option.had_explicit_value:
        newValue = parser.rargs[0]
        del parser.rargs[0]
        if newValue: # handle --opt= - treat like --opt
            value = newValue
    elif (not strict and parser.rargs 
          and parser.rargs[0] and parser.rargs[0][0] != '-'):
        newValue = parser.rargs[0]
        del parser.rargs[0]
        if newValue: # handle --opt= - treat like --opt
            value = newValue
    setattr(parser.values, option.dest, value)

def strictOptParamCallback(*args, **kw):
    kw['strictOpt'] = True
    return optParamCallback(*args, **kw)


def addOptions(parser, argDef, skip=None):
    for name, data in sorted(argDef.iteritems()):
        if name == skip:
            continue
        if isinstance(data, dict):
            group = optparse.OptionGroup(parser, name)
            addOptions(group, data)
            parser.add_option_group(group)
            continue
        help = ''
        help_level = NORMAL_HELP
        shortOpt = None
        meta = None
        if isinstance(data, (list, tuple)):
            if len(data) == 3:
                shortOpt = data[0]
                data = data[1:]
            if len(data) >= 2:
                help = data[1]
                if isinstance(help, (list, tuple)):
                    if isinstance(help[0], int):
                        help_level = help[0]
                        help = help[1:]
                    if len(help) == 2:
                        help, meta = help
                    else:
                        help = help[0]
            paramType = data[0]
        else:
            paramType = data
        flagNames = ['--' + name]
        if shortOpt:
            flagNames.append(shortOpt)

        if paramType == NO_PARAM:
            parser.add_option(action='store_true', dest=name, help=help,
                              help_level=help_level,
                              metavar=meta, *flagNames)
        elif paramType == ONE_PARAM:
            parser.add_option(dest=name, help=help, metavar=meta,
                              help_level=help_level, *flagNames)
        elif paramType == STRICT_OPT_PARAM:
            parser.add_option(action='callback',
                              callback=strictOptParamCallback, dest=name,
                              type='string', nargs=0, help=help,
                              help_level=help_level,
                              metavar=meta, *flagNames)
        elif paramType == OPT_PARAM:
            parser.add_option(action='callback',
                              callback=optParamCallback, dest=name,
                              type='string', nargs=0, help=help,
                              help_level=help_level,
                              metavar=meta, *flagNames)
        elif paramType == MULT_PARAM:
            parser.add_option(action='append', dest=name, help=help,
                              help_level=help_level,
                              metavar=meta, *flagNames)
        elif paramType == COUNT_PARAM:
{'fraction_non_alphanumeric': 0.09737943867266242, 'fraction_numerical': 0.00744301442084044, 'mean_word_length': 2.032204983544899, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3998189', 'n_tokens_mistral': 3710, 'n_tokens_neox': 3621, 'n_words': 970}