python args参数处理
2020-06-08 本文已影响0人
逍遥_yjz
1
#coding:utf-8
import argparse
def test2(name,age):
print('今天天气不错')
print(name)
print(age)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='测试接受参数')
parser.add_argument('-n', help='姓名输入.')
parser.add_argument('-a', help='年龄输入.')
args = parser.parse_args()
print(type(args),args)
name = args.n
print(type(name))
age = args.a
print(type(age),age)
test2(name,age)
python3 testArgs.py -n "小明" -a 28
<class 'argparse.Namespace'> Namespace(a='28', n='小明')
<class 'str'>
<class 'str'> 28
今天天气不错
小明
28
2
#coding:utf-8
import argparse
import sys
def test2(name,age):
print('今天天气不错')
print(name)
print(age)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='测试接受参数')
parser.add_argument('-n', help='姓名输入.')
parser.add_argument('-a', help='年龄输入.')
# args = parser.parse_args()
# print(type(args),args)
# name = args.n
# print(type(name))
# age = args.a
# print(type(age),age)
#print(args[0],'234',args[1],'34',args[2])
str1 = sys.argv[0]
name = sys.argv[1]
age = sys.argv[2]
print(str1)
test2(name,age)
python3 testArgs.py "小明" 28
testArgs.py
今天天气不错
小明
28