🧩 x-cmd 简介 >>>
- x-cmd:高效命令行增强工具,统一调用外部命令,支持灵活扩展与批量操作
- 🌈 适配多平台:Windows、Linux、macOS
- 🔗 支持自定义指令映射、参数模板、组合操作、输出管道
- ☑️ 常用于脚本集成、批量运维、CLI自动化、跨平台开发
- ⚡ 支持并发执行、变量展开、模板替换
🚀 安装与升级 >>>
npm 全局安装
npm install -g x-cmd
x-cmd --version
x-cmd --help
其他安装方式
yarn global add x-cmd
pnpm add -g x-cmd
升级到最新版
npm update -g x-cmd
npm install -g x-cmd@latest
📝 基本命令格式 >>>
x-cmd <模块> [子命令] [参数] [选项]
模块:任务类别,如 git、npm、fs 等
子命令:具体功能,如 clone、install、list
参数/选项:目标操作对象及详细参数
基本示例
x-cmd git clone https://github.com/foo/bar.git
x-cmd npm install express --save
x-cmd fs copy "*.md" backup/ --dry-run
🗂️ 常用模块总览 >>>
git:Git仓库操作工具集(clone/pull/push/branch/stash等)
npm:Node.js包管理相关命令
fs:文件与目录批量操作(copy, move, rm, touch, find等)
shell:执行 Shell/批处理脚本及命令
http:HTTP文件下载、接口调用、批量请求
json:JSON数据处理、格式化、比较、查询
regex:正则批量查找、替换、测试文本
tools:辅助集(uuid、date、base64、time计时等常用工具)
docker:Docker容器管理命令
k8s:Kubernetes集群管理命令
⚙️ 通用选项/参数 >>>
-h, --help:显示模块/命令帮助说明
-v, --version:显示当前 x-cmd 版本
-c, --config [路径]:自定义配置文件
--dry-run:仅打印将要执行的命令,不实际运行
-q, --quiet:降低输出,错误时仅打印警告
-y, --yes:自动确认所有询问
-o, --output [文件]:指定输出重定向
--concurrency [数字]:设置并发执行数量(默认:CPU核心数)
-d 变量=值1,值2:批量变量展开
--timeout [秒数]:设置命令执行超时时间
--verbose:显示详细执行过程
🔧 git 模块详细用法 >>>
仓库操作
x-cmd git clone <repo-url> --depth 1
x-cmd git init <directory>
x-cmd git status --porcelain
x-cmd git add .
分支管理
x-cmd git branch -a
x-cmd git checkout -b feature/new
x-cmd git merge main --no-ff
x-cmd git branch -d feature/old
提交与推送
x-cmd git commit -m "commit message"
x-cmd git push origin main
x-cmd git pull --rebase
Stash 操作
x-cmd git stash push -m "work in progress"
x-cmd git stash list
x-cmd git stash pop
📁 fs 文件系统模块 >>>
文件复制
x-cmd fs copy src/*.js dist/
x-cmd fs copy --recursive src/ backup/
x-cmd fs copy --preserve mode,timestamps *.txt archive/
文件移动/重命名
x-cmd fs move "*.log" logs/
x-cmd fs move oldname.txt newname.txt
x-cmd fs move --update src/*.md backup/
文件删除
x-cmd fs rm *.tmp --dry-run
x-cmd fs rm --recursive --force temp/
x-cmd fs rm --interactive *.bak
文件查找
x-cmd fs find . -name "*.js" -type f
x-cmd fs find . -size +10M -exec ls -l {} \;
支持通配符模式匹配,可结合--dry-run预览操作
🖇️ shell 模块高级用法 >>>
基础执行
x-cmd shell run "echo hello, $USER"
x-cmd shell run "npm test" --cwd ./project
x-cmd shell run "docker build -t myapp ."
批量变量展开
x-cmd shell run "echo Processing {file}" -d files=*.md
x-cmd shell run "mkdir {dir}" -d dirs=docs,test,build
脚本执行
x-cmd shell run deploy.sh --env production
x-cmd shell run "scripts/setup.sh" --args --force --verbose
管道操作
cat files.txt | x-cmd shell run "wc -l {stdin}"
find . -name "*.log" | x-cmd shell run "gzip {stdin}"
支持变量批量展开,自动并发执行和环境变量注入
🌐 http 模块网络操作 >>>
文件下载
x-cmd http download https://host/a.zip -o ./a.zip
x-cmd http download --resume https://host/largefile.iso
x-cmd http download --parallel 4 *.tar.gz
API 请求
x-cmd http get https://api.site/users/123
x-cmd http post https://api.site/data --json '{"key":"value"}'
x-cmd http put https://api.site/update/123 --form file=@data.txt
批量API测试
x-cmd http get https://api.site/users/{id} -d id=1,2,3 --concurrency 5
x-cmd http post https://api.site/batch --data-file requests.json
请求选项
x-cmd http get https://api.example.com/data \
--header "Authorization: Bearer token" \
--header "Content-Type: application/json" \
--timeout 30 \
--retry 3
支持 download/get/post/put/delete 等HTTP方法,支持动态变量与并发控制
🧮 json 处理工具 >>>
格式化与验证
x-cmd json format data.json --indent 2
x-cmd json validate config.json --schema schema.json
x-cmd json pretty-print --sort-keys file.json
数据查询
x-cmd json query "users[0].name" data.json
x-cmd json query "$.items[*].price" --format csv products.json
数据比较
x-cmd json compare old.json new.json
x-cmd json diff file1.json file2.json --output diff.json
数据转换
x-cmd json to-yaml data.json
x-cmd json to-xml --root items data.json
x-cmd json extract "users.*.email" users.json --output emails.txt
支持JSONPath查询、数据格式转换和差异比较
🔍 regex 正则表达式工具 >>>
查找匹配
x-cmd regex find "\d{4}-\d{2}-\d{2}" --files=*.txt
x-cmd regex find "email:\s*([^\s]+)" --groups --files=*.log
替换操作
x-cmd regex replace "(\d+)" "[数字: \1]" --files=*.txt
x-cmd regex replace "\b(https?://[^\s]+)" "[链接]" --global
批量处理
x-cmd regex replace "TODO:" "FIXME:" --files="src/**/*.{js,ts}"
x-cmd regex find "function\s+(\w+)" --count --files=*.js
测试与验证
x-cmd regex test "^\d{4}-\d{2}-\d{2}$" "2023-12-25"
x-cmd regex extract "(\w+)@(\w+\.\w+)" "email@example.com"
支持文件批量、交互式替换、正则表达式测试和捕获组
🛠️ tools 实用工具集 >>>
UUID 生成
x-cmd tools uuid
x-cmd tools uuid --v4
x-cmd tools uuid --namespace dns --name example.com
时间日期
x-cmd tools date --format "%Y-%m-%d %H:%M:%S"
x-cmd tools timestamp
x-cmd tools time --stopwatch
编码转换
x-cmd tools base64 encode "hello world"
x-cmd tools base64 decode "aGVsbG8gd29ybGQ="
x-cmd tools url-encode "https://example.com/q=test value"
x-cmd tools hash md5 "data"
x-cmd tools hash sha256 "file.txt"
文件操作
x-cmd tools split largefile.txt --lines 1000
x-cmd tools merge part1.txt part2.txt --output combined.txt
x-cmd tools watch "*.js" --command "npm test"
提供常用开发工具函数,包括加密、编码、文件处理等
⚡ 高级用法与技巧 >>>
多步流水线组合
x-cmd fs rm *.tmp | x-cmd fs copy src/*.js dist/ | x-cmd tools notify "Build complete"
模板变量高级用法
x-cmd shell run "echo User: {user}, Home: {home}" -d user=admin,home=/home/admin
x-cmd http post https://api.site/{endpoint} --json 'data: {payload}' -d endpoint=users,data='{"name":"test"}'
条件执行
x-cmd shell run "test -f config.json" --then "x-cmd json format config.json"
x-cmd fs find . -name "*.js" --exec "x-cmd tools eslint {file}"
与脚本语言协作
const { exec } = require('child_process');
exec('x-cmd fs copy "*.md" backup/');
import subprocess
subprocess.run(['x-cmd', 'git', 'pull'])
支持管道操作、条件执行、模板变量嵌套和多语言集成
🔧 配置与扩展 >>>
全局配置文件 (~/.xcmddrc)
[alias]
ls = fs list
gco = git checkout
npm-test = npm test
[defaults]
concurrency = 4
timeout = 60
dry-run = false
[modules.git]
default-remote = origin
default-branch = main
项目配置 (.xcmddrc)
[environment]
NODE_ENV = development
API_URL = http://localhost:3000
[scripts]
build = "npm run build"
test = "npm run test"
deploy = "npm run build && npm run deploy"
自定义模块
module.exports = {
commands: {
hello: (args) => {
console.log(`Hello, ${args.name || 'World'}!`);
}
}
};
支持全局/项目配置、命令别名、环境变量和自定义模块开发
🐳 Docker 模块 >>>
容器管理
x-cmd docker ps -a
x-cmd docker run nginx:latest -p 8080:80
x-cmd docker stop $(x-cmd docker ps -q)
x-cmd docker rm $(x-cmd docker ps -a -q --filter status=exited)
镜像操作
x-cmd docker build -t myapp:1.0 .
x-cmd docker push myapp:1.0
x-cmd docker pull node:16-alpine
x-cmd docker rmi $(x-cmd docker images -q --filter dangling=true)
网络与卷
x-cmd docker network create app-network
x-cmd docker volume create app-data
x-cmd docker run --network app-network -v app-data:/data app:latest
提供完整的 Docker 容器生命周期管理功能
☸️ Kubernetes 模块 >>>
Pod 管理
x-cmd k8s get pods --all-namespaces
x-cmd k8s describe pod my-app-pod
x-cmd k8s logs my-app-pod --follow
x-cmd k8s delete pod my-app-pod
部署与服务
x-cmd k8s apply -f deployment.yaml
x-cmd k8s expose deployment my-app --port=80 --target-port=3000
x-cmd k8s scale deployment my-app --replicas=3
配置管理
x-cmd k8s get configmaps
x-cmd k8s create configmap app-config --from-file=properties.txt
x-cmd k8s get secrets
支持 Kubernetes 资源管理和集群操作
🌟 最佳实践与FAQ >>>
性能优化
- 合理设置
--concurrency 避免资源耗尽
- 使用
--dry-run 预览批量操作结果
- 配置适当的
--timeout 防止命令挂起
安全实践
- 避免在命令行中直接传递敏感信息
- 使用环境变量或配置文件管理凭证
- 定期清理临时文件和日志
常见问题解决
- 路径问题:使用绝对路径或检查工作目录
- 权限问题:确保有足够的文件系统权限
- 并发问题:降低并发数或增加延迟
- 编码问题:指定正确的文件编码格式
🚀 性能监控与调试 >>>
调试模式
x-cmd --verbose fs copy *.txt backup/
x-cmd --debug http get https://api.example.com/data
DEBUG=x-cmd:* x-cmd git pull
性能分析
x-cmd --time npm install
x-cmd --profile fs move large/ backup/
日志管理
x-cmd --log-file xcmd.log fs rm --recursive temp/
x-cmd --log-level debug shell run "complex script.sh"
错误处理
x-cmd shell run "failing_command" --retry 3 --delay 5
x-cmd fs copy important/ backup/ --continue-on-error
提供丰富的调试和监控选项,便于问题排查