Skip to content

Commit

Permalink
20240522 update2
Browse files Browse the repository at this point in the history
  • Loading branch information
neko0xff committed May 22, 2024
1 parent a05989e commit e1fc670
Show file tree
Hide file tree
Showing 59 changed files with 702 additions and 550 deletions.
18 changes: 17 additions & 1 deletion book/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Page not found - Rust 中的命令列應用程式</title>
<base href="/book/">
<base href="/">


<!-- Custom HTML head -->
Expand Down Expand Up @@ -198,6 +198,22 @@ <h1 id="document-not-found-404"><a class="header" href="#document-not-found-404"

</div>

<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};

window.onbeforeunload = function() {
socket.close();
}
</script>



Expand Down
39 changes: 25 additions & 14 deletions book/in-depth/config-files.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,15 @@ <h1 class="menu-title">Rust 中的命令列應用程式</h1>
<div id="content" class="content">
<main>
<h1 id="使用配置檔"><a class="header" href="#使用配置檔">使用配置檔</a></h1>
<p>Dealing with configurations can be annoying
especially if you support multiple operating systems
which all have their own places
for short- and long-term files.</p>
<p>處理配置可能很煩人
特別是如果您支援多個作業系統
都有自己的位置
適用於短期和長期文件。</p>
<p>處理配置可能會很煩人
尤其是在支援多個作業系統的情況下
都有自己的短期和長期文件。</p>
<p>對此有許多解決方案,有些比其他的層次更低。</p>
<p>The easiest crate to use for this is <a href="https://docs.rs/confy/0.3.1/confy/"><code>confy</code></a>.
It asks you for the name of your application
and requires you to specify the config layout
via a <code>struct</code> (that is <code>Serialize</code>, <code>Deserialize</code>)
and it will figure out the rest!</p>
<p>最容易使用的crate是 <a href="https://docs.rs/confy/0.3.1/confy/"><code>confy</code></a>
它會詢問您的應用程式名稱
並請您指定配置佈局 <strong>結構(<code>struct</code>)</strong>
(即 <strong>序列化(<code>Serialize</code>)</strong><strong>反序列化(<code>Deserialize</code>)</strong> )。
然後剩下的它將解決其他問題!</p>
<pre><code class="language-rust ignore">#[derive(Debug, Serialize, Deserialize)]
struct MyConfig {
name: String,
Expand All @@ -213,7 +208,7 @@ <h2 id="配置環境"><a class="header" href="#配置環境">配置環境</a></h
<p><strong>TODO</strong></p>
<ol>
<li>評估現有的 crate</li>
<li>CLI-args + 多個配置 + 環境變數</li>
<li>命令列參數(CLI-args) + 多個配置 + 環境變數</li>
<li><a href="https://docs.rs/configure/0.1.1/configure/"><code>configure</code></a> 可以完成這一切嗎? 周圍有漂亮的包裝嗎?</li>
</ol>
</aside>
Expand Down Expand Up @@ -247,6 +242,22 @@ <h2 id="配置環境"><a class="header" href="#配置環境">配置環境</a></h

</div>

<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};

window.onbeforeunload = function() {
socket.close();
}
</script>



Expand Down
49 changes: 28 additions & 21 deletions book/in-depth/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,10 @@ <h1 class="menu-title">Rust 中的命令列應用程式</h1>
<div id="content" class="content">
<main>
<h1 id="為你的-cli-程式產生文件"><a class="header" href="#為你的-cli-程式產生文件">為你的 CLI 程式產生文件</a></h1>
<p>CLI 的文件 通常包括指令中的 <code>--help</code> 部分和一個手冊(<code>man</code>)頁面。</p>
<p>Both can be automatically generated
when using <a href="https://crates.io/crates/clap"><code>clap</code></a>, via
<a href="https://crates.io/crates/clap_mangen"><code>clap_mangen</code></a> crate.</p>
<p>兩者都可以自動生成
使用 <a href="https://crates.io/crates/clap"><code>clap</code></a> 時,透過<a href="https://crates.io/crates/clap_mangen"><code>clap_mangen</code></a> 箱。</p>
<p>CLI 程式的文件 通常會包括指令中的 <code>--help</code> 部分和一個手冊(<code>man</code>)頁面。</p>
<p>兩者都可以自動產生
當使用 <a href="https://crates.io/crates/clap"><code>clap</code></a> 時,
會透過 <a href="https://crates.io/crates/clap_mangen"><code>clap_mangen</code></a> crate。</p>
<pre><code class="language-rust ignore">#[derive(Parser)]
pub struct Head {
/// file to load
Expand All @@ -192,15 +190,10 @@ <h1 id="為你的-cli-程式產生文件"><a class="header" href="#為你的-cli
#[arg(short = "n", default_value = "5")]
pub count: usize,
}</code></pre>
<p>Secondly, you need to use a <code>build.rs</code>
to generate the manual file at compile time
from the definition of your app
in code.</p>
<p>There are a few things to keep in mind
(such as how you want to package your binary)
but for now
we simply put the <code>man</code> file
next to our <code>src</code> folder.</p>
<p>其次,您需要使用 <code>build.rs</code>在編譯時,
請根據您的應用程式在程式碼中的定義而產生手冊文件。</p>
<p>在這裡你要留意幾件事(例如如何打包你的程式),
但現在我們只是簡單地把 <code>man</code> 檔案放到我們的 <code>src</code> 同等級目錄。</p>
<pre><code class="language-rust ignore">use clap::CommandFactory;

