如果用户未传递参数,我如何让argparse不接受任何参数,而是使用变量作为参数(Python)

时间:2019-01-11 15:03:21

标签: python python-2.7 parameters arguments argparse

如果用户未传递参数,我如何使日志解析器不接受任何参数,并使用存储在log变量中的生成的日志。

    #####################
    #LogFile
    #####################
    log =(sys.argv[2])
    logging.basicConfig(filename=log ,level=logging.DEBUG)

    if log == "-p" or log == "--path":
        TIME_STAMPS = re.split('\s+', time.strftime('%Y%m%d %H%M%S %H:%M:%S', time.localtime()))
        FILE_TIME_STAMP = TIME_STAMPS[0] + "_" + TIME_STAMPS[1]
        log = "version" + "_" + FILE_TIME_STAMP + "_ValidLinkCheck.log"
        logging.basicConfig(filename=log ,level=logging.DEBUG)
    ########################
    #Command Line Options
    ########################
    #initiate the parser
    parser = argparse.ArgumentParser()
    parser.add_argument('-l','--log', dest='log', default = 'log1' , help = "Log File", action="store_true", required= False)
    parser.add_argument("-p", "--path", action="store", help="path to file", dest="filename")
    parser.add_argument("-x", "--execute", help="delete invalid link", action="store_true")

    #read arguments from the command line
    args = parser.parse_args()

    #check for --path or -p
    if args.filename:
        print("Script is up to date and running.......................")
        logging.info("Script is up to date and running.......................")
        path = (format(args.filename)) 
        logging.info("Searching for input link" + path)
        status, result = commands.getstatusoutput('cd ' + path)

0 个答案:

没有答案