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
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/01-Report_Bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ body:
attributes:
label: chsrc 版本
# description: 你正在使用 chsrc 哪个版本?
placeholder: 请使用 chsrc -h 或 chsrc -v 查看,并*复制发布日期*
placeholder: 请使用 chsrc -h 或 chsrc --version 查看,并*复制发布日期*
validations:
required: true

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ chsrc-ci-release
##############################
xy
fw
cli-parser-test
README.md.bak*
*.tmp
chsrc_tmp_test.txt
Expand Down
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ debug: build-in-debug-mode



test: test-make-var test-xy test-fw
test: test-make-var test-xy test-fw test-cli-parser

test-make-var:
@echo "On-Linux: $(On-Linux)"
Expand Down Expand Up @@ -250,6 +250,17 @@ test-fw:
@$(CC) test/fw.c $(CFLAGS) $(LDLIBS_win) -o fw
@./fw

test-cli-parser: CFLAGS += $(CFLAGS_debug)
test-cli-parser:
@$(CC) test/cli-parser.c $(CFLAGS) $(CFLAGS_warning) $(LDLIBS_win) -o cli-parser-test
@./cli-parser-test
ifeq ($(On-Windows), 1)
@pwsh -NoLogo -NoProfile -NonInteractive -File test/cli-argv.ps1
else
@perl test/cli-argv.pl
@bash test/cli-completion.sh
endif

check: test

# AUR package 安装时将执行此 target
Expand All @@ -264,6 +275,7 @@ clean:
-@rm *.res 2>/dev/null
-@rm xy 2>/dev/null
-@rm fw 2>/dev/null
-@rm cli-parser-test 2>/dev/null
-@rm README.md.bak* 2>/dev/null

-@rm chsrc 2>/dev/null
Expand Down Expand Up @@ -296,4 +308,4 @@ install: $(ReleaseMode-Target-Name)

.PHONY: all b build bd br bcir d t check c \
build-in-dev-mode build-in-debug-mode build-in-release-mode build-in-ci-release-mode \
debug test test-make-var test-xy test-fw fastcheck test-cli clean install build-deb clean-deb rawstr4c
debug test test-make-var test-xy test-fw test-cli-parser fastcheck test-cli clean install build-deb clean-deb rawstr4c
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,14 @@ git clone https://gitee.com/RubyMetric/chsrc.git; cd chsrc; make
chsrc - Change Source - (GPLv3+)

使用:
chsrc <command> [options] [dish] [mirror]
chsrc [options] <command> [dish] [mirror] [-- recipe-args...]
本体选项可放在 -- 之前的任意位置,例如 chsrc set npm first --dry。
从左到右解析;遇到帮助或版本立即返回,后续参数不再解析。
-- 后的参数原样交给 recipe;未配置解析器时忽略。
引号包裹的内容作为一个参数,保留参数内的双引号、CJK 和 emoji。

命令:
help, h 打印此帮助,或 -h, --help
help 打印此帮助,或 -h, --help
issue, i 查看相关issue

list, ls, l 列出可用镜像站和可换源菜品
Expand All @@ -366,11 +370,14 @@ git clone https://gitee.com/RubyMetric/chsrc.git; cd chsrc; make
reset <dish> 重置,使用上游默认使用的源

选项:
-dry Dry Run,模拟换源过程,命令仅打印并不运行
-scope=project|user|system 仅对本项目换源 / 用户级换源 / 系统级换源 (通过ls <dish>查看)
-ipv6 使用IPv6测速
-en(glish) 使用英文输出
-no-color 无颜色输出
--dry 模拟换源过程,命令仅打印并不运行
--scope <value> 换源作用域,也可写为 --scope=<value>
default / project / user / system
--ipv6 使用 IPv6 测速
--english, --English 使用英文输出(须放在帮助/版本选项之前)
--no-color 无颜色输出
--version 打印版本并立即返回
--help, -h 打印帮助并立即返回
```

<br>
Expand All @@ -395,8 +402,8 @@ git clone https://gitee.com/RubyMetric/chsrc.git; cd chsrc; make

对支持 *项目级* 换源的菜品,可以避免全局(*系统级* 或 *用户级*)换源

$ chsrc set -scope=project bundler
$ chsrc set -scope project pdm
$ chsrc set --scope=project bundler
$ chsrc set --scope project pdm
```

