Question: I'm trying to figure out how to write a decorator to check if the function was called with specific optional argument. This may not be the pythonic way of checking for arguments, but I'd like to know the solution using decorators regardless. Here is an example of what I'm looking for:

```
@require_arguments("N", "p")  # Question here

def g(x,*args,**kwargs):
    if "N" not in kwargs: raise SyntaxError("missing N")
    if "p" not in kwargs: raise SyntaxError("missing p")
    print x

g(3,N=2) # Raise "missing p"

```

How do I write the decorator `@require_arguments(*args)` that will raise the appropriate error?

Answer: 
Here you go:

```
def require_arguments(*reqargs):
    def decorator(func):
        def wrapper(*args, **kwargs):
            for arg in reqargs:
                if not arg in kwargs:
                    raise TypeError("Missing %s" % arg)
            return func(*args, **kwargs)
        return wrapper
    return decorator

@require_arguments("N", "p")  # Question here
def g(x,*args,**kwargs):
    print x

g(3,N=2) # Raise "missing p"

```

Output:

```
Traceback (most recent call last):
  File "arg.py", line 19, in <module>
    g(3,N=2) # Raise "missing p"
  File "arg.py", line 7, in wrapper
    raise ValueError("Missing %s" % arg)
ValueError: Missing p

```
[dano](https://stackoverflow.com/users/2073595/dano)'s answer is correct, the only suggestions I can add are:

* Use [`functools.wraps`](https://docs.python.org/2/library/functools.html#functools.wraps) to create your decorators, this way the name of the function and its *docstring* will be preserved.
* Generate a list of all the missing arguments instead of a single one. I personally hate when the compiler just says: '*you forgot a*', and then when I add it it still complains: '*you forgot b*' and so on.

The decorator becomes:

```
from functools import wraps

def require_arguments(*reqs):
    def decorator(func):
        @wraps(func)
        def decorated(*args, **kwargs):
            missing = [req for req in reqs if req not in kwargs]
            if missing:
                raise TypeError('missing ' + ', '.join(missing))
            return func(*args, **kwargs)
        return decorated
    return decorator

```

Example
-------

```
@require_arguments('N', 'p')
def g(x, *args, **kargs):
    """just return x"""
    print x

print g.__name__
print g.__doc__

try:
    print g(3)
except TypeError as e:
    print e

```

*Output*

```
g
just return x
missing N, p

```

A better approach
-----------------

If you need required arguments, just put ask for them explicitly like: def `g(x, N, p)`. You will still be able to invoke the function like `g(x=2, N=5, p=3)` for make your code more expressive, even in a different order if you specify the argument. But you will have a fixed api (which is normally good) and you will be able either to specify the arguments or to use the fixed api order.

```
def f(a, b, c):
    print a, b, c

f(1, 2, 3)            # 1 2 3
f(c=1, b=2, a=3)      # 3 2 1

```