我在Python中编写了脚本A,它使用ArgumentParser并在从cmd行运行时切换到argparse。现在,我需要编写第二个脚本,它使用A中的函数,但它有自己的解析器,用于它自己的输入
脚本中的我的argparse函数:
def switches():
filepath = os.path.dirname(os.path.abspath(__file__))
parser = argparse.ArgumentParser(prog='log-analyser')
parser.add_argument('-ch', '--channels', help='desired number of output channels. 0 here will terminate the script.', default=2, type=int)
parser.add_argument('-r', '--ranges', nargs='?', help='input the ranges limits. use square brackets format [a,b,c,....,n] to get a-b, b-c, c-d, (...) ranges. DONT USE SPACES! frames beyond the limits will not be checked.')
parser.add_argument('-m', '--margins', nargs='?', help='input the margins for stated ranges. use square brackets format [x,y,z,...,xz] to get x for a-b, y for b-c etc. DONT USE SPACES! (r-1) margins needed. ')
parser.add_argument('-log', '--logfilepath', default='log1', help='filename of input_logfile to process (logfile for pcmtool supported). could be whole path to input_logfile.')
parser.add_argument('-pre', '--prefix', default='', help='output file prefix and name of folder for outputs. optional; if left empty, program will output files with input_logfile prefix (e.g. log1_errors_ch1.txt).')
parser.add_argument('-outdir', '--outputpath', default=filepath, help='path for every output for this program. .py file dir by default.')
parser.add_argument('--marginsfilename', default='margins.txt', help='name of margin file to process. optional; if file exists, program inputs ranges and margins from this file. if file doesnt exist, it will be created, but correct -r and -m data is required.')
return parser.parse_args()
我需要这个脚本中的其他几个函数,例如这个:
def convert_to_floatlist(string_input):
out = string_input
out = out.replace('[', '')
out = out.replace(']', '')
out = out.split(',')
for i in range (0, len(out)):
out[i] = float(out[i])
return out
但是,当我在第二个脚本中调用
时from log_analyser import convert_to_floatlist
我从脚本B获得的是从脚本A
中检查失败的参数>> Please use -r & -m switches or specify correct marginfile directory. Program will now terminate.
有解决方法吗?我不需要来自A的开关(),但是我需要其他几个函数,我想在B中进行argparse。