<commit_before>'''
A wrapper around optparse.OptionParser that makes defining options for a script even easier.


-------------------------myscript.py--------------------

def main():
    options = Options.from_argv()
    if options.action == 'audit':
        some_function(options.account_id, dry_run=options.dry_run)
    elif ...
    else:
        options.print_help()
    


class Options(ScriptOptions)
    account_id = Opt(required=True, help="The account you want to audit")
    dry_run = Opt(default=False, action='store_true', help="Set this option to print what would happen without execution")
    action = Opt(default='audit', choices=['audit', 'detect_fraud', 'validate_books'], help="The action you want to perform")

if __name__ == '__main__':
    main()

---------------------
Now you can run your script and include the options:

python myscript.py --acount-id=7
python myscript.py -a 7 --dry-run





'''

from optparse import OptionParser
import sys

from .propertized import Propertized, Prop

class Opt(Prop):
    def __init__(self, default=None, required=False, help='', choices=(), action=None, type=None, **kwargs):
        super(Opt, self).__init__(default=default, help=help, choices=choices, **kwargs)
        self.default = default
        self.help = help
        self.choices = choices
        self.extra = kwargs
        self.action = action
        self.required = required
        self.type = type



class ScriptOptions(Propertized):
    def __init__(self):
        parser = None

    @classmethod
    def from_argv(cls, argv=None, **defaults):
        if not argv:
            argv = sys.argv
        parser = OptionParser()
        script_options = cls()
        letters = set(['h'])

        for opt in script_options.list_class_props():

            args = []
            kwargs = {}
            letter = opt.attr_name[0]
            if letter not in letters:
                args.append('-' + letter)
                letters.add(letter)
            args.append('--' + opt.attr_name.replace('_', '-'))
            if opt.default:
                kwargs['default'] = opt.default
            if opt.attr_name in defaults:
                kwargs['default'] = defaults[opt.attr_name]
            kwargs['help'] = ''
            if opt.help:
                kwargs['help'] = opt.help<commit_msg>allowing initiazing options with params<commit_after>'''
A wrapper around optparse.OptionParser that makes defining options for a script even easier.


-------------------------myscript.py--------------------

def main():
    options = Options.from_argv()
    if options.action == 'audit':
        some_function(options.account_id, dry_run=options.dry_run)
    elif ...
    else:
        options.print_help()
    


class Options(ScriptOptions)
    account_id = Opt(required=True, help="The account you want to audit")
    dry_run = Opt(default=False, action='store_true', help="Set this option to print what would happen without execution")
    action = Opt(default='audit', choices=['audit', 'detect_fraud', 'validate_books'], help="The action you want to perform")

if __name__ == '__main__':
    main()

---------------------
Now you can run your script and include the options:

python myscript.py --acount-id=7
python myscript.py -a 7 --dry-run





'''

from optparse import OptionParser
import sys

from .propertized import Propertized, Prop

class Opt(Prop):
    def __init__(self, default=None, required=False, help='', choices=(), action=None, type=None, **kwargs):
        super(Opt, self).__init__(default=default, help=help, choices=choices, **kwargs)
        self.default = default
        self.help = help
        self.choices = choices
        self.extra = kwargs
        self.action = action
        self.required = required
        self.type = type



class ScriptOptions(Propertized):
    def __init__(self, **kwargs):
        self._hydrate_from_kwargs(kwargs)
        parser = None

    @classmethod
    def from_argv(cls, argv=None, **defaults):
        if not argv:
            argv = sys.argv
        parser = OptionParser()
        script_options = cls()
        letters = set(['h'])

        for opt in script_options.list_class_props():

            args = []
            kwargs = {}
            letter = opt.attr_name[0]
            if letter not in letters:
                args.append('-' + letter)
                letters.add(letter)
            args.append('--' + opt.attr_name.replace('_', '-'))
            if opt.default:
                kwargs['default'] = opt.default
            if opt.attr_name in defaults:
                kwargs['default'] = defaults[opt.attr_name]
            kwargs['help'] = ''
            if opt.help:
                kwargs['help'] = opt.help
{'fraction_non_alphanumeric': 0.14032121724429417, 'fraction_numerical': 0.001267962806424345, 'mean_word_length': 2.2153532608695654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5875764', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1293, 'n_words': 363}