<br>
Expand Down
3 changes: 2 additions & 1 deletion doc/01-开发与构建.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ $ make debug DEBUGGER=lldb
```bash
make test-xy # 测试 xy.h
make test-fw # 测试 framework
make test # 测试上述两个
make test-cli-parser # 测试参数解析、recipe 分发及 Unicode/引号传递
make test # 测试上述三个
make test-cli # 测试命令
make clean
```
Expand Down
45 changes: 45 additions & 0 deletions doc/10-如何编写recipe.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,51 @@

<br>

## 接收 recipe 专用参数

主程序把第一个独立的 `--` 后面的参数按原有顺序、内容和边界交给 recipe。
例如 `chsrc set cargo -- "--skfw sq" sdf wiw "www"` 的尾部是四个参数:
`--skfw sq`、`sdf`、`wiw`、`www`。主程序不会再次拆分、去引号或解释这些参数。
作为参数内容传入的双引号、空字符串、CJK 和 emoji 均保留;Windows 入口复用
`xy_wstr_to_utf8()` 将宽字符参数转换为 UTF-8,其他平台保留传入字节。

recipe 默认忽略这些参数。需要解析时,在 `_prepare()` 中使用
`chef_set_args_parser (this, my_parse_args)` 注册回调:

```c
static RecipeArgsResult_t
my_parse_args (TargetCmd command, int argc, const char *const argv[])
{
if (argc == 0)
return RecipeArgs_Continue;

if (argc == 1 && xy_streql (argv[0], "--help"))
{
puts ("本 recipe 支持的参数...");
return RecipeArgs_Handled;
}

chsrc_error ("本 recipe 不支持这些参数");
return RecipeArgs_Error;
}
```

回调在操作执行前调用,可以用 `command` 区分 get、set、reset、measure 和 list。
`argc` 是尾部参数数量,不含程序名和分隔符;`argc == 0` 时 `argv` 可以为 NULL。
参数只读,生命周期覆盖本次 CLI 调用。

- `RecipeArgs_Continue`:继续当前 recipe 的正常操作。
- `RecipeArgs_Handled`:当前 recipe 已处理完毕,跳过其操作和子 recipe;同级 recipe 继续。
- `RecipeArgs_Error`:停止整个命令,返回退出码 2。回调负责输出具体错误。

组合 recipe 和实际参与操作的子 recipe 收到同一份参数,未配置解析器的组合仍继续向下传递。
某个 recipe 解析参数不会消耗或改变其他 recipe 收到的参数。组合的 measure 仅展示子菜品名称,
不执行子菜品测速,因此只调用组合自身的解析器。

shell 负责外层引号和转义。例如 PowerShell 7 中
`chsrc set cargo -- '他说 "你好" 😀'` 传入一个含双引号的参数。
`--` 后的 `--dry`、`--help` 等均属于 recipe;本体选项须放在分隔符前。

## 开发准则

1. 代码高度可移植
Expand Down
25 changes: 16 additions & 9 deletions doc/chsrc.1
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ chsrc - Change Source for every software on every platform from the command line

.SH SYNOPSIS
.SY chsrc
<command> [options] [dish] [mirror]
[options] <command> [dish] [mirror] [-- recipe-args...]
.YS
.PP
本体选项可放在第一个独立的 -- 之前的任意位置,例如 chsrc set npm first --dry。
按从左到右的顺序解析,遇到 help、--help、-h 或 --version 后立即返回,不再校验业务参数或读取后续参数;help 仅在命令位置生效。此前遇到的未知选项或无效选项值会报错。
-- 后的参数按原有顺序和边界交给 recipe;未注册解析器时忽略。引号分组后的字符串保持为一个参数,保留内层双引号、空参数、CJK 和 emoji。组合 recipe 的实际执行子 recipe 也收到同一份参数。


.SH DESCRIPTION
.SS 基本命令
.TP
.B help
打印此帮助,或 h, -h, --help
打印此帮助并立即返回,或 -h, --help
.TP
.B issue
查看相关issue
Expand Down Expand Up @@ -72,19 +76,22 @@ chsrc - Change Source for every software on every platform from the command line

