# xcmd **Repository Path**: kingdix10/xcmd ## Basic Information - **Project Name**: xcmd - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-26 - **Last Updated**: 2026-06-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # xcmd 基于 Python 标准 `cmd` 的 bash 风格交互式 Shell - 支持环境变量、shell 模式、Tab 补全。 ## 安装 ```bash pip install xcmd ``` ## 快速开始 ### 作为库使用 ```python from xcmd import xCmd shell = xCmd(tryshell=True) # 开启 shell 模式 shell.cmdloop() ``` ### 直接运行 ```bash python -m xcmd # 默认 prompt python -m xcmd -p "app> " # 自定义 prompt python -m xcmd -s # 开启 shell 模式 ``` ### 项目初始化 ```bash xcmd-init myapp # 默认:wrapper 模式 xcmd-init myapp --no-wrapper # 最少文件模式 xcmd-init myapp --standalone # 独立模式(不依赖 xcmd) ``` ## 内置命令 | 命令 | 说明 | |------|------| | `quit` / `q` / `exit` / `Ctrl+D` | 退出 | | `shell ` / `!` | 执行系统命令 | | `clear` / `cls` | 清屏 | | `set ` | 设置环境变量 | | `unset ` | 删除环境变量 | | `print` / `p` | 打印环境变量 | | `export ` | 导出到系统环境变量 | | `unexport ` | 从系统环境变量移除 | | `newloop` | 重启命令循环 | ## 环境变量 xcmd 维护一个 `_env` 内表,类似 bash 的环境变量。所有变量可通过 `.` 访问: ``` xcmd=> set name Alice xcmd=> set host 127.0.0.1 xcmd=> print TRYSHELL = False AUTOREP = True COMMENT_PREFIX = # name = Alice host = 127.0.0.1 xcmd=> export name ``` 在代码中通过 `shell.name` 读取 -> `'Alice'`。 ### 内置系统变量 | 变量 | 类型 | 说明 | |------|------|------| | `TRYSHELL` | bool | 未知命令转发为系统命令 | | `AUTOREP` | bool | 空行重复上条命令 | | `COMMENT_PREFIX` | str | 注释前缀(跳过该行) | 运行时可通过 `set TRYSHELL True` 动态修改。 ## API ```python from xcmd import xCmd, NewLoop shell = xCmd(prompt="app> ", tryshell=True, autorep=True, comment_prefix="#") # 扩展自定义命令 def do_hello(self, line): print(f"hello, {line}") xCmd.do_hello = do_hello # 读取环境变量 shell.name # 等价于 shell._env['name'] shell._env_bool('FLAG') # 字符串 -> bool # 程序化操作 shell.do_set('key value') shell.do_export('key') shell.do_print('') ```