Skip to content

Instantly share code, notes, and snippets.

@yuuichi-fujioka
Created April 17, 2013 07:47
Show Gist options
  • Save yuuichi-fujioka/5402498 to your computer and use it in GitHub Desktop.
Save yuuichi-fujioka/5402498 to your computer and use it in GitHub Desktop.
argparse example
import argparse
def exist_user(string):
exist_users = ['taro','jiro','saburo','ichi','ni','san']
if string not in exist_users:
raise argparse.ArgumentTypeError("%s is not exist user"%(string))
return string
parser = argparse.ArgumentParser(prog='test command', description='this is description text.')
parser.add_argument('foo', dest='foo', help='hogehoge')
parser.add_argument('--hoge', dest='hoge', nargs='+', help='hogehoge')
parser.add_argument('--fuga', dest='fuga', nargs='*', help='hogehoge')
parser.add_argument('--scot', dest='scot', nargs='?', help='hogehoge')
parser.add_argument('--asdf', dest='asdf', choices=['aaa','bbb','ccc'])
parser.add_argument('-n', '--name', dest='name', help='your name', required=True)
parser.add_argument('--x',action='store_true')
parser.add_argument('--age', dest='age', choices=range(0,10), nargs=1, default=123, help='your age',type=int)
parser.add_argument('--aiu', dest='aiu',metavar='mmmmm', default=923 ,type=int)
parser.add_argument('--file', '-f', dest='file', type=file)
parser.add_argument('--parent', dest='parent', type=exist_user)
args = parser.parse_args()
print args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment