def setopts(args): ''' 根据命令行输入的参数修改全局变量 :param args: 命令行参数列表 :return: ''' try: opts, others = getopt.getopt( args, 'vhk:s:c:o:', ['verbose', 'help', 'keyword=', 'source=', 'count=', 'outdir=']) except getopt.GetoptError as e: logger.error('命令解析失败') logger.error(e) echo.usage() sys.exit(2) glovar.init_option() for o, a in opts: if o in ('-h', '--help'): echo.usage() sys.exit(2) elif o in ('-v', '--verbose'): glovar.set_option('verbose', True) elif o in ('-k', '--keyword'): glovar.set_option('keyword', a) elif o in ('-s', '--source'): glovar.set_option('source', a) elif o in ('-c', '--count'): glovar.set_option('count', int(a)) elif o in ('-o', '--outdir'): glovar.set_option('outdir', a) else: assert False, 'unhandled option'
glovar.get_option('keyword')) except Exception as e: logger.error('Get %s music list failed.' % source.upper()) logger.error(e) if glovar.get_option('merge'): # 对搜索结果排序和去重 music_list = music_list_merge(music_list) echo.menu(music_list) choices = input('请输入要下载的歌曲序号,多个序号用空格隔开:') downloadByIndexList(choices.split(), music_list) # 下载完后继续搜索 keyword = input('请输入要搜索的歌曲,或Ctrl+C退出:\n > ') glovar.set_option('keyword', keyword) main() if __name__ == '__main__': # 初始化全局变量 glovar.init_option() if len(sys.argv) > 1: setopts(sys.argv[1:]) try: main() except KeyboardInterrupt: sys.exit(0)