google / google/python-fire

[bug] short flags/arguments does not work with `**kwargs`

Đang mở
#454 1 bình luận 0 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ả

# Description:

Short flags/arguments (e.g. `-u` for `--username`) does not work with `**kwargs`

# Example that works OK:

The following example works ok:

```python
import fire

def cli(first_arg="left", second_arg="right"):
"""Prints the 2 arguments separated by bar

Parameters
----------
first_arg : str
the first arg
second_arg : str
the second arg
"""
print(f"{first_arg} | {second_arg}" )

if __name__ == '__main__':
fire.Fire(cli)
```
## Help output
```sh
❯ python .\example.py -- --help
NAME
example.py - Prints the 2 arguments separated by bar

SYNOPSIS
example.py

DESCRIPTION
Prints the 2 arguments separated by bar

FLAGS
-f, --first_arg=FIRST_ARG
Default: 'left'
the first arg
-s, --second_arg=SECOND_ARG
Default: 'right'
the second arg
```
## Usage with short flags
```sh
❯ python .\example.py -f="hello" -s="word"
hello | word
```
# Example that does not work:

The following next example does not work with short flags, **even when they are showing on the help output**.
It is using `**kwargs`. It seems the short flags are going to the `**kwargs` dictionary.

```python
import fire

def cli(first_arg="left", second_arg="right", **kwargs):
"""Prints the 2 arguments separated by bar

Parameters
----------
first_arg : str
the first arg
second_arg : str
the second arg
kwargs : str
additional args
"""
print(f"{first_arg} | {second_arg}" )
print(kwargs)

if __name__ == '__main__':
fire.Fire(cli)
```
## Help output
```sh
❯ python .\example2.py -- --help
NAME
example2.py - Prints the 2 arguments separated by bar

SYNOPSIS
example2.py

DESCRIPTION
Prints the 2 arguments separated by bar

FLAGS
-f, --first_arg=FIRST_ARG
Default: 'left'
the first arg
-s, --second_arg=SECOND_ARG
Default: 'right'
the second arg
Additional flags are accepted.
additional args
```
## Usage with short flags
```sh
❯ python .\example2.py -f="hello" -s="word"
left | right
{'f': 'hello', 's': 'word'}
```
In summary: the `-f` and `-s` arguments should be parsing to `--first_arg` and `--second_arg` but they are parsing to `kwargs`

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.