如何阻止argparser打印默认搜索?

时间:2018-02-11 01:12:46

标签: python-3.x command-line-interface youtube-data-api

链接到我的代码:https://pastebin.com/y4zLD2Dp 在我进行项目的过程中,将使用未使用的导入,我只想拥有我需要准备好的所有导入。该程序的目标是首先将YouTube视频下载器转换为mp3格式。这是我的第一个符合我标准的大项目,仅编码了2个多月。

from bs4 import BeautifulSoup
from selenium import webdriver
import requests
import sqlite3
import argparse
import sys
from selenium import webdriver


from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser

#To get a developer key visit https://console.developers.google.com.
DEVELOPER_KEY = ""
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

'''Searches for results on youtube and stores them in the database.'''
def yt_search(options):
    youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    developerKey=DEVELOPER_KEY)

    search = youtube.search().list(q=options.q, part="id,snippet", 
                         maxResults=options.max_results).execute()

    video_id = []

    #Add results to a list and print them.
    for result in search.get("items", []):
        if result["id"]["kind"] == "youtube#video":
            video_id.append("%s (%s)" % (result["snippet"]["title"],
                                    result["id"]["videoId"]))
        else:
            continue
    print("Videos:\n", "\n".join(video_id), "\n")

def download(args):
    print(args)  

"""Arguments for the program."""
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description= "This program searches for 
          Youtube links and allows you to download songs from said list. " +
                                 "Please remember to be specific in your 
                                           searches for best results.")

    parser.add_argument("--q", help="Search term", default="Aeryes")
    parser.add_argument("--max-results", help="Max results", default=25)

    parser.add_argument("--d", type=download, help="Download a video from 
                        search results.")

    args = parser.parse_args()

    if len(sys.argv) < 2:
        parser.parse_args(['--help'])
        sys.exit(1)

    try:
        yt_search(args)
    except HttpError:
        print("HTTP error")

我遇到的问题是,在CLI中运行--d cmd时它会工作并按预期打印arg(这只是一个测试,看看函数是否正在使用解析器)但是在打印出来之后来自--q default的默认youtube链接列表,我不希望它这样做。我该如何阻止这种情况发生。我应该使用subparser还是有些东西我不知道?

如果有人为argparser模块提供了除官方文档支持以外的良好资源,请分享。

0 个答案:

没有答案