Last active
August 29, 2015 14:20
-
-
Save mountcedar/228ba68c347354957454 to your computer and use it in GitHub Desktop.
Pythonで関数の引数をargparseに自動登録する ref: http://qiita.com/mountcedar/items/1b2e76f3bfd18dcc1975
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
from inspect import getargspec | |
def set_arguments(parser, func): | |
args, varargs, keywords, defaults = getargspec(func) | |
print 'args: ', args | |
print 'varargs', varargs | |
print 'keywords: ', keywords | |
print 'defaults: ', defaults | |
while len(args) > len(defaults): | |
l = list(defaults) | |
l.insert(0, None) | |
defaults = tuple(l) | |
for arg, default in zip(args, defaults): | |
if default is None: | |
parser.add_argument(dest=arg) | |
elif type(default) is tuple or type(default) is list: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default), | |
nargs='+' | |
) | |
elif type(default) is bool and default: | |
parser.add_argument( | |
'--disable_' + arg, | |
dest=arg, | |
default=default, | |
action="store_false" | |
) | |
elif type(default) is bool and not default: | |
parser.add_argument( | |
'--enable_' + arg, | |
dest=arg, | |
default=default, | |
action="store_true" | |
) | |
else: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default) | |
) | |
return parser | |
def func(x, aaa=1, bbb="a", ccc=[1,2,3,], ddd=True, eee=False): | |
pass | |
parser = argparse.ArgumentParser() | |
parser = set_arguments(parser, func) | |
print parser.parse_args() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getargspec | |
def func(aaa=1, bbb="a", ccc=[1,2,3,]): | |
pass | |
args, varargs, keywords, defaults = getargspec(func) | |
print 'args: ', args | |
print 'varargs', varargs | |
print 'keywords: ', keywords | |
print 'defaults: ', defaults |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
args: ['aaa', 'bbb', 'ccc'] | |
varargs None | |
keywords: None | |
defaults: (1, 'a', [1, 2, 3]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getargspec | |
def func(aaa=1, bbb="a", ccc=[1,2,3,]): | |
pass | |
args, varargs, keywords, defaults = getargspec(func) | |
import argparse | |
parser = argparse.ArgumentParser() | |
for arg, default in zip(args, defaults): | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default) | |
) | |
print parser.parse_args() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Namespace(aaa=1, bbb='a', ccc=[1, 2, 3]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getargspec | |
def func(aaa=1, bbb="a", ccc=[1,2,3,]): | |
pass | |
args, varargs, keywords, defaults = getargspec(func) | |
import argparse | |
parser = argparse.ArgumentParser() | |
for arg, default in zip(args, defaults): | |
if type(default) is tuple or type(default) is list: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default), | |
nargs='+' | |
) | |
else: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getargspec | |
def func(aaa=1, bbb="a", ccc=[1,2,3,], ddd=True, eee=False): | |
pass | |
args, varargs, keywords, defaults = getargspec(func) | |
import argparse | |
parser = argparse.ArgumentParser() | |
for arg, default in zip(args, defaults): | |
if type(default) is tuple or type(default) is list: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default), | |
nargs='+' | |
) | |
elif type(default) is bool and default: | |
parser.add_argument( | |
'--disable_' + arg, | |
dest=arg, | |
default=default, | |
action="store_false" | |
) | |
elif type(default) is bool and not default: | |
parser.add_argument( | |
'--enable_' + arg, | |
dest=arg, | |
default=default, | |
action="store_true" | |
) | |
else: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from inspect import getargspec | |
def func(x, aaa=1, bbb="a", ccc=[1,2,3,], ddd=True, eee=False): | |
pass | |
args, varargs, keywords, defaults = getargspec(func) | |
print 'args: ', args | |
print 'varargs', varargs | |
print 'keywords: ', keywords | |
print 'defaults: ', defaults | |
parser = argparse.ArgumentParser() | |
while len(args) > len(defaults): | |
l = list(defaults) | |
l.insert(0, None) | |
defaults = tuple(l) | |
for arg, default in zip(args, defaults): | |
if default is None: | |
parser.add_argument(dest=arg) | |
elif type(default) is tuple or type(default) is list: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default), | |
nargs='+' | |
) | |
elif type(default) is bool and default: | |
parser.add_argument( | |
'--disable_' + arg, | |
dest=arg, | |
default=default, | |
action="store_false" | |
) | |
elif type(default) is bool and not default: | |
parser.add_argument( | |
'--enable_' + arg, | |
dest=arg, | |
default=default, | |
action="store_true" | |
) | |
else: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default) | |
) | |
print parser.parse_args() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python ./register_argments.py a | |
Namespace(aaa=1, bbb='a', ccc=[1, 2, 3], ddd=True, eee=False, x='a') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
from inspect import getargspec | |
def register_arguments(parser, func): | |
args, varargs, keywords, defaults = getargspec(func) | |
while len(args) > len(defaults): | |
l = list(defaults) | |
l.insert(0, None) | |
defaults = tuple(l) | |
for arg, default in zip(args, defaults): | |
if default is None: | |
parser.add_argument(dest=arg) | |
elif type(default) is tuple or type(default) is list: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default), | |
nargs='+' | |
) | |
elif type(default) is bool and default: | |
parser.add_argument( | |
'--disable_' + arg, | |
dest=arg, | |
default=default, | |
action="store_false" | |
) | |
elif type(default) is bool and not default: | |
parser.add_argument( | |
'--enable_' + arg, | |
dest=arg, | |
default=default, | |
action="store_true" | |
) | |
else: | |
parser.add_argument( | |
'--' + arg, | |
default=default, | |
type=type(default) | |
) | |
return parser | |
def register_argparse(parser): | |
def wrapper(f): | |
register_arguments(parser, f) | |
return f | |
return wrapper | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
@register_argparse(parser) | |
def func(x, aaa=1, bbb="a", ccc=[1,2,3,], ddd=True, eee=False): | |
pass | |
print parser.parse_args() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment