1
0
card/public/book/1/AGlkOnBsYzo0aHFqZm43bTZuNWhubzNkb2FtdWhnZWY/c3/2.html
2024-03-23 05:22:55 +09:00

637 lines
19 KiB
HTML

<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>seahorse · hello world! bluesky</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="@gitbook-ng/gitbook 3.3.6">
<meta name="author" content="syui">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-codeblock-filename/block.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="3.html" />
<link rel="prev" href="1.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
hello world! bluesky
</a>
</li>
<li class="chapter " data-level="1.2" data-path="../c1/">
<a href="../c1/">
part 1
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="../c1/1.html">
<a href="../c1/1.html">
quick start
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="../c1/2.html">
<a href="../c1/2.html">
example
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="../c2/">
<a href="../c2/">
part 2
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../c2/1.html">
<a href="../c2/1.html">
bluesky
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../c2/2.html">
<a href="../c2/2.html">
terminal
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="../c2/3.html">
<a href="../c2/3.html">
shell
</a>
</li>
<li class="chapter " data-level="1.3.4" data-path="../c2/4.html">
<a href="../c2/4.html">
rust
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="./">
<a href="./">
part 3
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="1.html">
<a href="1.html">
hello world
</a>
</li>
<li class="chapter active" data-level="1.4.2" data-path="2.html">
<a href="2.html">
seahorse
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="3.html">
<a href="3.html">
reqwest
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../c4/">
<a href="../c4/">
part 4
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../c4/0.html">
<a href="../c4/0.html">
ai
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="../c4/1.html">
<a href="../c4/1.html">
config
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="../c4/2.html">
<a href="../c4/2.html">
mention
</a>
</li>
<li class="chapter " data-level="1.5.4" data-path="../c4/3.html">
<a href="../c4/3.html">
base64
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="../end/">
<a href="../end/">
end
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >seahorse</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h3 id="seahorse">seahorse</h3>
<p>Next, introduce the framework <a href="https://github.com/ksk001100/seahorse" target="_blank">ksk001100/seahorse</a>.</p>
<p>This framework is for writing a cli (command line interface).</p>
<p>A cli is simply the same command as <code>which</code> or <code>curl</code> that you have been executing. We will now create our own command.</p>
<p>It may sound difficult to some, but it is easy if you use a wonderful framework called <code>seahorse</code>.</p>
<p>First you install <code>seahorse</code>, but to install the library in rust, write the package name in <code>Cargo.toml</code>. This will automatically install the library when you build.</p>
<p>Note that library is sometimes abbreviated as <code>lib</code>.</p>
<div><p class="code-filename">Cargo.toml</p></div>
<pre><code class="lang-toml"><span class="hljs-section">[package]</span>
<span class="hljs-attr">name</span> = <span class="hljs-string">&quot;rust&quot;</span>
<span class="hljs-attr">version</span> = <span class="hljs-string">&quot;0.1.0&quot;</span>
<span class="hljs-attr">edition</span> = <span class="hljs-string">&quot;2021&quot;</span>
<span class="hljs-section">
[dependencies]</span>
<span class="hljs-attr">seahorse</span> = <span class="hljs-string">&quot;*&quot;</span>
</code></pre>
<p>Then we write the body code that uses <code>seahorse</code>.</p>
<div><p class="code-filename">src/main.rs</p></div>
<pre><code class="lang-rust"><span class="hljs-keyword">use</span> seahorse::{App, Context};
<span class="hljs-keyword">use</span> std::env;
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
<span class="hljs-keyword">let</span> args: <span class="hljs-built_in">Vec</span>&lt;<span class="hljs-built_in">String</span>&gt; = env::args().collect();
<span class="hljs-keyword">let</span> app = App::new(<span class="hljs-built_in">env!</span>(<span class="hljs-string">&quot;CARGO_PKG_NAME&quot;</span>))
.action(s)
;
app.run(args);
}
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">s</span></span>(_c: &amp;Context) {
<span class="hljs-built_in">println!</span>(<span class="hljs-string">&quot;Hello, world!&quot;</span>);
}
</code></pre>
<p>The contents are very simple. When the command is executed, <code>Hello, world!</code> is output.</p>
<pre><code class="lang-sh">$ cargo build
$ ./target/debug/rust
Hello, world!
</code></pre>
<p>What is different now, for example, is that the <code>help</code> option is automatic.</p>
<pre><code class="lang-sh">$ ./target/debug/rust -h
Name:
rust
Flags:
-h, --help : Show <span class="hljs-built_in">help</span>
</code></pre>
<p>To help readers understand the awesomeness of <code>seahorse</code>, ask them to think about the application themselves.</p>
<div><p class="code-filename">src/main.rs</p></div>
<pre><code class="lang-rust"><span class="hljs-keyword">use</span> seahorse::{App, Context, Command};
<span class="hljs-keyword">use</span> std::env;
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">main</span></span>() {
<span class="hljs-keyword">let</span> args: <span class="hljs-built_in">Vec</span>&lt;<span class="hljs-built_in">String</span>&gt; = env::args().collect();
<span class="hljs-keyword">let</span> app = App::new(<span class="hljs-built_in">env!</span>(<span class="hljs-string">&quot;CARGO_PKG_NAME&quot;</span>))
.action(s)
.command(
Command::new(<span class="hljs-string">&quot;yes&quot;</span>)
.alias(<span class="hljs-string">&quot;y&quot;</span>)
.action(y),
)
.command(
Command::new(<span class="hljs-string">&quot;no&quot;</span>)
.alias(<span class="hljs-string">&quot;n&quot;</span>)
.action(n),
)
;
app.run(args);
}
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">s</span></span>(_c: &amp;Context) {
<span class="hljs-built_in">println!</span>(<span class="hljs-string">&quot;Hello, world!&quot;</span>);
}
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">y</span></span>(_c: &amp;Context) {
<span class="hljs-built_in">println!</span>(<span class="hljs-string">&quot;yes&quot;</span>);
}
<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">n</span></span>(_c: &amp;Context) {
<span class="hljs-built_in">println!</span>(<span class="hljs-string">&quot;no&quot;</span>);
}
</code></pre>
<p>Try writing this and executing the command you created.</p>
<pre><code class="lang-sh">$ ./target/debug/rust
$ ./target/debug/rust y
$ ./target/debug/rust n
</code></pre>
<p>The differences and key points of the code are as follows</p>
<p><pre><code class="lang-diff"> use seahorse::{App, Context
<span class="hljs-addition">+ , Command</span>
};
<span class="hljs-addition">+ </span>
.command(
Command::new(&quot;yes&quot;)
.alias(&quot;y&quot;)
.action(y),
)
fn y(_c: &amp;Context) {
println!(&quot;yes&quot;);
}
</code></pre>
</p>
<p>Feel free to rewrite or add these values to create your own commands.</p>
<p>Here, the value specified in <code>Command::new</code> means the option name.</p>
<p>In this case, <code>rust yes</code> is the issue of this command.</p>
<p>You can specify omission by <code>alias(&quot;y&quot;)</code>. In this case, <code>rust y</code>.</p>
<p>The <code>action(y)</code> specifies the function <code>fn y</code>, the contents of which will be executed. The processing of the command body is written in <code>action</code>.</p>
<p>By the way, <code>action</code> does not necessarily have to be a function.</p>
<p>For example, try adding the following code in place. The command is <code>rust t</code> or <code>rust t foo</code>.</p>
<div><p class="code-filename">src/main.rs</p></div>
<pre><code class="lang-rust">.command(
Command::new(<span class="hljs-string">&quot;test&quot;</span>)
.alias(<span class="hljs-string">&quot;t&quot;</span>)
.action(|c| <span class="hljs-built_in">println!</span>(<span class="hljs-string">&quot;Hello, {:?}&quot;</span>, c.args)),
)
</code></pre>
<pre><code class="lang-sh">$ ./target/debug/rust t bluesky
Hello, [<span class="hljs-string">&quot;bluesky&quot;</span>]
</code></pre>
<h4 id="cli">cli</h4>
<ul>
<li>CLI</li>
</ul>
<p>The term <code>cli</code> has many meanings. It can refer to the cli tool as described above, or it can refer to terminal operations in general.</p>
<h4 id="cui-and-gui">cui and gui</h4>
<ul>
<li>CUI, GUI</li>
</ul>
<p>There are two kinds of cui and gui. The one we are using now is <code>cui</code>.</p>
<p>The term <code>cli</code> is used in almost the same way.</p>
<p>cui means terminal operation, and gui means operation on graphical os.</p>
<p>It is divided into c-ui and ui, which is just ui. ui stands for user interface.</p>
<p>All common os such as windows and mac are based on gui operation.</p>
<h4 id="author">author</h4>
<p>The author of <a href="https://github.com/ksk001100/seahorse" target="_blank">seahorse</a> is <a href="https://twitter.com/ksk001100" target="_blank">ksk</a>.</p>
<p>Thanks for creating a great framework.</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="1.html" class="navigation navigation-prev " aria-label="Previous page: hello world">
<i class="fa fa-angle-left"></i>
</a>
<a href="3.html" class="navigation navigation-next " aria-label="Next page: reqwest">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"seahorse","level":"1.4.2","depth":2,"next":{"title":"reqwest","level":"1.4.3","depth":2,"path":"c3/3.md","ref":"c3/3.md","articles":[]},"previous":{"title":"hello world","level":"1.4.1","depth":2,"path":"c3/1.md","ref":"c3/1.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","codeblock-filename","mermaid-gb3","diff"],"root":"./","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"codeblock-filename":{},"mermaid-gb3":{},"diff":{"type":"markdown","method":"diffChars","options":{}},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"syui","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"hello world! bluesky","gitbook":"*","description":"This is a bluesky ai-card example, and first rust"},"file":{"path":"c3/2.md","mtime":"2023-07-31T07:00:11.330Z","type":"markdown"},"gitbook":{"version":"3.3.6","time":"2023-07-31T07:16:09.509Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
<script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>
</body>
</html>