简体   繁体   English

将参数传递给命令组,python fire

[英]Pass arguments to command groups, python fire

How can I pass the arguments val and code to the group get如何将参数valcode传递给组get

  • Code so far到目前为止的代码

import fire

class Get:

    def __init__(self, val="no", code=""):

        self.val = val
        self.code = code


    def get(self):
        return f"called get {self.val} {self.code}"

    def get_many(self):
        return f"called get many {self.val}"


class Pipeline:

    def __init__(self,):
        self.get = Get()

def main():
    fire.Fire(Pipeline)
  • The error I Got我得到的错误

$> my_p get --code="test" --val="yes" get

ERROR: Could not consume arg: --code=test
Usage: my_p get get <command>

according to https://github.com/google/python-fire/blob/master/docs/guide.md#version-4-firefireclass根据https://github.com/google/python-fire/blob/master/docs/guide.md#version-4-firefireclass

you need to change :你需要改变:

self.get = Get

Besides, I recommend to not use the same name in "get" function in class Get.此外,我建议不要在 Get 类的“get”函数中使用相同的名称。 It is confusing.这令人困惑。

import fire


class Get(object):
    def __init__(self, attr):
        self.attr = attr

    def get(self):
        return f"called get {self.attr.val} {self.attr.code}"

    def get_many(self):
        return f"called get many {self.attr.val}"


class Pipeline(object):
    def __init__(self, **kwargs):
        for key, val in kwargs.items():
            self.__dict__[key] = kwargs.get(key, val)
        self.get = Get(self)


if __name__ == "__main__":
    fire.Fire(Pipeline)

output输出

python my_p.py get --code="test" --val="yes" get
called get yes test

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

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