Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
BodyLabel,
FluentIcon,
FluentIconBase,
LineEdit,
MessageBoxBase,
PlainTextEdit,
RoundMenu,
Expand Down Expand Up @@ -479,8 +480,9 @@ class PythonCodeEditorDialog(BaseCodeEditorDialog):
"""Python 代码编辑器弹窗"""

def __init__(self, parent=None, title: str = "Python 脚本编辑器",
initial_code: str = "", script_path: str = ""):
initial_code: str = "", script_path: str = "", script_arguments: str = ""):
self._script_path = script_path
self._script_arguments = script_arguments
super().__init__(parent=parent, title=title,
placeholder="# 输入 Python 脚本",
initial_code=initial_code)
Expand All @@ -497,8 +499,23 @@ def __init__(self, parent=None, title: str = "Python 脚本编辑器",

def _setup_editor_layout(self) -> None:
self.highlighter = PythonHighlighter(self.editor.document())
# 设置启动参数
self._setup_arguments_layout()
self.viewLayout.addWidget(self.editor)

def _setup_arguments_layout(self) -> None:
"""设置启动参数的布局"""
args_layout = QHBoxLayout()
args_label = BodyLabel("启动参数: ")
self.args_input = LineEdit()
self.args_input.setPlaceholderText("请输入脚本启动参数")
self.args_input.setText(self._script_arguments)
args_layout.addWidget(args_label)
args_layout.addWidget(self.args_input)

self.viewLayout.addLayout(args_layout)
self.viewLayout.addSpacing(8)

def _on_external_edit(self) -> None:
if not self._script_path:
return
Expand All @@ -513,6 +530,10 @@ def _on_external_edit(self) -> None:
def get_code(self) -> str:
return self.editor.toPlainText()

def get_arguments(self) -> str:
"""获取填写的 Python 脚本启动参数"""
return self.args_input.text()


class JsonHighlighter(QSyntaxHighlighter):
""" JSON 语法高亮器 """
Expand Down
4 changes: 4 additions & 0 deletions src/script_chainer/gui/page/script_setting_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,13 @@ def on_edit_clicked(self) -> None:
title='Python 脚本',
initial_code=code,
script_path=path,
script_arguments=self.config.script_arguments,
)
if dialog.exec():
self.chain_config.save_python_script(self.config.idx, dialog.get_code())
# 保存用户输入的脚本启动参数
self.config.script_arguments = dialog.get_arguments()
self.value_changed.emit(self.config)
self._update_display()

def _update_display(self) -> None:
Expand Down
16 changes: 13 additions & 3 deletions src/script_chainer/win_exe/script_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ def _on_stdout(line: str) -> None:
return _on_stdout


def _get_args_list(script_arguments: str | None) -> list[str] | None:
"""获取解析后的脚本启动参数列表"""
args_list = None
if script_arguments and script_arguments.strip():
args_list = shlex.split(script_arguments, posix=False)
return args_list
Comment on lines +201 to +206

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

Does Python shlex.split with posix=False retain quote characters in the resulting tokens?

💡 Result:

No, Python's shlex.split does not retain quote characters in the resulting tokens, regardless of whether posix is set to True or False [1][2]. The shlex module is designed to perform lexical analysis similar to a Unix shell [2]. In both POSIX and non-POSIX modes, the parser is configured to identify, process, and strip quote characters used to encapsulate tokens [2]. When you pass posix=False, you are selecting a non-POSIX compatibility mode that may change how specific characters are handled—such as how escapes or specific punctuation are treated—but it does not change the core behavior of stripping the quotes themselves [1][2]. If your goal is to preserve quote characters in the output, shlex.split is not the appropriate tool, as it is intended to resolve and remove shell-style quoting [1]. If you require access to the raw tokens including the original quotes, you can use the underlying shlex.shlex class directly, which provides lower-level control over the tokenization process [1]. For example, using the shlex.shlex class allows you to access individual tokens as they are parsed without the automatic stripping performed by the higher-level split function [1]: import shlex data = '"A Multi-line comment."' sh = shlex.shlex(data) print(list(iter(sh.get_token, ''))) # Output: ['"A Multi-line comment."']

Citations:


🏁 Script executed:

python3 - <<'PY'
import shlex
samples = [
    '--name "张三"',
    '--name "a b"',
    "'quoted'",
    '"quoted"',
    r'--path "C:\\Program Files\\App"',
]
for s in samples:
    for posix in (True, False):
        print('INPUT:', repr(s), 'posix=', posix, '->', shlex.split(s, posix=posix))
    print()
PY

Repository: OneDragon-Anything/OneDragon-ScriptChainer

Length of output: 790


🏁 Script executed:

sed -n '1,280p' src/script_chainer/win_exe/script_runner.py

Repository: OneDragon-Anything/OneDragon-ScriptChainer

Length of output: 8221


🏁 Script executed:

sed -n '280,520p' src/script_chainer/win_exe/script_runner.py

Repository: OneDragon-Anything/OneDragon-ScriptChainer

Length of output: 8202


🏁 Script executed:

sed -n '280,520p' src/script_chainer/win_exe/script_runner.py

Repository: OneDragon-Anything/OneDragon-ScriptChainer

Length of output: 8202


🏁 Script executed:

grep -n "_run_python_script\|sys.argv\|extend(" -n src/script_chainer/win_exe/script_runner.py

Repository: OneDragon-Anything/OneDragon-ScriptChainer

Length of output: 415


🏁 Script executed:

grep -n "_run_python_script\|sys.argv\|extend(" src/script_chainer/win_exe/script_runner.py

Repository: OneDragon-Anything/OneDragon-ScriptChainer

Length of output: 415


_get_args_list 这里会把引号当作字面字符保留下来。 --name "张三" 会被传成 '"张三"'sys.argv.extend(args_list) 也会把这个值原样交给脚本。_launch_script_run_python_script 都复用这套解析,建议统一改成 shell 语义(posix=True)或在进入 sys.argv 前去掉外层引号。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/script_chainer/win_exe/script_runner.py` around lines 201 - 206,
_get_args_list currently parses script_arguments with literal quotes preserved,
so values like --name "张三" are passed through with surrounding quotes into
sys.argv. Update the parsing in _get_args_list to use shell-style semantics (for
example, shlex.split with POSIX behavior) or strip outer quotes before
sys.argv.extend is called, and make sure the shared call paths in _launch_script
and _run_python_script use the corrected behavior consistently.



def _launch_script(
script_config: ScriptConfig,
target_process_infos: list[ProcessInfo] | None = None,
Expand All @@ -222,9 +230,7 @@ def _launch_script(
script_path = script_config.script_path

# 解析启动参数
args_list = None
if script_config.script_arguments and script_config.script_arguments.strip():
args_list = shlex.split(script_config.script_arguments, posix=False)
args_list = _get_args_list(script_config.script_arguments)

pm = ProcessManager()
try:
Expand Down Expand Up @@ -637,6 +643,10 @@ def _run_python_script(
script_dir = script_file_abs.parent
try:
sys.argv = [script_path]
# 解析并添加可能存在的启动参数
args_list = _get_args_list(script_config.script_arguments)
if args_list:
sys.argv.extend(args_list)
sys.path.insert(0, str(script_dir))
os.chdir(script_dir)

Expand Down