tools-alias-cli 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,210 @@
1
+ Metadata-Version: 2.3
2
+ Name: tools-alias-cli
3
+ Version: 0.1.0
4
+ Summary: Add your description here
5
+ Author: riag
6
+ Author-email: riag <riag@163.com>
7
+ License: Apache License 2.0
8
+ Requires-Dist: tomli>=2.0.0 ; python_full_version < '3.11'
9
+ Requires-Dist: mypy>=1.10.0 ; extra == 'dev'
10
+ Requires-Python: >=3.10
11
+ Provides-Extra: dev
12
+ Description-Content-Type: text/markdown
13
+
14
+ # Tools Alias CLI
15
+
16
+ 一个用于管理和运行自定义工具别名的命令行工具,支持环境变量配置。
17
+
18
+ ## 特性
19
+
20
+ - 📋 **工具管理**:定义自定义工具,包含名称、命令和帮助信息
21
+ - 🌍 **环境变量**:从配置文件加载环境变量,并支持用户路径展开
22
+ - 📝 **字符串模板**:在工具命令中使用 `${VARIABLE}` 语法引用环境变量
23
+ - 🎯 **动态子命令**:工具可作为直接子命令使用
24
+ - 📖 **帮助系统**:内置 help 命令,支持工具特定的帮助信息
25
+ - 📦 **易于安装**:通过 pip 或 uv 安装
26
+ - 📂 **配置目录定制**:通过 TOOLS_ALIAS_DIR 环境变量自定义配置目录位置
27
+
28
+ ## 安装
29
+
30
+ ### 使用 uv
31
+
32
+ ```bash
33
+ uv pip install tools-alias-cli
34
+ ```
35
+
36
+ ### 使用 pip
37
+
38
+ ```bash
39
+ pip install tools-alias-cli
40
+ ```
41
+
42
+ ## 配置
43
+
44
+ ### 配置文件
45
+
46
+ 默认情况下,工具会在 `~/.tools-alias/config.toml` 查找配置文件。
47
+
48
+ ### 配置目录定制
49
+
50
+ 你可以通过环境变量 `TOOLS_ALIAS_DIR` 来指定配置文件所在的目录:
51
+
52
+ ```bash
53
+ # Linux/Mac
54
+ TOOLS_ALIAS_DIR="~/my-tools-alias" tools-alias-cli list
55
+
56
+ # Windows
57
+ set TOOLS_ALIAS_DIR="C:\\my-tools-alias" && tools-alias-cli list
58
+ ```
59
+
60
+ 如果未设置该环境变量,工具会自动将 `TOOLS_ALIAS_DIR` 设置为默认目录 `~/.tools-alias` 的绝对路径。
61
+
62
+ ### 配置格式
63
+
64
+ 配置文件使用 TOML 格式,结构如下:
65
+
66
+ ```toml
67
+ # 环境变量(可选)
68
+ [ENV]
69
+ HOME_PATH = "~/my-tools"
70
+ CONFIG_PATH = "~/.config/tools-alias"
71
+ USER = "myuser"
72
+
73
+ # 工具定义(必填)
74
+ [[tool]]
75
+ name = "show-home"
76
+ command = "echo ${HOME_PATH}"
77
+ short_help = "显示用户主目录"
78
+ help = "显示展开后的主目录路径"
79
+
80
+ [[tool]]
81
+ name = "list-files"
82
+ command = "ls -la"
83
+ short_help = "列出当前目录文件"
84
+
85
+ [[tool]]
86
+ name = "grep-pattern"
87
+ command = "grep $args *.py"
88
+ short_help = "在 Python 文件中搜索模式"
89
+ ```
90
+
91
+ ### 配置选项
92
+
93
+ #### 环境变量 (`[ENV]`)
94
+
95
+ - 定义可用于所有工具命令的环境变量
96
+ - 值可以包含用户路径展开(`~` 会被替换为用户主目录)
97
+ - 配置文件中的环境变量会覆盖系统环境变量
98
+
99
+ #### 工具定义 (`[[tool]]`)
100
+
101
+ - **name**:工具的唯一标识符(必填)
102
+ - **command**:要执行的命令(必填)
103
+ - **short_help**:帮助输出中显示的简短描述(可选)
104
+ - **help**:详细帮助文本(可选)
105
+ - **help_command**:使用 `help` 命令时执行的帮助命令(可选)
106
+
107
+ ## 使用方法
108
+
109
+ ### 基本命令
110
+
111
+ ```bash
112
+ # 列出所有可用工具
113
+ tools-alias-cli list
114
+
115
+ # 显示特定工具的帮助信息
116
+ tools-alias-cli help show-home
117
+
118
+ # 显示所有工具的帮助信息
119
+ tools-alias-cli help
120
+
121
+ # 使用 run 命令执行工具
122
+ tools-alias-cli run show-home
123
+
124
+ # 直接作为子命令执行工具
125
+ tools-alias-cli show-home
126
+ ```
127
+
128
+ ### 示例
129
+
130
+ #### 示例 1:列出所有工具
131
+
132
+ ```bash
133
+ $ tools-alias-cli list
134
+ 工具别名列表:
135
+ show-home -> echo C:\Users\user/my-tools ( 显示用户主目录 )
136
+ list-files -> ls -la ( 列出当前目录文件 )
137
+ grep-pattern -> grep $args *.py ( 在 Python 文件中搜索模式 )
138
+ ```
139
+
140
+ #### 示例 2:显示所有工具的帮助信息
141
+
142
+ ```bash
143
+ $ tools-alias-cli help
144
+ 所有工具别名:
145
+ show-home - 显示用户主目录
146
+ list-files - 列出当前目录文件
147
+ grep-pattern - 在 Python 文件中搜索模式
148
+ ```
149
+
150
+ #### 示例 3:执行工具
151
+
152
+ ```bash
153
+ $ tools-alias-cli show-home
154
+ 执行命令: echo C:\Users\user/my-tools
155
+ C:\Users\user/my-tools
156
+ ```
157
+
158
+ #### 示例 4:带参数执行工具
159
+
160
+ ```bash
161
+ $ tools-alias-cli grep-pattern "import"
162
+ 执行命令: grep import *.py
163
+ __init__.py:import argparse
164
+ __init__.py:import os
165
+ __init__.py:import sys
166
+ __init__.py:from string import Template
167
+ __init__.py:from typing import Dict, List, Optional
168
+ # 处理 Python 3.10 兼容性
169
+ __init__.py:if sys.version_info >= (3, 11):
170
+ __init__.py: import tomllib
171
+ __init__.py:else:
172
+ __init__.py: import tomli as tomllib
173
+ ```
174
+
175
+ ## 环境变量展开
176
+
177
+ 工具支持两种类型的环境变量处理:
178
+
179
+ 1. **用户路径展开**:环境变量中的 `~` 会被替换为用户的主目录
180
+ 2. **字符串模板**:命令中的 `${VARIABLE}` 会被替换为对应的环境变量值
181
+
182
+ ## 开发
183
+
184
+ ### 要求
185
+
186
+ - Python 3.10+
187
+ - uv(用于开发)
188
+
189
+ ### 设置开发环境
190
+
191
+ ```bash
192
+ # 安装依赖
193
+ uv install
194
+
195
+ # 以开发模式安装
196
+ uv install -e .
197
+
198
+ # 运行测试
199
+ uv run python -m pytest
200
+
201
+ # 运行类型检查
202
+ uv run mypy src/
203
+
204
+ # 构建包
205
+ uv build
206
+ ```
207
+
208
+ ## 许可证
209
+
210
+ Apache License 2.0
@@ -0,0 +1,197 @@
1
+ # Tools Alias CLI
2
+
3
+ 一个用于管理和运行自定义工具别名的命令行工具,支持环境变量配置。
4
+
5
+ ## 特性
6
+
7
+ - 📋 **工具管理**:定义自定义工具,包含名称、命令和帮助信息
8
+ - 🌍 **环境变量**:从配置文件加载环境变量,并支持用户路径展开
9
+ - 📝 **字符串模板**:在工具命令中使用 `${VARIABLE}` 语法引用环境变量
10
+ - 🎯 **动态子命令**:工具可作为直接子命令使用
11
+ - 📖 **帮助系统**:内置 help 命令,支持工具特定的帮助信息
12
+ - 📦 **易于安装**:通过 pip 或 uv 安装
13
+ - 📂 **配置目录定制**:通过 TOOLS_ALIAS_DIR 环境变量自定义配置目录位置
14
+
15
+ ## 安装
16
+
17
+ ### 使用 uv
18
+
19
+ ```bash
20
+ uv pip install tools-alias-cli
21
+ ```
22
+
23
+ ### 使用 pip
24
+
25
+ ```bash
26
+ pip install tools-alias-cli
27
+ ```
28
+
29
+ ## 配置
30
+
31
+ ### 配置文件
32
+
33
+ 默认情况下,工具会在 `~/.tools-alias/config.toml` 查找配置文件。
34
+
35
+ ### 配置目录定制
36
+
37
+ 你可以通过环境变量 `TOOLS_ALIAS_DIR` 来指定配置文件所在的目录:
38
+
39
+ ```bash
40
+ # Linux/Mac
41
+ TOOLS_ALIAS_DIR="~/my-tools-alias" tools-alias-cli list
42
+
43
+ # Windows
44
+ set TOOLS_ALIAS_DIR="C:\\my-tools-alias" && tools-alias-cli list
45
+ ```
46
+
47
+ 如果未设置该环境变量,工具会自动将 `TOOLS_ALIAS_DIR` 设置为默认目录 `~/.tools-alias` 的绝对路径。
48
+
49
+ ### 配置格式
50
+
51
+ 配置文件使用 TOML 格式,结构如下:
52
+
53
+ ```toml
54
+ # 环境变量(可选)
55
+ [ENV]
56
+ HOME_PATH = "~/my-tools"
57
+ CONFIG_PATH = "~/.config/tools-alias"
58
+ USER = "myuser"
59
+
60
+ # 工具定义(必填)
61
+ [[tool]]
62
+ name = "show-home"
63
+ command = "echo ${HOME_PATH}"
64
+ short_help = "显示用户主目录"
65
+ help = "显示展开后的主目录路径"
66
+
67
+ [[tool]]
68
+ name = "list-files"
69
+ command = "ls -la"
70
+ short_help = "列出当前目录文件"
71
+
72
+ [[tool]]
73
+ name = "grep-pattern"
74
+ command = "grep $args *.py"
75
+ short_help = "在 Python 文件中搜索模式"
76
+ ```
77
+
78
+ ### 配置选项
79
+
80
+ #### 环境变量 (`[ENV]`)
81
+
82
+ - 定义可用于所有工具命令的环境变量
83
+ - 值可以包含用户路径展开(`~` 会被替换为用户主目录)
84
+ - 配置文件中的环境变量会覆盖系统环境变量
85
+
86
+ #### 工具定义 (`[[tool]]`)
87
+
88
+ - **name**:工具的唯一标识符(必填)
89
+ - **command**:要执行的命令(必填)
90
+ - **short_help**:帮助输出中显示的简短描述(可选)
91
+ - **help**:详细帮助文本(可选)
92
+ - **help_command**:使用 `help` 命令时执行的帮助命令(可选)
93
+
94
+ ## 使用方法
95
+
96
+ ### 基本命令
97
+
98
+ ```bash
99
+ # 列出所有可用工具
100
+ tools-alias-cli list
101
+
102
+ # 显示特定工具的帮助信息
103
+ tools-alias-cli help show-home
104
+
105
+ # 显示所有工具的帮助信息
106
+ tools-alias-cli help
107
+
108
+ # 使用 run 命令执行工具
109
+ tools-alias-cli run show-home
110
+
111
+ # 直接作为子命令执行工具
112
+ tools-alias-cli show-home
113
+ ```
114
+
115
+ ### 示例
116
+
117
+ #### 示例 1:列出所有工具
118
+
119
+ ```bash
120
+ $ tools-alias-cli list
121
+ 工具别名列表:
122
+ show-home -> echo C:\Users\user/my-tools ( 显示用户主目录 )
123
+ list-files -> ls -la ( 列出当前目录文件 )
124
+ grep-pattern -> grep $args *.py ( 在 Python 文件中搜索模式 )
125
+ ```
126
+
127
+ #### 示例 2:显示所有工具的帮助信息
128
+
129
+ ```bash
130
+ $ tools-alias-cli help
131
+ 所有工具别名:
132
+ show-home - 显示用户主目录
133
+ list-files - 列出当前目录文件
134
+ grep-pattern - 在 Python 文件中搜索模式
135
+ ```
136
+
137
+ #### 示例 3:执行工具
138
+
139
+ ```bash
140
+ $ tools-alias-cli show-home
141
+ 执行命令: echo C:\Users\user/my-tools
142
+ C:\Users\user/my-tools
143
+ ```
144
+
145
+ #### 示例 4:带参数执行工具
146
+
147
+ ```bash
148
+ $ tools-alias-cli grep-pattern "import"
149
+ 执行命令: grep import *.py
150
+ __init__.py:import argparse
151
+ __init__.py:import os
152
+ __init__.py:import sys
153
+ __init__.py:from string import Template
154
+ __init__.py:from typing import Dict, List, Optional
155
+ # 处理 Python 3.10 兼容性
156
+ __init__.py:if sys.version_info >= (3, 11):
157
+ __init__.py: import tomllib
158
+ __init__.py:else:
159
+ __init__.py: import tomli as tomllib
160
+ ```
161
+
162
+ ## 环境变量展开
163
+
164
+ 工具支持两种类型的环境变量处理:
165
+
166
+ 1. **用户路径展开**:环境变量中的 `~` 会被替换为用户的主目录
167
+ 2. **字符串模板**:命令中的 `${VARIABLE}` 会被替换为对应的环境变量值
168
+
169
+ ## 开发
170
+
171
+ ### 要求
172
+
173
+ - Python 3.10+
174
+ - uv(用于开发)
175
+
176
+ ### 设置开发环境
177
+
178
+ ```bash
179
+ # 安装依赖
180
+ uv install
181
+
182
+ # 以开发模式安装
183
+ uv install -e .
184
+
185
+ # 运行测试
186
+ uv run python -m pytest
187
+
188
+ # 运行类型检查
189
+ uv run mypy src/
190
+
191
+ # 构建包
192
+ uv build
193
+ ```
194
+
195
+ ## 许可证
196
+
197
+ Apache License 2.0
@@ -0,0 +1,25 @@
1
+ [project]
2
+ name = "tools-alias-cli"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ authors = [
7
+ { name = "riag", email = "riag@163.com" }
8
+ ]
9
+ requires-python = ">=3.10"
10
+ license = { text = "Apache License 2.0" }
11
+ dependencies = [
12
+ "tomli>=2.0.0; python_version < '3.11'",
13
+ ]
14
+
15
+ [project.optional-dependencies]
16
+ dev = [
17
+ "mypy>=1.10.0",
18
+ ]
19
+
20
+ [project.scripts]
21
+ tools-alias-cli = "tools_alias_cli:main"
22
+
23
+ [build-system]
24
+ requires = ["uv_build>=0.9.17,<0.10.0"]
25
+ build-backend = "uv_build"
@@ -0,0 +1,197 @@
1
+ import argparse
2
+ import os
3
+ import subprocess
4
+ import sys
5
+ from string import Template
6
+ from typing import Dict, List, Optional
7
+
8
+ # 处理 Python 3.10 兼容性
9
+ if sys.version_info >= (3, 11):
10
+ import tomllib
11
+ else:
12
+ import tomli as tomllib
13
+
14
+ ALIAS_DIR = os.path.expanduser(os.environ.get("TOOLS_ALIAS_DIR", "~/.tools-alias"))
15
+ # 如果环境变量 TOOLS_ALIAS_DIR 不存在,就使用 ALIAS_DIR 的值设置它
16
+ if "TOOLS_ALIAS_DIR" not in os.environ:
17
+ os.environ["TOOLS_ALIAS_DIR"] = ALIAS_DIR
18
+ # 使用默认配置文件路径
19
+ CONFIG_FILE = os.path.join(ALIAS_DIR, "config.toml")
20
+
21
+
22
+ class Tool(object):
23
+ def __init__(
24
+ self,
25
+ name: str,
26
+ command: str,
27
+ short_help: str = "",
28
+ help: str = "",
29
+ help_command: str = "",
30
+ ):
31
+ self.name = name
32
+ self.command = command
33
+ self.short_help = short_help
34
+ self.help = help
35
+ self.help_command = help_command
36
+
37
+
38
+ # 全局环境变量存储
39
+ global_env_vars: Dict[str, str] = {}
40
+
41
+
42
+ def load_config() -> Dict[str, Tool]:
43
+ # 确保配置目录存在
44
+ if not os.path.exists(ALIAS_DIR):
45
+ os.makedirs(ALIAS_DIR, exist_ok=True)
46
+
47
+ tools: Dict[str, Tool] = {}
48
+
49
+ if os.path.exists(CONFIG_FILE):
50
+ with open(CONFIG_FILE, "rb") as f:
51
+ config = tomllib.load(f)
52
+
53
+ # 加载环境变量
54
+ env_vars = config.get("ENV", {})
55
+ if isinstance(env_vars, dict):
56
+ # 展开环境变量值中的用户路径
57
+ expanded_env_vars = {
58
+ key: os.path.expanduser(value) for key, value in env_vars.items()
59
+ }
60
+ # 首先将配置文件中的环境变量加载到全局环境变量存储
61
+ global_env_vars.update(expanded_env_vars)
62
+ # 然后使用配置文件中的环境变量更新系统环境变量,覆盖已存在的系统环境变量
63
+ os.environ.update(expanded_env_vars)
64
+
65
+ # 加载工具别名
66
+ tool_configs = config.get("tool", [])
67
+
68
+ # 处理工具配置
69
+ if isinstance(tool_configs, list):
70
+ for tool_data in tool_configs:
71
+ if isinstance(tool_data, dict):
72
+ # 列表格式: [[tool]]
73
+ name = tool_data.get("name", "")
74
+ if not name:
75
+ continue # 跳过没有名称的工具
76
+
77
+ command = tool_data.get("command", "")
78
+ short_help = tool_data.get("short_help", "")
79
+ help_text = tool_data.get("help", "")
80
+ help_command = tool_data.get("help_command", "")
81
+ tool = Tool(
82
+ name=name,
83
+ command=command,
84
+ short_help=short_help,
85
+ help=help_text,
86
+ help_command=help_command,
87
+ )
88
+ tools[name] = tool
89
+
90
+ return tools
91
+
92
+
93
+ def list_aliases(tools: Dict[str, Tool]) -> None:
94
+ """列出所有工具别名"""
95
+ if tools:
96
+ print("工具别名列表:")
97
+ for name, tool in tools.items():
98
+ if tool.short_help:
99
+ print(f" {name} -> {tool.command} ( {tool.short_help} )")
100
+ else:
101
+ print(f" {name} -> {tool.command}")
102
+ else:
103
+ print("没有定义任何工具别名")
104
+
105
+
106
+ def run_alias(tools: Dict[str, Tool], name: str, args: List[str]) -> None:
107
+ """执行工具别名"""
108
+ if name in tools:
109
+ tool = tools[name]
110
+ # 使用 Template 来处理环境变量替换
111
+ m = {}
112
+ m.update(os.environ)
113
+ m["args"] = " ".join(args)
114
+ m["args_list"] = args
115
+ command_template = Template(tool.command)
116
+ full_command = command_template.substitute(m)
117
+ print(f"执行命令: {full_command}")
118
+ subprocess.run(full_command, shell=True, env=os.environ)
119
+ else:
120
+ print(f"别名 {name} 不存在")
121
+
122
+
123
+ def show_help(tools: Dict[str, Tool], name: Optional[str] = None) -> None:
124
+ """显示工具别名的帮助信息"""
125
+ if name is None:
126
+ # 输出所有工具的帮助信息(只显示name和short_help)
127
+ if tools:
128
+ print("所有工具别名:")
129
+ for tool_name, tool in tools.items():
130
+ if tool.short_help:
131
+ print(f" {tool_name} - {tool.short_help}")
132
+ else:
133
+ print(f" {tool_name}")
134
+ else:
135
+ print("没有定义任何工具别名")
136
+ elif name in tools:
137
+ # 输出指定工具的帮助信息
138
+ tool = tools[name]
139
+ print(f"{name}")
140
+ print(f"{tool.command}")
141
+
142
+ if tool.help_command:
143
+ # 使用 Template 来处理 help_command 环境变量替换
144
+ help_command_template = Template(tool.help_command)
145
+ processed_help_command = help_command_template.substitute(os.environ)
146
+
147
+ # 生成具体的可执行命令
148
+ full_command = f"{processed_help_command}"
149
+ print(f"执行帮助命令: {full_command}")
150
+ subprocess.run(full_command, shell=True, env=os.environ)
151
+ elif tool.help:
152
+ # 如果没有help_command但有help字段,显示帮助信息
153
+ print(f"详细帮助: {tool.help}")
154
+ else:
155
+ print(f"别名 {name} 不存在")
156
+
157
+
158
+ def main() -> None:
159
+ """工具别名管理命令行工具"""
160
+ # 加载配置
161
+ tools = load_config()
162
+
163
+ parser = argparse.ArgumentParser(description="工具别名管理命令行工具")
164
+ subparsers = parser.add_subparsers(dest="command", help="可用命令")
165
+
166
+ # 列出所有别名命令
167
+ list_parser = subparsers.add_parser("list", help="列出所有工具别名")
168
+
169
+ # 显示别名帮助命令
170
+ help_parser = subparsers.add_parser("help", help="显示工具别名的帮助信息")
171
+ help_parser.add_argument(
172
+ "name", nargs="?", default=None, help="别名名称(可选,不提供则显示所有工具)"
173
+ )
174
+
175
+ # 为每个工具配置动态创建子命令
176
+ for tool_name, tool in tools.items():
177
+ tool_parser = subparsers.add_parser(
178
+ tool_name, help=tool.short_help or f"执行工具 {tool_name}"
179
+ )
180
+
181
+ args, unkown_args = parser.parse_known_args()
182
+
183
+ if args.command == "list":
184
+ list_aliases(tools)
185
+ elif args.command == "run":
186
+ run_alias(tools, args.name, unkown_args)
187
+ elif args.command == "help":
188
+ show_help(tools, args.name)
189
+ elif args.command in tools:
190
+ # 直接执行工具命令
191
+ run_alias(tools, args.command, unkown_args)
192
+ else:
193
+ parser.print_help()
194
+
195
+
196
+ if __name__ == "__main__":
197
+ main()
@@ -0,0 +1,3 @@
1
+ [tool]
2
+ ls = "dir"
3
+ git = "git"