#[path="src/cli.rs"]
Expand All @@ -218,12 +211,10 @@ <h1 id="為你的-cli-程式產生文件"><a class="header" href="#為你的-cli

Ok(())
}</code></pre>
<p>When you now compile your application
there will be a <code>head.1</code> file
in your project directory.</p>
<p>If you open that in <code>man</code>
you’ll be able to admire your free documentation.
如果你在 <code>man</code> 中打開它,則你可閱讀</p>
<p>現在你在編譯你的程式時,
將會在你的專案目錄產生一個 <code>head.1</code> 檔案。</p>
<p>如果你使用 <code>man</code> 開啟它,
你就可以看到你的文件了。</p>

</main>

Expand Down Expand Up @@ -254,6 +245,22 @@ <h1 id="為你的-cli-程式產生文件"><a class="header" href="#為你的-cli

</div>

<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};

window.onbeforeunload = function() {
socket.close();
}
</script>



Expand Down
64 changes: 30 additions & 34 deletions book/in-depth/exit-code.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,40 +178,20 @@ <h1 class="menu-title">Rust 中的命令列應用程式</h1>
<div id="content" class="content">
<main>
<h1 id="退出狀態碼"><a class="header" href="#退出狀態碼">退出狀態碼</a></h1>
<p>A program doesn’t always succeed.
And when an error occurs,
you should make sure to emit the necessary information correctly.
In addition to
<a href="human-communication.html">telling the user about errors</a>,
on most systems,
when a process exits,
it also emits an exit code
(an integer between 0 and 255 is compatible with most platforms).
You should try to emit the correct code
for your program’s state.
For example,
in the ideal case when your program succeeds,
it should exit with <code>0</code>.</p>
<p>一個計劃並不總是成功。
並且當出現錯誤時,您應該確保正確發出必要的資訊。
另外<a href="human-communication.html">告訴使用者錯誤</a>,
在大多數系統上,當進程退出時,它還發出退出代碼(0 到 255 之間的整數與大多數平台相容)。
您應該嘗試發出正確的程式碼
對於你的程式的狀態。
例如,在您的計劃成功的理想情況下,它應該以“0”退出。</p>
<p>When an error occurs, it gets a bit more complicated, though.
In the wild,
many tools exit with <code>1</code> when a common failure occurs.
Currently, Rust sets an exit code of <code>101</code> when the process panicked.
Beyond that, people have done many things in their programs.</p>
<p>So, what to do?
The BSD ecosystem has collected a common definition for their exit codes
(you can find them <a href="https://www.freebsd.org/cgi/man.cgi?query=sysexits&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+11.2-stable&amp;arch=default&amp;format=html">here</a>).
The Rust library <a href="https://crates.io/crates/exitcode"><code>exitcode</code></a> provides these same codes,
ready to be used in your application.
Please see its API documentation for the possible values to use.</p>
<p>After you add the <code>exitcode</code> dependency to your <code>Cargo.toml</code>,
you can use it like this:</p>
<p>程式並不總是成功的。
當錯誤發生時,你應該確保正確地發出必要的資訊,
除了<a href="human-communication.html">告訴使用者錯誤訊息</a>
在大多數系統中,當行程退出時也會發出一個退出代碼
( 一個介於 0 和 255 之間的整數,與大多數平台相容 )。
您應盡量根據程式的狀態去制定狀態碼。
例如: 在程式成功運行的理想情況下,應該以 <code>0</code> 退出。</p>
<p>所以,要如何去做呢?
BSD 生態系統為其退出碼做了一個通用的定義 (你可以在<a href="https://www.freebsd.org/cgi/man.cgi?query=sysexits&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+11.2-stable&amp;arch=default&amp;format=html">這裡</a>找到它們)。
Rust 的 <a href="https://crates.io/crates/exitcode"><code>exitcode</code></a> 函式庫也提供了一樣的程式碼,
而且你可在你的程式中使用。
請參閱其 API 文件以了解其用法。</p>
<p>當你在你的 <code>Cargo.toml</code> 中加入 <code>exitcode</code> 依賴後,
你可以這樣使用:</p>
<pre><code class="language-rust ignore">fn main() {
// ...actual work...
match result {
Expand Down Expand Up @@ -259,6 +239,22 @@ <h1 id="退出狀態碼"><a class="header" href="#退出狀態碼">退出狀態

</div>

<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};

window.onbeforeunload = function() {
socket.close();
}
</script>



Expand Down
16 changes: 16 additions & 0 deletions book/in-depth/human-communication.html
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,22 @@ <h2 id="when-panicking"><a class="header" href="#when-panicking">When panicking<

</div>

<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};

