[issue11588] Add "necessarily inclusive" groups to argparse

paul j3 report at bugs.python.org
Tue Mar 4 20:01:46 CET 2014


paul j3 added the comment:

A couple more thoughts on an expanded argument testing mechanism:

- do we need both 'seen_actions' and 'seen_non_default_actions'?  

'seen_actions' is used only to test whether all required actions have been seen.  These 2 sets differ in how positionals with '?*' are categorized.  Positionals like this are always 'seen', even if they just get the default value.  But they are not required (the case of a '*' positional without default needs to be revisited.)

- If 'seen_non_default_actions' is changed to a list (currently its a set), users could test for repeated use on an optional, or even the order of arguments.  

- One way to make this testing mechanism more user-friendly is to provide convenience functions via a decorator.  

For example the decorator could wrap the 'seen_non_default_actions' argument in a 'seen' function.  Such a function could accept either an Action or a 'dest' string, it could accept a single Action, or a list of them, etc.  There could be other functions like 'count', 'unique', 'mutually_exclusive', 'inclusive', etc.

    def testwfnc(func):
        # decorator to register function and provide 'seen'
        name = func.__name__
        def wrapped(parser, seen_actions, *args):
            def seen(*args):
                actions = seen_actions
                if isinstance(args[0], str):
                    actions = [a.dest for a in actions]
                if len(args)>1:
                    return [a in actions for a in args]
                else:
                    return args[0] in actions
            return func(parser, seen)
        parser.register('cross_tests', name, wrapped)
        return wrapped

    #@testwfnc
    def test(parser, seen, *args):
        if seen(a_file):
            print(seen(a_dir, a_pat, a_suf))
            cnt = sum(seen(a_dir, a_pat, a_suf))
        if cnt>0:
            parser.error('FILE cannot have DIR, PATTERN or SUFFIX')
        ...

The attached script experiments with several versions of decorators.  Some sort of testing Class is probably the way to go if we want to provide many convenience methods.

----------
Added file: http://bugs.python.org/file34285/issue11588_4.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11588>
_______________________________________


More information about the Python-bugs-list mailing list