将argparse参数传递给不同的python模块
我需要将用户输入从命令行传递到我编写的另一个python模块中 我编写了一个web scraper模块,该模块从公司的wiki收集信息,我的主脚本使用该wiki。我将web scraper作为一个模块导入主脚本 问题在于,当用户执行main函数并指示他们要运行web scraper时,系统会提示他们输入密码。即使他们在命令行中输入密码 在主脚本中,我将:将argparse参数传递给不同的python模块,python,Python,我需要将用户输入从命令行传递到我编写的另一个python模块中 我编写了一个web scraper模块,该模块从公司的wiki收集信息,我的主脚本使用该wiki。我将web scraper作为一个模块导入主脚本 问题在于,当用户执行main函数并指示他们要运行web scraper时,系统会提示他们输入密码。即使他们在命令行中输入密码 在主脚本中,我将: import argparse import getpass from web_scraper import web_scraper def
import argparse
import getpass
from web_scraper import web_scraper
def authenticate():
auth = get_login()
auth = str(auth).replace('(','').replace('\'','').replace(',',':').replace(')','').replace(' ','')
return auth
def arguments():
parser = argparse.ArgumentParser(description='This is a program that lists the servers in EC2')
parser.add_argument(
"-u",
"--user",
default = getpass.getuser(),
help = "Specify the username to log into Confluence")
parser.add_argument(
"-d",
"--password",
help = "Specify the user's password")
options = parser.parse_args()
return options
write_data_to_confluence(auth, html, pageid, title):
print("Stuff happens here.")
def main():
html = 'hi'
pageid = 12345678
title = 'My Title'
options = arguments()
if update_account_list.lower() == 'y':
web_scraper()
if options.password and options.user:
user = options.user
password = options.password
auth = (user, password)
write_data_to_confluence(auth, html, pageid, title)
else:
auth = authenticate()
write_data_to_confluence(auth, html, pageid, title)
def authenticate():
auth = get_login()
auth = str(auth).replace('(','').replace('\'','').replace(',',':').replace(')','').replace(' ','')
return auth
web_scraper():
print("I'm scaping the web!") # code for this function doesn't matter to the problem
def main():
web_scraper()
在web_scraper模块中,我将:
import argparse
import getpass
from web_scraper import web_scraper
def authenticate():
auth = get_login()
auth = str(auth).replace('(','').replace('\'','').replace(',',':').replace(')','').replace(' ','')
return auth
def arguments():
parser = argparse.ArgumentParser(description='This is a program that lists the servers in EC2')
parser.add_argument(
"-u",
"--user",
default = getpass.getuser(),
help = "Specify the username to log into Confluence")
parser.add_argument(
"-d",
"--password",
help = "Specify the user's password")
options = parser.parse_args()
return options
write_data_to_confluence(auth, html, pageid, title):
print("Stuff happens here.")
def main():
html = 'hi'
pageid = 12345678
title = 'My Title'
options = arguments()
if update_account_list.lower() == 'y':
web_scraper()
if options.password and options.user:
user = options.user
password = options.password
auth = (user, password)
write_data_to_confluence(auth, html, pageid, title)
else:
auth = authenticate()
write_data_to_confluence(auth, html, pageid, title)
def authenticate():
auth = get_login()
auth = str(auth).replace('(','').replace('\'','').replace(',',':').replace(')','').replace(' ','')
return auth
web_scraper():
print("I'm scaping the web!") # code for this function doesn't matter to the problem
def main():
web_scraper()
web scraper模块是一个单独的文件,其他几个模块也在使用它
我希望用户在命令行中键入密码,然后将其传递给另一个模块中的web scraper。这样用户就不必输入两次密码(一次在命令行,一次在程序中)
如何实现这一点?您拥有一切,只需通过函数调用传递
选项即可
auth = authenticate(options)