window.onbeforeunload = function() {
socket.close();
}
</script>



Expand Down
18 changes: 17 additions & 1 deletion book/in-depth/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ <h1 class="menu-title">Rust 中的命令列應用程式</h1>
<div id="content" class="content">
<main>
<h1 id="深入探究主題"><a class="header" href="#深入探究主題">深入探究主題</a></h1>
<p>該章節會涵蓋更多較為進階的小細節,但您在編寫命令列應用程式時可能會在乎這一點</p>
<p>該章節會涵蓋更多較為進階的小細節,但您在撰寫命令列應用程式時可能會在乎這一點</p>

</main>

Expand Down Expand Up @@ -209,6 +209,22 @@ <h1 id="深入探究主題"><a class="header" href="#深入探究主題">深入

</div>

<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};

window.onbeforeunload = function() {
socket.close();
}
</script>



Expand Down
Empty file modified book/in-depth/machine-communication-stdin.rs
100644 → 100755
Empty file.
Empty file modified book/in-depth/machine-communication-wc.rs
100644 → 100755
Empty file.
20 changes: 18 additions & 2 deletions book/in-depth/machine-communication.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ <h1 id="communicating-with-machines"><a class="header" href="#communicating-with
but also a version tailored to what other programs need.
Let’s see how we can do this.</p>
<aside>
<p><strong>Note:</strong>
<p><strong>筆記:</strong>
Make sure to read <a href="../tutorial/output.html">the chapter on CLI output</a>
in the tutorial first.
It covers how to write output to the terminal.</p>
Expand Down Expand Up @@ -371,7 +371,7 @@ <h3 id="practical-example-ripgrep"><a class="header" href="#practical-example-ri
(as well the files they are in)
even while <em>ripgrep</em> is still searching.</p>
<aside>
<p><strong>Note:</strong>
<p><strong>筆記:</strong>
This is how Visual Studio Code uses <em>ripgrep</em> for its code search.</p>
</aside>
<h2 id="how-to-deal-with-input-piped-into-us"><a class="header" href="#how-to-deal-with-input-piped-into-us">How to deal with input piped into us</a></h2>
Expand Down Expand Up @@ -493,6 +493,22 @@ <h2 id="how-to-deal-with-input-piped-into-us"><a class="header" href="#how-to-de

</div>

<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};

window.onbeforeunload = function() {
socket.close();
}
</script>



Expand Down
Empty file modified book/in-depth/machine-communication.rs
100644 → 100755
Empty file.
Empty file modified book/in-depth/signals-channels.rs
100644 → 100755
Empty file.
Empty file modified book/in-depth/signals-ctrlc.rs
100644 → 100755
Empty file.
Empty file modified book/in-depth/signals-hooked.rs
100644 → 100755
Empty file.
18 changes: 17 additions & 1 deletion book/in-depth/signals.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ <h1 id="訊號處理"><a class="header" href="#訊號處理">訊號處理</a></h
you need to consider how you can receive these signals
as well as how you can react to them.</p>
<aside>
<p><strong>Note:</strong>
<p><strong>筆記:</strong>
If your applications does not need to gracefully shutdown,
the default handling is fine
(i.e. exit immediately
Expand Down Expand Up @@ -362,6 +362,22 @@ <h2 id="what-to-do-when-you-receive-another-ctrlc-while-youre-handling-the-first

</div>

<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};

window.onbeforeunload = function() {
socket.close();
}
</script>



Expand Down
Loading

0 comments on commit e1fc670

Please sign in to comment.