我有这样的功能 -
def my_func(my_list_arg=["one", "two"]):
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用python-fire.
我尝试了几种方法 -
尝试 1:
fire_runner.py my_func [one, two]
Run Code Online (Sandbox Code Playgroud)
导致两个单独的参数"[one"和"two]"。
尝试 2:
fire_runner.py my_func ["one", "two"]
Run Code Online (Sandbox Code Playgroud)
具有相同的结果。
尝试 3:
fire_runner.py my_func [\"one\", \"two\"]
Run Code Online (Sandbox Code Playgroud)
同样的结果。
我有一个如下所示的代码,其中包含用于各种计算的多个函数。
我使用python fire来传递参数,而不是定义 argparse 并从 cli 调用函数。每次添加新参数时,我都必须self在 init 中添加它。我正在寻找更好的方法。
我发现 python数据类可以解决这个问题。我研究了 python fire命令分组和多个命令。
class MyClass:
def __init__(
self,
input_path: str,
output_path: str = '',
same_size: bool = False,
crop_size: int = 300,
padding: int = 20,
write_json: bool = False,
write_image: bool = False,
line_thickness: int = 2,
side_color: Tuple = (255, 255, 0),
top_color: Tuple = (255, 0, 0),
) -> None:
super(MyClass, self).__init__()
self.input_path = input_path
self.output_path = …Run Code Online (Sandbox Code Playgroud) python command-line-interface python-dataclasses python-fire