删除帮助功能中的冗余

时间:2018-08-16 17:46:27

标签: python

我可以编写参数,但是如果命令没有提供正确的参数,则无法避免编写函数“ help”来打印它。

任何建议将不胜感激。

def get_arguments():
    parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
    parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
    parser.add_argument('-l', required = False, type = str, help='Name of input file  when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
    parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format
    return parser.parse_args()


def help():
    parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
    parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
    parser.add_argument('-l', required = False, type = str, help='Name of input file  when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
    parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format 
    parser.print_help()


def main():
    args = get_arguments()  # check arguments

    if args.s and args.g:
        with open(args.g, "rU") as genome: #open(args.c + '.gbk','w') as outgbk:
            for record in SeqIO.parse(genome, "genbank"):
                if args.s == record.id:
                    #with open(args.c + '.gbk','w') as outgbk:
                    SeqIO.write([record], open(record.id + ".gbk", "w"), "genbank")
        print record.id + ".gbk created."

    else:
        help()

我希望避免在get_arguments和帮助函数中重复行。

2 个答案:

答案 0 :(得分:2)

这样编写函数get_parser

def get_parser():
    parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
    parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
    parser.add_argument('-l', required = False, type = str, help='Name of input file  when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
    parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format
    return parser

并像这样重写您的main函数:

def main():
    parser = get_parser()
    args = parser.parse_args()  # check arguments

    if args.s and args.g:
        with open(args.g, "rU") as genome: #open(args.c + '.gbk','w') as outgbk:
            for record in SeqIO.parse(genome, "genbank"):
                if args.s == record.id:
                    #with open(args.c + '.gbk','w') as outgbk:
                    SeqIO.write([record], open(record.id + ".gbk", "w"), "genbank")
        print record.id + ".gbk created."

    else:
        parser.print_help()

答案 1 :(得分:1)

我不是专家,但是如果您只是添加在两个原始函数中使用的第三个函数,名为:get_parser

,该怎么办?
def get_parser():
parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
parser.add_argument('-l', required = False, type = str, help='Name of input file  when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format
return parser

然后,两个原始函数将只有两行: 首先调用get_parser进行获取,然后解析/打印

希望这很有帮助