【发布时间】:2013-12-10 21:29:18
【问题描述】:
我有一个简单的实用程序脚本,用于下载给定 URL 的文件。它基本上只是 Linux 二进制“aria2c”的包装器。
这是名为getFile的脚本:
#!/usr/bin/python
#
# SCRIPT NAME: getFile
# PURPOSE: Download a file using up to 20 concurrent connections.
#
import os
import sys
import re
import subprocess
try:
fileToGet = sys.argv[1]
if os.path.exists(fileToGet) and not os.path.exists(fileToGet+'.aria2'):
print 'Skipping already-retrieved file: ' + fileToGet
else:
print 'Downloading file: ' + fileToGet
subprocess.Popen(["aria2c-1.8.0", "-s", "20", str(fileToGet), "--check-certificate=false"]).wait() # SSL
except IndexError:
print 'You must enter a URI.'
例如,这个命令会下载一个文件:
$ getFile http://upload.wikimedia.org/wikipedia/commons/8/8e/Self-portrait_with_Felt_Hat_by_Vincent_van_Gogh.jpg
我想要做的是允许一个可选的第二个参数(在 URI 之后),它是一个带引号的字符串。该字符串将是下载文件的新文件名。因此,下载完成后,文件将根据第二个参数重命名。使用上面的例子,我希望能够输入:
$ getFile http://upload.wikimedia.org/wikipedia/commons/8/8e/Self-portrait_with_Felt_Hat_by_Vincent_van_Gogh.jpg "van-Gogh-painting.jpg"
但我不知道如何将带引号的字符串作为可选参数。我该怎么做?
【问题讨论】:
标签: python command-line-arguments argument-passing