.SH OPTIONS
.TP
\fB-dry\fR
\fB--version\fR
打印版本并立即返回
.TP
\fB--dry\fR
Dry Run,模拟换源过程,命令仅打印并不运行
.TP
\fB-scope\fR
设置换源的作用域,可选值有:project|user|system|default (通过ls \fI<dish>\fR查看支持情况)
\fB--scope\fR
支持 --scope VALUE 和 --scope=VALUE。设置换源的作用域,可选值有:project|user|system|default (通过ls \fI<dish>\fR查看支持情况)
.TP
\fB-ipv6\fR
\fB--ipv6\fR
使用IPv6测速
.TP
\fB-en(glish)\fR
使用英文输出
\fB--english, --English\fR
使用英文输出,须放在帮助或版本选项之前
.TP
\fB-no-color\fR
\fB--no-color\fR
无颜色输出


Expand Down
25 changes: 16 additions & 9 deletions doc/chsrc.texi
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ For chsrc @value{Chsrc_Version}

The tool and framework @command{chsrc} is a great helper to Change Source for every software on every platform from the command line. This texinfo documentation is published on @b{@value{Doc_Publish_Date}} for @command{chsrc} @b{@value{Chsrc_Version}}

@exdent 使用: chsrc <command> [options] [dish] [mirror]
@exdent 使用: chsrc [options] <command> [dish] [mirror] [-- recipe-args...]

本体选项可放在第一个独立的 -- 之前的任意位置,例如 chsrc set npm first --dry。
按从左到右的顺序解析,遇到 help、--help、-h 或 --version 后立即返回,不再校验业务参数或读取后续参数;help 仅在命令位置生效。此前遇到的未知选项或无效选项值会报错。
-- 后的参数按原有顺序和边界交给 recipe;未注册解析器时忽略。引号分组后的字符串保持为一个参数,保留内层双引号、空参数、CJK 和 emoji。组合 recipe 的实际执行子 recipe 也收到同一份参数。

Exit status:

Expand Down Expand Up @@ -75,7 +79,7 @@ Exit status:
@section 基本命令
@table @samp
@item help
打印此帮助,或 h, -h, --help
打印此帮助并立即返回,或 -h, --help
@item issue
查看相关issue

Expand Down Expand Up @@ -131,19 +135,22 @@ Exit status:
@node options
@chapter 选项
@table @samp
@item -dry
@item --version
打印版本并立即返回

@item --dry
Dry Run,模拟换源过程,命令仅打印并不运行

@item -ipv6
@item --ipv6
使用IPv6测速

@item -scope
设置换源的作用域,可选值有:project|user|system|default (通过ls <dish>查看支持情况)
@item --scope
支持 --scope VALUE 和 --scope=VALUE。设置换源的作用域,可选值有:project|user|system|default (通过ls <dish>查看支持情况)

@item -en(glish)
使用英文输出
@item --english, --English
使用英文输出,须放在帮助或版本选项之前

@item -no-color
@item --no-color
无颜色输出
@end table

Expand Down
8 changes: 7 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ build-in-release-mode:
debug: build-in-debug-mode
@{{DEBUGGER}} {{DebugMode-Target-Name}}

test: test-xy test-fw
test: test-xy test-fw test-cli-parser

# 这些测试文件都用 DEBUG mode
test-xy:
Expand All @@ -148,6 +148,11 @@ test-fw:
@{{CC}} test/fw.c {{CFLAGS_debug_mode}} -o fw
@{{BIN_fw}}

test-cli-parser:
@{{CC}} test/cli-parser.c {{CFLAGS_debug_mode}} -o cli-parser-test
@{{ if os() == 'windows' { 'cli-parser-test.exe' } else { './cli-parser-test' } }}
@{{ if os() == 'windows' { 'pwsh -NoLogo -NoProfile -NonInteractive -File test/cli-argv.ps1' } else { 'perl test/cli-argv.pl' } }}

fastcheck:
@perl ./test/cli.pl fastcheck

Expand All @@ -159,6 +164,7 @@ clean:
-@{{BIN_rm}} *.res
-@{{BIN_rm}} xy
-@{{BIN_rm}} fw
-@{{BIN_rm}} cli-parser-test
-@{{BIN_rm}} chsrc
-@{{BIN_rm}} chsrc-debug
-@{{BIN_rm}} chsrc-release
Expand Down
Loading