google / google/python-fire

default mode without requiring named arguments

Đang mở
#331 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
28.2k
Fork
1.5k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Surprised if this is not a dup, or documented - but I couldn't find it.

In most of my scripts I have a default behavior, and keyword modifiers, e.g.

```
# Default behavior is adding numbers
# a)
python script 1 2
-> 3

# modified behavior is subtracting numbers
# b)
python script subtract 1 2
-> -2
```

right now I can only get Fire to support keyword behavior, so a) would be
```
python script add 1 2
-> 3

```

I tried writing it all in a class, where the default is the \_\_call\_\_ method:

```
from fire import Fire

@Fire
class script:
@staticmethod
def subtract(a: int, b: int) -> int:
return a - b

@staticmethod
def __call__(a: int, b: int) -> int:
return a + b
```

but in this case I'm only able to call #a it as

```
python script --a=1 --b=2
```

I could of course manually process args, but at that point I might as well just use argparse.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.