From e1153875d56f07fbf8421e9c885b635a41168685 Mon Sep 17 00:00:00 2001 From: banbanchs Date: Thu, 20 Mar 2014 12:55:37 +0800 Subject: [PATCH] Replace getopt with argparse --- bddown_core.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/bddown_core.py b/bddown_core.py index b8d8672..4ad5d95 100644 --- a/bddown_core.py +++ b/bddown_core.py @@ -8,7 +8,7 @@ import os import subprocess import json -import getopt +import argparse import logging from time import time @@ -162,15 +162,20 @@ class GetFilenameError(Exception): def download(args): limit = global_config.limit output_dir = global_config.dir - secret = "" - optlist, links = getopt.getopt(args, 'lDS', ['limit=', 'dir=', 'secret=']) - for k, v in optlist: - if k == '--limit': - limit = v - elif k == '--dir': - output_dir = os.path.expanduser(v) - elif k == '--secret': - secret = v + parser = argparse.ArgumentParser(description="download command arg parser") + parser.add_argument('-L', '--limit', action="store", dest='limit', help="Max download speed limit.") + parser.add_argument('-D', '--dir', action="store", dest='output_dir', help="Download task to dir.") + parser.add_argument('-S', '--secret', action="store", dest='secret', help="Retrieval password.", default="") + if not args: + parser.print_help() + exit(1) + namespace, links = parser.parse_known_args(args) + secret = namespace.secret + if namespace.limit: + limit = namespace.limit + if namespace.output_dir: + output_dir = namespace.output_dir + links = filter(check_url, links) # filter the wrong url links = map(add_http, links) # add 'http://' for url in links: