Python代码: maximum command line parameter length? 命令行参数可以有多长?
01 #how long can a command line parameter have?
02 #
03 # Directly answer is 8196 on Window7(not sure about other platform)
04 #
05 #It’s easy to write a script to repro it:
06 #1. In one script, output the parameter it got, say script#1
07 #2. In another script, recursively execute script#1 and increase parameter step by step,
08 # capture the output, see if you have the same with the parameter you give
09 #
10 # Usage: python test2.py
11 # then you got the result
12 # (if it's 10000, then adjust "end" variable below)
13 #
14 #--- test.py ---
15 import sys
16 print sys.argv[1]
17
18 #--- test2.py ---
19 import sys,os
20 param = 'a'
21 start = 0
22 end = 10000
23 while start < end: #binary search to speed up
24 mid = (start + end) / 2
25 param = 'a' * mid
26 cmdline = 'test.py ' + param
27 out = os.popen(cmdline).read().rstrip()
28 if param == out:
29 start = mid + 1
30 else:
31 end = mid - 1
32 print len(param)
33 print "\nMax Command Line Length=", start
34
35 #What I got on Window7 is: 8153
36 # considering cmd.exe starts with "C:\\Windows\\system32\\cmd.exe /c test.py "
37 # then the maximum is about 8196
38 #
39 #If you want official answer, it's here: http://support.microsoft.com/kb/830473
40 #