-
-
Save agusmakmun/cabb6b87a15a261ec0f73ab3e53bb1d1 to your computer and use it in GitHub Desktop.
argparse example
This file contains hidden or 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
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