Skip to content

Commit c00073b

Browse files
author
tuntun
committed
Python3命令行
1 parent ae18bec commit c00073b

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

sys_argv.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import optparse
88
import sys
9+
import webbrowser
910

1011

1112
def process_command_line(argv):
@@ -23,6 +24,9 @@ def process_command_line(argv):
2324
parser.add_option( # customized description; put --help last
2425
'-h', '--help', action='help',
2526
help='Show this help message and exit.')
27+
parser.add_option(
28+
'-u', '--url', action='store',
29+
help='Open a link.')
2630
settings, args = parser.parse_args(argv)
2731
# check number of arguments, verify values, etc.:
2832
if args:
@@ -34,9 +38,24 @@ def process_command_line(argv):
3438
def main(argv=None):
3539
settings, args = process_command_line(argv)
3640
# application code here, like:
37-
# run(settings, args)
41+
run(settings, args)
3842
return 0 # success
3943

44+
def run(settings, args):
45+
if settings.url:
46+
webbrowser.open(settings.url, 1)
47+
48+
4049
if __name__ == '__main__':
4150
status = main()
42-
sys.exit(status)
51+
sys.exit(status)
52+
53+
"""
54+
命令行功能:
55+
56+
python sys_argv.py -h
57+
显示帮助信息
58+
59+
python sys_argv.py -u www.163.com
60+
用默认浏览器打开你输入的网址
61+
"""

0 commit comments

Comments
 (0)