Skip to content

docs: polish text of the 2nd chapter #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2025
Merged
Changes from 1 commit
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
23 changes: 11 additions & 12 deletions docs/2.-开始.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ $ chmod +x wyag

我们首先需要导入一些模块(可以逐一复制每个导入,或合并成一行):

- Git 是一个命令行应用程序,因此我们需要解析命令行参数的工具。Python 提供了一个很棒的模块名为 [argparse](https://docs.python.org/3/library/argparse.html),可以为我们完成 99% 的工作。
- Git 是一个命令行应用程序,因此我们需要解析命令行参数的工具。Python 提供了一个很棒的模块名为 [argparse](https://docs.python.org/zh-cn/3/library/argparse.html),可以为我们完成 99% 的工作。

```python
import argparse
```

- 我们还需要一些基本库中没有的容器类型,特别是 `OrderedDict`,它在 [collections](https://docs.python.org/3/library/collections.html#collections.OrderedDict) 中。
- 我们还需要一些基本库中没有的容器类型,特别是 `OrderedDict`,它在 [collections](https://docs.python.org/zh-cn/3/library/collections.html#collections.OrderedDict) 中。

```python
import collections
```

- Git 使用的配置文件格式基本上是微软的 INI 格式。可以使用 [configparser](https://docs.python.org/3/library/configparser.html) 模块读取和写入这些文件。
- Git 使用的配置文件格式基本上是微软的 INI 格式。可以使用 [configparser](https://docs.python.org/zh-cn/3/library/configparser.html) 模块读取和写入这些文件。

```python
import configparser
Expand All @@ -66,19 +66,19 @@ $ chmod +x wyag
from fnmatch import fnmatch
```

- Git 广泛使用 SHA-1 函数。在 Python 中,它位于 [hashlib](https://docs.python.org/3/library/hashlib.html) 中。
- Git 广泛使用 SHA-1 函数。在 Python 中,它位于 [hashlib](https://docs.python.org/zh-cn/3/library/hashlib.html) 中。

```python
import hashlib
```

- 只需要使用 [math](https://docs.python.org/3/library/math.html) 中的一个函数:
- 只需要使用 [math](https://docs.python.org/zh-cn/3/library/math.html) 中的一个函数:

```python
from math import ceil
```

- [os](https://docs.python.org/3/library/os.html) 和 [os.path](https://docs.python.org/3/library/os.path.html) 提供了一些很好的文件系统抽象例程
- [os](https://docs.python.org/zh-cn/3/library/os.html) 和 [os.path](https://docs.python.org/zh-cn/3/library/os.path.html) 提供了一些很好的文件系统抽象操作

```python
import os
Expand All @@ -90,28 +90,28 @@ $ chmod +x wyag
import re
```

- 另外需要 [sys](https://docs.python.org/3/library/sys.html) 来访问实际的命令行参数(在 `sys.argv` 中):
- 另外需要 [sys](https://docs.python.org/zh-cn/3/library/sys.html) 来访问实际的命令行参数(在 `sys.argv` 中):

```python
import sys
```

- Git 使用 zlib 进行所有内容的压缩。Python 中也有 [这个功能](https://docs.python.org/3/library/zlib.html):
- Git 使用 zlib 进行所有内容的压缩。Python 中也有 [这个功能](https://docs.python.org/zh-cn/3/library/zlib.html):

```python
import zlib
```

导入完成。我们将频繁处理命令行参数。Python 提供了一个简单但功能强大的解析库 `argparse`。这是一个不错的库,但其接口可能并不是最直观的;如果需要,可以参考其 [文档](https://docs.python.org/3/library/argparse.html)。
导入完成。我们将频繁处理命令行参数。Python 提供了一个简单但功能强大的解析库 `argparse`。这是一个不错的库,但其接口可能并不是最直观的;如果需要,可以参考其 [文档](https://docs.python.org/zh-cn/3/library/argparse.html)。

```python
argparser = argparse.ArgumentParser(description="最简单的内容跟踪器")
```

我们需要处理子命令(如 git 中的 `init`、`commit` 等)。在 argparse 的术语中,这些被称为“子解析器”。此时我们只需声明我们的 CLI 将使用子解析器,并且所有调用都必须包含一个——你不能只调用 `git`,而是要调用 `git COMMAND`。
我们需要处理子命令(如 git 中的 `init`、`commit` 等)。在 argparse 的术语中,这些被称为“子解析器”。此时我们只需声明我们的 CLI 将使用子解析器,并且所有调用都*必须*包含一个——你不能只调用 `git`,而是要调用 `git 子命令`。

```python
argsubparsers = argparser.add_subparsers(title="Commands", dest="command")
argsubparsers = argparser.add_subparsers(title="子命令", dest="command")
argsubparsers.required = True
```

Expand All @@ -138,4 +138,3 @@ def main(argv=sys.argv[1:]):
case "tag" : cmd_tag(args)
case _ : print("无效命令。")
```