argparse
分为 必写属性 可选属性
可以设置默认值 范围 等等,e.g., 默认为0,范围必须在0~150
example 1
import argparse
# https://zhuanlan.zhihu.com/p/34395749
def cmd():
args = argparse.ArgumentParser(description = 'Personal Information ',epilog = 'Information end ')
#必写属性,第一位
args.add_argument("name", type = str, help = "Your name")
#必写属性,第二位
args.add_argument("birth", type = str, help = "birthday")
#可选属性,默认为None
args.add_argument("-r",'--race', type = str, dest = "race", help = u"民族")
#可选属性,默认为0,范围必须在0~150
args.add_argument("-a", "--age", type = int, dest = "age", help = "Your age", default = 0, choices=range(150))
#可选属性,默认为male
args.add_argument('-s',"--sex", type = str, dest = "sex", help = 'Your sex', default = 'male', choices=['male', 'female'])
#可选属性,默认为None,-p后可接多个参数
args.add_argument("-p","--parent",type = str, dest = 'parent', help = "Your parent", default = "None", nargs = '*')
#可选属性,默认为None,-o后可接多个参数
args.add_argument("-o","--other", type = str, dest = 'other', help = "other Information",required = False,nargs = '*')
args = args.parse_args() #返回一个命名空间,如果想要使用变量,可用args.attr
print("argparse.args=",args,type(args))
print('name = %s'%args.name)
d = args.__dict__
for key,value in d.items():
print('%s = %s'%(key,value))
if __name__=="__main__":
cmd()
python args1.py ltt 1996
argparse.args= Namespace(age=0, birth='1996', name='ltt', other=None, parent='None', race=None, sex='male') <class 'argparse.Namespace'>
name = ltt
name = ltt
birth = 1996
race = None
age = 0
sex = male
parent = None
other = None
example 2
import argparse
# https://www.jianshu.com/p/b8b09084bd1a
parser = argparse.ArgumentParser()
parser.add_argument("--echo", type=str, help="echo the string you use here")
parser.add_argument("--square", type=int, help="display a square of a given number")
args = parser.parse_args()
print(args.echo)
print(args.square**2)
python args2.py --echo ok --square 6
ok
36
yaml
refer to PC-GNN