Hiroto's diary

プログラミングとか色々

RubyのThorで設定出来ないコマンド名を無理矢理設定する

Rubyで簡単にCLIツールを作れるThor。

github.com

Thorを使って、runshellなどのコマンド名でコマンドを作ろうとすると怒られます。

test-run.rb

require 'thor'

class Cli < Thor

  desc 'run', 'Run command.'
  def run
    puts 'Running "run" command.'
  end

  desc 'shell', 'Shell command.'
  def shell
    puts 'Running "shell" command.'
  end

end

Cli.start(ARGV)

実行すると…

$ ruby test-run.rb
/path/to/ruby/2.4.0/gems/thor-0.20.0/lib/thor/base.rb:557:in `is_thor_reserved_word?': "run" is a Thor reserved word and cannot be defined as command (RuntimeError)
    from /path/to/ruby/2.4.0/gems/thor-0.20.0/lib/thor/base.rb:620:in `method_added'
    from test-run.rb:6:in `<class:Cli>'
    from test-run.rb:3:in `<main>'

と怒られる。

設定出来ないメソッド名

Thor::THOR_RESERVED_WORDSで定義されているワードが使えません。

具体的には

invoke
shell
options
behavior
root
destination_root
relative_root
action
add_file
create_file
in_root
inside
run
run_ruby_script

です。

runshellは割りと使うので普通に困る。(startconsoleなどの別のコマンド名にすれば回避出来るとかは無しで…)

解決策

適当なメソッド名で書いた上で mapを使って紐付け

どうせ紐付けるのでメソッド名は何でも良いんですが、後ろに_commandとでも付けておくのがベタ。

require 'thor'

class Cli < Thor

  desc 'run', 'Run command.'
  def run_command
    puts 'Running "run" command.'
  end

  # runコマンドをrun_commandに紐付け
  map 'run' => 'run_command'

  desc 'shell', 'Shell command.'
  def shell_command
    puts 'Running "shell" command.'
  end

  # shellコマンドをshell_commandに紐付け
  map 'shell' => 'shell_command'

end

Cli.start(ARGV)

実行してみる。

$ ruby test-run.rb
Commands:
  test-run.rb help [COMMAND]  # Describe available commands or one specific com...
  test-run.rb run             # Run command.
  test-run.rb shell           # Shell command.

$ ruby test-run.rb run
Running "run" command.

$ ruby test-run.rb shell
Running "shell" command.

これで怒られずにrunshellコマンドを作成出来ます。

MacでLinuxディストリをUSBに焼く。

たまにやり方忘れて一々検索するのが面倒なのでメモがてら。

焼くのはUbuntu GNOMEUbuntu GNOMEじゃなくても他のディストリでも殆どこの方法でOK。

USBは8GBあれば十分です。ディストリにもよりますが4GBでも使えるものは使えます。


isoファイルをダウンロード

Linuxのサイトからisoファイルをダウンロード。Ubuntu GNOMEならhttps://ubuntugnome.org/download/からダウンロード出来ます。

f:id:Hiroto-K:20170118193916p:plain

USBメモリをフォーマット

Mac標準のディスクユーティリティを使います。

外部の所に出てるUSBメモリを選択して消去をクリック。名前は何でも良いです。

f:id:Hiroto-K:20170118193801p:plain f:id:Hiroto-K:20170118193814p:plain

ダウンロードしたisoファイルを.dmgファイル変換

ダウンロードしたisoファイルをdmgに変換する。ターミナルで以下のコマンドを実行。isoファイルのパスやdmgファイルの出力場所は置き換えてください。

hdiutil convert -format UDRW -o /path/to/ubuntu-gnome.dmg /path/to/ubuntu-gnome.iso

USBの場所を確認してアンマウント

ターミナルで以下のコマンドを実行。

diskutil list

場所が確認できたらアンマウント。以下のコマンドを実行。パスは置き換えてください。

diskutil unmountDisk /path/to/usb

USBに書き込む

以下のコマンドを実行。dmgファイルとUSBの場所は置き換えてくださ。

sudo dd if=/path/to/ubuntu.dmg of=/path/to/usb bs=1m

書き込みが終わると警告が出ますが取り出すをクリック。

最後に以下のコマンドを実行。これもusbの場所を置き換えてください。

diskutil eject /path/to/usb

書き込みはこれで終わりです。正常に出来ていればブート出来ます。

© 2015 hiroxto