|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
是不是非要在cmd中实现给argv传参?能不能在IDLE的shell中给argv传参?后者试了好久都不行?
例如以下代码在cmd中 键入 python sys1.py --version help 就可以运行,但是在 idle 中 shell 中键入 python sys1.py --version help 却不行?
- #!/usr/local/bin/env python
- import sys
- def readfile(filename):
- '''Print a file to the standard output.'''
- f = file(filename)
- while True:
- line = f.readline()
- if len(line) == 0:
- break
- print(line)
- f.close()
- print ("sys.argv[0]---------",sys.argv[0])
- print ("sys.argv[1]---------",sys.argv[1])
- print ("sys.argv[2]---------",sys.argv[2])
- # Script starts from here
- if len(sys.argv) < 2:
- print( 'No action specified.')
- sys.exit()
- if sys.argv[1].startswith('--'):
- option = sys.argv[1][2:]
- # fetch sys.argv[1] but without the first two characters
- if option == 'version':
-
- print ('Version 1.2')
-
- elif option == 'help':
- print('lllllll')
- '''
-            This program prints files to the standard output.
-            Any number of files can be specified.
-            Options include:
-            --version : Prints the version number
-            --help    : Display this help'''
- else:
- print ('Unknown option.')
- sys.exit()
- else:
- for filename in sys.argv[1:]:
- readfile(filename)
复制代码
|
|