我发现argparse在参数前添加了额外的空间。基于下面Python argparse在参数前添加额外空间
def parse_arguments():
parser = argparse.ArgumentParser(
prog='sample',
description='bla bla',
parser.add_argument('-s', '--search', dest='pattern', required=True,
help='search path pattern (e.g. /dir1/dir2/*.ext)')
args = parser.parse_args()
if args.pattern[0] == ' ':
print "One space is added to the argument"
return args
示例代码和测试在交互式shell为:
import sys
sys.argv = ['', '-s /Users/user/Desktop/test']
execfile('test.py')
提供自变量sys.argv = ['', '-s=/Users/user/Desktop/test']
不会引起这样的加(从https://stackoverflow.com/a/36376287/2101864激励)。
这是记录的行为还是我错过了什么?因为通常用户提供的参数在参数标记和值之间添加空格。
谢谢@aneroid您的详细说明。 –