Skip to content

Commit

Permalink
fix: fix parsing *args
Browse files Browse the repository at this point in the history
  • Loading branch information
likianta committed Jan 2, 2024
1 parent b539962 commit 4f71b7e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 40 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

---

### 0.5.5 (2024-01-02)

- 添加 `:empty` 来传递空字符串
- 修复对 `*args` 的处理

### 0.5.4 (2023-11-03)

- 修复 args 参数无法通过 kwargs 语法传递的问题
Expand Down
114 changes: 77 additions & 37 deletions argsense/parser/func_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ class T:
'kwargs': t.List[t.Tuple[ParamName, PlainParamType, t.Any]],
'return': PlainParamType, # noqa
})
''' e.g.
{
'name': 'run',
'args': [('appid', 'str'), ('*', 'list')],
'kwargs': [('_version', 'str', None), ('**', 'dict', {})],
'return': 'none',
}
'''


class FuncInfo:
Expand All @@ -49,41 +57,71 @@ class FuncInfo:
target: t.Callable
transport_help: bool # FIXME: temp solution

@classmethod
def global_kwargs(cls) -> T.KwArgsInfo:
return { # noqa
':help' : {
'cname' : '--help',
'ctype' : ParamType.FLAG,
'desc' : 'show help message and exit',
'default' : False, # False means `not explicitly set by user`.
# for example, when user inputs in command line:
# `argsense xxx.py -h` # -> True
# `argsense xxx.py` # -> False
},
':helpx': {
'cname' : '--helpx',
'ctype' : ParamType.FLAG,
'desc' : 'expand all command helps',
'default' : False,
},
}
GLOBAL_CNAME_2_NAME: t.Dict[str, str] = {
'--help' : ':help',
'--helpx' : ':helpx',
'-h' : ':help',
'-hh' : ':helpx',
# DELETE: below are deprecated
'--:help' : ':help',
'--:helpx': ':helpx',
'-:h' : ':help',
'-:hh' : ':helpx',
}
GLOBAL_KWARGS: T.KwArgsInfo = { # noqa
':help' : {
'cname' : '--help',
'ctype' : ParamType.FLAG,
'desc' : 'show help message and exit',
'default': False, # False means `not explicitly set by user`.
# for example, when user inputs in command line:
# `argsense xxx.py -h` # -> True
# `argsense xxx.py` # -> False
},
':helpx': {
'cname' : '--helpx',
'ctype' : ParamType.FLAG,
'desc' : 'expand all command helps',
'default': False,
},
}

@classmethod
def global_cname_2_name(cls) -> t.Dict[str, str]:
return {
'--help' : ':help',
'--helpx' : ':helpx',
'-h' : ':help',
'-hh' : ':helpx',
# DELETE: below are deprecated
'--:help' : ':help',
'--:helpx': ':helpx',
'-:h' : ':help',
'-:hh' : ':helpx',
}
# @classmethod
# def global_kwargs(cls) -> T.KwArgsInfo:
# return { # noqa
# ':help' : {
# 'cname' : '--help',
# 'ctype' : ParamType.FLAG,
# 'desc' : 'show help message and exit',
# 'default' : False, # False means `not explicitly set by user`.
# # for example, when user inputs in command line:
# # `argsense xxx.py -h` # -> True
# # `argsense xxx.py` # -> False
# },
# ':helpx': {
# 'cname' : '--helpx',
# 'ctype' : ParamType.FLAG,
# 'desc' : 'expand all command helps',
# 'default' : False,
# },
# }
#
# @classmethod
# def global_cname_2_name(cls) -> t.Dict[str, str]:
# return {
# '--help' : ':help',
# '--helpx' : ':helpx',
# '-h' : ':help',
# '-hh' : ':helpx',
# # DELETE: below are deprecated
# '--:help' : ':help',
# '--:helpx': ':helpx',
# '-:h' : ':help',
# '-:hh' : ':helpx',
# }

def __init__(self, info: T.RawInfo):
# print(info, ':lv')
from ..converter import name_2_cname
from ..converter import type_2_ctype

Expand All @@ -92,14 +130,16 @@ def __init__(self, info: T.RawInfo):
self.desc = ''
# self.return_type = info['return']
self.args = {}
self.kwargs = FuncInfo.global_kwargs().copy()
self.cname_2_name = FuncInfo.global_cname_2_name().copy()
self.kwargs = FuncInfo.GLOBAL_KWARGS.copy()
self.cname_2_name = FuncInfo.GLOBAL_CNAME_2_NAME.copy()
self.transport_help = False

for name, type in info['args']:
cname = name_2_cname(name, style='arg')
self._append_cname(name, cname)
self._append_cname(name, name_2_cname(name, style='opt')) # alias
if cname != '*':
# alias of "option" style
self._append_cname(name, name_2_cname(name, style='opt'))
self.args[name] = {
'cname': cname,
'ctype': type_2_ctype(type),
Expand All @@ -123,8 +163,8 @@ def local_kwargs(self) -> T.KwArgsInfo:
def _append_cname(self, name: str, cname: str) -> None:
assert cname not in self.cname_2_name, (
f'duplicate cname: `{cname}` (for `{name}`). '
f'make sure you have not defined `xxx`, `_xxx` and `xxx_` '
f'(or something like this) in the same function.'
f'make sure you have not defined parameters like `xxx`, `_xxx` and '
f'`xxx_` in the same function.'
)
self.cname_2_name[cname] = name

Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "argsense"
version = "0.5.5a0"
version = "0.5.5"
homepage = "https://github.com/likianta/argsense-cli"
description = "New command line interface based on Python Rich library."
readme = "README.md"
Expand All @@ -11,15 +11,15 @@ license = "MIT"
python = "^3.8"
pysimplegui = { version = "*", optional = true }
rich = "*"
textual = "*"
# TODO: trogon = "*"
textual = "*"

[tool.poetry.extras]
gui = ["pysimplegui"] # pip install argsense[gui]

[[tool.poetry.source]]
name = "tsinghua"
url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/"
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
priority = "default"

[tool.poetry.scripts]
Expand Down

0 comments on commit 4f71b7e

Please sign in to comment.