Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

CLI

CLIツールの色指定。役割・用途・具体例のまとめ

CLI

CLIツールの色指定について、その役割・典型的な用途・具体的なプロダクトでの利用例をまとめました。

3つのgemの事例から学ぶRubyにおけるコマンドラインツール設計技法

3つのgemからRubyのCLIツールの設計について学びます。 今回コールドリーディングをするのは bundler itamae serverkit です。

Ruby | CLI | Add Color Formatting and Interactivity | Formatting Output with Tables

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Add Color Formatting and Interactivity いつ表形式の出力を使うか レコードと列からなる大量のデータを扱う場合。 DBの表データが典型的な例。 他のアプリケーションが利用しやすいフォー…

Ruby | CLI | Add Color Formatting and Interactivity | Adding Color Using ANSI Escape Sequences

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Add Color Formatting and Interactivity いつ色付きの出力を使うか 状態を表す Cucumberは色を効果的に使ったCLI Toolの良い例です。 テストが失敗していれば、赤。 成功していれば緑で表示…

Ruby | CLI | Add Color Formatting and Interactivity

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Add Color Formatting and Interactivity 詳細 例えばCucumberはカラフルな出力でユーザーにテスト結果をわかりやすく伝えます。 例えばSQLクライアントはユーザーに優しい表形式の出力を行…

Ruby | CLI | Be Easy to Maintain

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Be Easy to Maintain 詳細 どのようにアプリケーションの複雑性の増加を防ぐか? アプリケーションをわかりやすく保つことは,アプリケーションの開発に協力してくれるくれる人を増やすのに役…

Ruby | CLI | Make Configration Easy | Using Configuration File with Command Suites

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Make Configration Easy 詳細 Command Suitesのように複数のコマンドを持つ場合、コマンドごとの引数やオプションの設定が必要です。 サンプル仕様 下記記事のサンプルをベースに設定を追加…

Ruby | CLI | Make Configration Easy | Reading External Configuration from Files

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Make Configration Easy 詳細 外部設定ファイルを利用すると、ユーザーはコマンドラインの引数やオプションを与えることなく アプリケーションの振る舞いを制御できる。例えばあるオプション…

Ruby | CLI | Make Configration Easy | Why External Configuration?

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Make Configration Easy 詳細 なぜ外部設定が必要になるのか?アプリケーションの基本的な振る舞いや、新規ユーザーが使いやすく・覚えやすいように作られるが、 パワーユーザーの複雑な作業…

Ruby | CLI | Delight Casual Users | Choosing the Best Default Output Format Based on Context

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Delight Casual Users 詳細 ls はコンテキストに合わせてベストなデフォルト値を適用しています。 ls をオプションなしで、単独実行した場合は 下記のように、人間向けのフォーマットになり…

Ruby | CLI | Delight Casual Users | Preventing Destructive Actions by Default

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Delight Casual Users 詳細 UNIXの rm は破壊的なコマンドの良い例です。 rm は指定ファイルを削除します。デフォルトの rm はHelpfulなだけではなく、破壊的ではありません。 rm のみだと削…

Ruby | CLI | Delight Casual Users | Deciding Default Behavior

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Delight Casual Users 詳細 アプリケーションのデフォルトの振る舞いは、オプションを省略した際の動作です。 適切なデフォルトの振る舞いを選ぶことは、アプリケーションが何をするか、に依…

Ruby | CLI | Delight Casual Users | Default Values for the App's Arguments

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Delight Casual Users 詳細 多くのコマンドラインは引数としてファイルリストや標準入力を使用します。 その際に、入力があれば引数から値を取得。 入力がなければ標準入力から値を取得する…

Ruby | CLI | Delight Casual Users | Flag Arguments That Control Output Formatting

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Delight Casual Users 詳細 フラグの典型的な利用方法として出力フォーマットの変更がある。 典型的なのはアプリケーション向けの標準フォーマットと人間向けのPrettyフォーマット。どちらを…

Ruby | CLI | Delight Casual Users | Default Values for Flags

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Delight Casual Users 詳細 ユーザーがコマンドを実行する際に毎回フラグを指定しなければならないのなら、 ユーザーフレンドリーではありません。デフォルト値の設定は設計の決定事項です。…

