Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python argparse模块-如何在运行时更改帮助格式?_Python_Runtime_Argparse - Fatal编程技术网

Python argparse模块-如何在运行时更改帮助格式?

Python argparse模块-如何在运行时更改帮助格式?,python,runtime,argparse,Python,Runtime,Argparse,比如说,我有一个解析器: self.__parser = argparse.ArgumentParser( prog = '<...>', fromfile_prefix_chars='@') 不起作用,因为argparser在ts格式化程序中缓存此prog somwhere。 有人知道是否可以用简单的方法更改此属性吗?我怀疑问题出在代码的其他地方,因为下面的代码可以更改

比如说,我有一个解析器:

self.__parser = argparse.ArgumentParser(
                            prog = '<...>',
                            fromfile_prefix_chars='@')
不起作用,因为argparser在ts格式化程序中缓存此prog somwhere。
有人知道是否可以用简单的方法更改此属性吗?

我怀疑问题出在代码的其他地方,因为下面的代码可以更改prog属性,这可以通过调用print\u help来证明:

import argparse
import sys

class MyParser():
    def __init__(self, nm=sys.argv[0]):
        self.__parser = argparse.ArgumentParser(prog=nm, fromfile_prefix_chars='@')
    def change_prog_name(self, nm):
        self.__parser.prog = nm
    def print_help(self):
        self.__parser.print_help()

my_parser = MyParser()
my_parser.print_help()
print 'after prog change:'
my_parser.change_prog_name('aaa')
my_parser.print_help()
输出:

用法:argparse_test.py[-h]

可选参数:
-h,--help显示此帮助消息并退出

程序更改后:
用法:aaa[-h]

可选参数:
-h,--help显示此帮助消息并退出

import argparse
import sys

class MyParser():
    def __init__(self, nm=sys.argv[0]):
        self.__parser = argparse.ArgumentParser(prog=nm, fromfile_prefix_chars='@')
    def change_prog_name(self, nm):
        self.__parser.prog = nm
    def print_help(self):
        self.__parser.print_help()

my_parser = MyParser()
my_parser.print_help()
print 'after prog change:'
my_parser.change_prog_name('aaa')
my_parser.print_help()