简体   繁体   English

转换为argparse时,字符串索引超出范围

[英]String index out of range when converting to argparse

I changed from optparse to argparse but when I try to run it I get the following error: 我从optparse更改为argparse,但是当我尝试运行它时,我收到以下错误:

    if not option_string[0] in self.prefix_chars:
IndexError: string index out of range

My code is: 我的代码是:

usage = "%prog -f <fasta TFs> -a <database all> -s <database small> -d <pfam database> [options]"
version = "1.0.1"
description = " "
epilog = " "\
         " "
parser = argparse.ArgumentParser(usage=usage, description=description,
                      version="%prog "+version, epilog=epilog)

# options for running the program
# TF file
parser.add_argument("-f", "",  dest="TF", metavar="<file>",
                        help="input file with transcription factors")
parser.set_defaults(fasta=None)

I can't find where this error comes from, how can fix this? 我无法找到此错误的来源,如何解决这个问题?

In argparse you can't pass empty argument strings to add_argument. 在argparse中,您无法将空参数字符串传递给add_argument。 argparse is trying to find a valid prefix_char (eg "-" or "--") in the empty string you pass (""), causing the error. argparse试图在您传递的空字符串(“”)中找到有效的prefix_char(例如“ - ”或“ - ”),从而导致错误。 Try this instead: 试试这个:

parser.add_argument("-f",  dest="TF", metavar="<file>",
                    help="input file with transcription factors")

获取此错误的唯一方法是请求不存在的索引 - 在这种情况下, option_string必须为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM