6
7

More than 5 years have passed since last update.

RubyでSTDINやSTDOUTを活用できるCLIツールを書く

Last updated at Posted at 2014-10-30

CLIツールを作るとき、UNIX哲学が好きな人は、
パイプを活用できるように、STDINから入力を取得したいし、STDOUTへ出力したいですよね。

サンプルとして、STDINから取得した文字列をキャメルケースにしたり、スネークケースにしたりしてSTDOUTに出力するコマンドを作ってみます。

こんなイメージのCLIツールを作ってみます。

$ cat 'hello_world' | mycommand camelize
HelloWorld

$ cat 'HelloWorld' | mycommand snake
hello_world

パイプでつなげるので

$ cat 'hello_world' | mycommand camelize | mycommand snake
hello_world

という無駄なこともできます。

bundler gem で雛形を作成する

まずはbundler gemコマンドで雛形を作成します。

$ bundler gem mycommand

CLIツールを簡単に作れるgemのThorを利用するので、mycommand.gemspecファイルにthorを追加します。

spec.add_dependency "thor"

とりあえずbundleを叩いてThorをインストールしておきます。

$ bundle

lib/mycommand.rbを書きます。コマンドはcamelizesnakeを作ります。

require "mycommand/version"
require "thor"

module Mycommand
  class CLI < Thor

    desc "echo 'hello_world' | mycommand camelize", "Camelize STDIN"
    def camelize()
      stdin = stdin_read
      puts stdin.split('_').map{|s| s.capitalize}.join('')
    end

    desc "echo 'HelloWorld' | mycommand snake", "Snakecase STDIN"
    def snake()
      stdin = stdin_read
      puts stdin.split(/(?![a-z])(?=[A-Z])/).map{|s| s.downcase}.join('_')
    end

    private
    def stdin_read()
      return $stdin.read
    end

  end
end

bin/mycommandも書きます。

#!/usr/bin/env ruby

require 'mycommand'
Mycommand::CLI.start

コマンドを試してみる

bin/mycommandに実行権限を与えます。

$ chmod +x bin/mycommand

コマンド実行を試してみます

$ echo 'hello_world' | bundle exec bin/mycommand camelize
HelloWorld

$ echo 'HelloWorld' | bundle exec bin/mycommand snake
hello_world'

$ echo 'hello_world' | bundle exec bin/mycommand camelize | bundle exec bin/mycommand snake
hello_world'

できました!

あとは、必要に応じてmycommand.gemspecファイルのTODOを修正して bundle exec rake install なりしてしまえばSTDINを入力として受け取れるmycommandができます。

というわけで

良いCLIツールライフを!

参考URL

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7