Argparse 子解析器,并链接到类

发布于 2024-11-24 19:43:16 字数 1504 浏览 1 评论 0原文

我们有一个简单的Python程序来管理各种类型的内部服务器,使用argparse:

manage_servers.py <operation> <type_of_server> 

操作是检查、构建、部署、配置、验证等。

服务器类型只是我们使用的不同类型的内部服务器。

我们有一个通用服务器类,然后是从该类继承的特定类型:

class Server
    def configure_logging(self, loggin_file):
        ...
    def check(self):
        ...
    def deploy(self):
        ...
    def configure(self):
        ...
    def __init__(self, hostname):
        self.hostname = hostname
        logging = self.configure_logging(LOG_FILENAME)
class SpamServer(Server):
    def check(self):
        ...
class HamServer(Server):
    def deploy(self):
        ...

我的问题是如何将所有这些链接到 argparse?

最初,我使用 argparse 子解析来执行操作(检查、构建、部署),并使用另一个参数来表示类型。

subparsers = parser.add_subparsers(help='The operation that you want to run on the server.')
parser_check = subparsers.add_parser('check', help='Check that the server has been setup correctly.')
parser_build = subparsers.add_parser('build', help='Download and build a copy of the execution stack.')
parser_build.add_argument('-r', '--revision', help='SVN revision to build from.')
...
parser.add_argument('type_of_server', action='store', choices=types_of_servers,
                    help='The type of server you wish to create.')

通常,您会将每个子解析链接到一个方法 - 然后传入 type_of_server 作为参数。然而,由于类的原因,这稍微落后了——我需要创建适当的 Server 类的实例,然后调用其中的操作方法。

我有什么想法可以实现上述目标吗?也许服务器有不同的设计模式?或者仍然按原样使用 argparse 的方法?

干杯, 胜利者

We have a simple Python program to manage various types of in-house servers, using argparse:

manage_servers.py <operation> <type_of_server> 

Operations are things like check, build, deploy, configure, verify etc.

Types of server are just different types of inhouse servers we use.

We have a generic server class, then specific types that inherit from that:

class Server
    def configure_logging(self, loggin_file):
        ...
    def check(self):
        ...
    def deploy(self):
        ...
    def configure(self):
        ...
    def __init__(self, hostname):
        self.hostname = hostname
        logging = self.configure_logging(LOG_FILENAME)
class SpamServer(Server):
    def check(self):
        ...
class HamServer(Server):
    def deploy(self):
        ...

My question is how to link that all up to argparse?

Originally, I was using argparse subparses for the operations (check, build, deploy) and another argument for the type.

subparsers = parser.add_subparsers(help='The operation that you want to run on the server.')
parser_check = subparsers.add_parser('check', help='Check that the server has been setup correctly.')
parser_build = subparsers.add_parser('build', help='Download and build a copy of the execution stack.')
parser_build.add_argument('-r', '--revision', help='SVN revision to build from.')
...
parser.add_argument('type_of_server', action='store', choices=types_of_servers,
                    help='The type of server you wish to create.')

Normally, you'd link each subparse to a method - and then pass in the type_of_server as an argument. However, that's slightly backwards due to the classes- I need to create an instance of the appropriate Server class, then call the operation method inside of that.

Any ideas of how I could achieve the above? Perhaps a different design pattern for Servers? Or a way to still use argparse as is?

Cheers,
Victor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-12-01 19:43:16

只需使用 parser.add_subparsers(dest=... 参数将 type_of_server 映射到类:

subparsers = parser.add_subparsers(dest='operation', help='The operation that you want to run on the server.')
...
server_types = dict(spam=SpamServer, ham=HamServer)
args = parser.parse_args()
server = server_types[args.type_of_server]()
getattr(server, args.operation)(args)

Just use the parser.add_subparsers(dest=... argument with a mapping of type_of_server to classes:

subparsers = parser.add_subparsers(dest='operation', help='The operation that you want to run on the server.')
...
server_types = dict(spam=SpamServer, ham=HamServer)
args = parser.parse_args()
server = server_types[args.type_of_server]()
getattr(server, args.operation)(args)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文