import argparse def main(): parser = argparse.ArgumentParser(description="Demo of argparse") parser.add_argument('-n', dest='name', default='Li') parser.add_argument('-y', dest='year', default='20') args = parser.parse_args() print(args) name = args.name year = args.year print('Hello {} {}'.format(name,year)) if __name__ == '__main__': main()
运行:python test_parse.py
输出:
Namespace(name=' Li ', year='20')
Hello Li 20
运行:python test_parse.py -n panda -y 22
输出:
Namespace(name='panda', year='22')
Hello panda 22
转载:https://blog.csdn.net/yy_diego/article/details/82851661