Ruby | CLI | Delight Casual Users | Naming Commands in a Command Suit

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Delight Casual Users 詳細 Command suit において全てのコマンドは簡潔で明確な名前を持つこと。 もし、意味をなすことができるならコマンドは短いニーモニックを持つのも良い。 良い例は l…

Ruby | CLI | Delight Casual Users | Choosing Names for Options and Commands

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Delight Casual Users 詳細 Short-form optionは何のためにあるか? => アプリケーションを使い慣れたユーザーが少ないタイプで素早く実行するため。Long-form optionは何のためにあるか? =…

Ruby | CLI | Play Well with Others | Trapping Signals Sent from Other Apps

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 長い処理を行うアプリケーションを作った場合などに、途中で処理を中止することがあります。 そのような際に行う処理を記述しておき、中途半端な処理にならない…

Ruby | CLI | Play Well with Others | Provide a Pretty-Printing Option

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 CLIアプリケーションは、他のアプリケーション向けの「機械的に読み取りやすいデータ」と 「人間が読みやすいデータ」の両面をサポートすることが重要です。この…

Ruby | CLI | Play Well with Others | Add Additional Fields to Make Searching Easier

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 ユーザーが対象データを抽出しやすいように、フラグやステータスを表す内容を レコードに含めることも有効。 サンプル仕様 引数1 〜 引数2 の範囲数値を配列化し…

Ruby | CLI | Play Well with Others | Format Output One Record per Line, Delimiting Fields

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 1レコード、デリミタ区切りのフィールドの構成にすることによって 後続のアプリケーションが加工しやすいフォーマットになります。 サンプル 引数1〜引数2の数値…

Ruby | CLI | Play Well with Others | Formatting Output for Use As Input Another Program

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 あるアプリケーションの出力が他のアプリケーションの入力になる例として、 ls と sort の組み合わせを見る。ls には数値順にソートする機能はない。 しかし、ls…

Ruby | CLI | Play Well with Others | Use STDOUT and STDERR to Send Output to the Correct Stream

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 標準出力はSTDOUTに、標準エラーはSTDERRに出力します。 標準エラーの出力には STDERR.puts "エラーメッセージ" warn "エラーメッセージ" の両方が利用できます…

Ruby | CLI | Play Well with Others | Using Open3 to Access the Standard Output and Error Streams Separately

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 Rubyではバッククォートもしくは、 %x[] によって標準出力を取得できるが 標準エラーは取得できない。標準エラーにもアクセスするには Open3ライブラリを利用す…

Ruby | CLI | Play Well with Others | Reporting Multiple Errors int the Exit

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 exit code は一つの数値しか取れません。 ビットマスクとして扱うことで各ビットに意味を持たせることができます、 サンプルコード 引数に応じて exit code を設…

Ruby | CLI | Play Well with Others | Sending Exit Codes to Calling Process

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 exit code の設定は非常にシンプルです。 Rubyは組み込みのメソッドとして exit メソッドを持っています。 引数を省略して exit すると、$? は 0 = 成功になりま…

Ruby | CLI | Play Well with Others | Accessing Exit Codes of Other Commands

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 $? という名前はBashをやっている人には慣れ親しんだ変数名だが その他人にはわかりにくい。 より人間に読みやすくするため、標準ライブラリの English を利用し…

Ruby | CLI | Play Well with Others | Using Exit Codes to Report Success or Failure

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 exit code はアプリケーション終了時の成否を伝える。 exit code が 0 なら成功。 それ以外なら失敗。シェルの $? にステータスが格納されています。 サンプル仕…

Ruby | CLI | Play Well with Others

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Play Well with Others 詳細 CLIアプリケーションを利用する直接のユーザーは利用者の一部。 利用者には他のCLIアプリケーションもいる。他のアプリケーションとコミュニケーションをとる手…

Ruby | CLI | Be Helpful | Documenting Everything Else

概要 書籍 Build Awesome Command-Line Applications in Ruby2 章 Be Helpful 詳細 man page など、helpオプション以外のより詳細を記述するドキュメントについて。 このドキュメントはセクションによって分類されるべき。一般的なカテゴリは以下 カテゴリ …