bar_1

contents_map

ラベル CT-技術 の投稿を表示しています。 すべての投稿を表示
ラベル CT-技術 の投稿を表示しています。 すべての投稿を表示

2018年12月21日金曜日

Thor メソッドオプション Method Options

メソッドオプション Method Options

Thorでは, タスクにいろいろなオプションを指定できる; これには, もろもろのオプションのHash変数を与える method_options を使うか, 個別のオプションについてより詳しく指定する method_option を使う. これらのメソッドの結果にはHash変数 options 経由でアクセスできる.
Thor allows you to specify options for its tasks, using method_options to supply a hash of options, or method_option to provide more detail about an individual option. You can access these options via the options hash.

可能なオプション Available Options

  • :aliases
    指定しようとしているオプションのエイリアスのリスト. 通常, オプションの省略形を与えるのに使うことになるだろう.
    A list of aliases for this option. Typically, you would use aliases to provide short versions of the option.
  • :banner
    これはオプションの短い説明で, 使い方の説明を表示する際に表示される. デフォルトでは, これはフラグの大文字バージョンである (from=FROM).
    The short description of the option, printed out in the usage description. By default, this is the upcase version of the flag (from=FROM).
  • :default
    設定しようとしているオプションが指定されなかった場合の, デフォルト値. :defaultをもつオプションは, :requiredは指定できない (訳注: 「デフォルト値 Default Values」の節に矛盾した記述がある. また、両方指定したときエラーとはならないようだ).
    The default value of this option if it is not provided. An option cannot be both :required and have a :default.
  • :lazy_default
    CLIオプションが値なしに渡された場合だけに使われるデフォルト値.
    A default that is only passed if the cli option is passed without a value.
  • :desc
    オプションの説明. CLIの help hello を使って, コマンドの使い方を全部表示させたときに, この説明がオプションの隣に表示される.
    A description for the option. When printing out full usage for a command using cli help hello, this description will appear next to the option.
  • :required
    必須オプションであることを示す.
    Indicates that an option is required
  • :type
    :string, :hash, :array, :numeric, もしくは :boolean (詳しくは後述)
    :string, :hash, :array, :numeric, or :boolean (see below for more details)
  • :enum
    オプションに許される値のリスト.
    A list of allowed values for this option.

method_options のタイプ Types for method_options

  • :boolean--option or --option=true としてパースされる
  • :string--option=VALUE としてパースされる
  • :numeric--option=N としてパースされる
  • :array--option=one two three としてパースされる
  • :hash--option=name:string age:integer としてパースされる
  • :boolean is parsed as --option or --option=true
  • :string is parsed as --option=VALUE
  • :numeric is parsed as --option=N
  • :array is parsed as --option=one two three
  • :hash is parsed as --option=name:string age:integer

デフォルト値 Default Values

method_option はデフォルト値を指定できる. たとえば:
method_option allows a default value to be given. Example:
method_option :value, :default => "some value"
#=> Creates a string option with a default value of "some value"
#=> デフォルト値が "some value" であるような 文字列タイプのオプション: --valueを作る.
また, (訳注: methodoption_sメソッドを使って, )オプション名に対してある値をデフォルトとして指定できる. たとえば:
You can also specify the default as a value to the option name. Examples:
# 訳注: 以下の例は, method_option*S*であることに注意.

method_options :force => false
#=> デフォルト値がfalseであるbooleanタイプのオプション: --forceを作る
#=> Creates a boolean option with default value false

method_options :aliases => "bar"
#=> エイリアス'bar'を設定する
#=> Creates an alias 'bar'

method_options :threshold => 3.0
#=> デフォルト値が3.0である数値タイプのオプション: --thresholdを作る
#=> Creates a numeric option with default value 3.0
あなたは(訳注: method_optionSを使っている場合) :option => :required を与えることで, オプション —option を必須のものとして設定することができる. このときのオプションのタイプは文字列とみなされる. (訳注: このときは—optionが必須のものであるという設定しかできないが)もし, オプションとして, 必須であり, かつデフォルト値をもったHash変数がほしい場合は, method_option を使った, より宣言的なスタイルを使うことができる (訳注: 下記のコードは期待した動作をしない. :required=>trueは無視されthor app nameは—attributes無しでもエラーなしで実行される. thor version 0.19.4で確認):
You can also supply :option => :required to mark an option as required. The
type is assumed to be string. If you want a required hash with default values
as option, you can use method_option which uses a more declarative style:
method_option :attributes, :type => :hash, :default => {}, :required => true
すべての引数argumentsは, no- もしくは skip- の変形引数を与えることで, nilに セットできる (必須の引数を除く). たとえば:
All arguments can be set to nil (except required arguments), by supplying a no- or
skip-variant. For example:
thor app name --no-attributes
以前のバージョンではオプションのエイリアスは自動で生成されたが, 今は, それらは明示的でなければならない. 下記のように, method_optionSを使った簡略スタイルでもmethod_optionを使った宣言的なスタイルでも, どちらででも与えることが出来る:
In previous versions, aliases for options were created automatically, but now
they should be explicit. You can supply aliases in both short and declarative
styles:
method_options %w( force -f ) => :boolean
または:
Or:
method_option :force, :type => :boolean, :aliases => "-f"
エイリアスはお望みの通りいくつでも与えることができる.
You can supply as many aliases as you want.
注意: Thorは1ダッシュ-1文字のオプション群という ひとつの 規則 に従う. よって “-something”のようなエイリアスはパースされない; “—something” もしくは “-s” を代わりに使うこと.
Note: Thor follows a convention of one-dash-one-letter options. Thus aliases like “-something” wont be parsed; use either “\—something” or “-s” instead.
注意: Thor 0.9.0 にあった:optionalタイプは不許可になった. 代わりに :string もしくは :boolean を使うこと.
NOTE: Type :optional available in Thor 0.9.0 was deprecated. Use :string or :boolean instead.

Thor はじめに Getting Started

始めるにあたり, thorについて知らなければならない2,3のことがある. 例えば, タスクをロードするためのファイル命名規則や, 通常はコマンドラインで thor コマンドによってあなたのスクリプトを呼び出すことである (これは他にも方法がある)
There are a few things that you need to know about thor to get started. For example, it uses a file name convention to load tasks, and you typically call your scripts via the thor command line, though this is not the only option.

ファイル名 File Names

thorタスクをひとつ作るには, 最初に, 拡張子 .thor をもつファイル (Thorfile というファイルでもよい) がひとつ必要となる. このファイルには, thorが提供する機能を含んだ任意の標準のRubyコードを, 入れることができる. thor コマンドライン・ツールが実行されると, もろもろの .thor ファイルを探し, ロードして, あなたが作成したタスク群が使用できるようになる.
To create a thor task, the first thing you need is a file ending with the .thor extension (a file named Thorfile works as well). This file can contain any standard ruby code, including the functionality provided by thor. When the thor command line tool is run, it will look for .thor files and load them up, making your tasks available.

シンプルな例 A Simple Example

test.thor というファイルを作って, 以下のコードを書く:
Create a file named test.thor and place the following code in it
class Test < Thor
  desc "example", "an example task"
  def example
    puts "I'm a thor task!"
  end
end
あなたは一旦これを作りさえすれば, コマンドラインから thor list を実行することで, 以下のような出力が得られる:
Once you have this created, you can run thor list from the command line and you should see the following output:
test
----
thor test:example  # an example task
さあ, コマンドラインから thor test:example を実行しよう. I'm a thor task! という出力が得られるはずだ.
Now run thor test:example from the command line. You should see I'm a thor task! output to the command line.

サンプルを深く理解する Breaking Down The Sample

このタスクには, 何が起きているかを理解するために試せる, 5,6個のパーツがある.
There are several parts of this task that can be examined to understand what is happening.
最初に, クラス名はタスクの名前空間として使われる. Thorは クラス名を名前空間に変えるために, もろもろのRuby標準命名規則を使う. 例えば, あなたがクラス名を FooBar に変えた場合, タスクはこのとき foo_bar:example としてリストされる.
First, the class name is used as the namespace for the task. Thor uses some standard ruby conventions to turn the class name into a namespace. If you change the class name to FooBar, for example, your task would now be listed as foo_bar:example.
次に, desc メソッドが使われているが, これはわれわれが作成しようとしているタスクを記述するのためのものだ. descメソッドには ふたつのパラメータ が ある. 第1のパラメータは 定義するタスク(メソッド)の名称を明示するためのもので, 任意のパラメータ群をそのメソッドに与えられる. 第2のパラメータは プレーンテキストで, このタスクが何をするものなのか説明するためのものだ.
Next, the desc method call is used to describe the task that we are creating. There are two parameters to the desc method. The first parameter should state the name of the task (the method) and any parameters that are provided to the method. The second option should be a plain text description of what this task does.
最後は, example メソッドそのものだ. Thorはこのメソッド名を実際のタスク名として使用する. それゆえ, exampleメソッドを widget にリネームした場合, タスクは test:widget としてリストされることになる.
Last, the example method itself. Thor uses this method name as the actual task name. Therefore, when you rename the example method to widget, you end up with a task listing of test:widget.

タスクのパラメータ Task Parameters

以下のthorタスクは, パラメータをひとつメソッドに加えることによって, タスクにパラメータをひとつ定義する:
The following thor task defines one parameter on the task by adding a parameter to the method:
class Test < Thor
  desc "example FILE", "an example task that does something with a file"
  def example(file)
    puts "You supplied the file: #{file}"
  end
end
descの第1パラメータを’FILE’という語を含むものにアップデートしたことに, 注意してほしい. 前述のように, タスクの記述にはタスク・パラメータを任意に含めてよい. これはエンドユーザが使いやすいようにヘルプ・テキストを提供するものだ.
Note that the description has been updated to include the word ‘FILE’ in the first parameter. As stated previously, you should include the name of any required task parameters in the task description. This is used to produce help text that is useful to the end user.
thor help test:example を実行してtest:exampleタスクのヘルプテキストを見てみると, 以下のような出力を見れるだろう:
Run thor help test:example to see the help text for the test:example task and you will see the following output:
Usage:
  thor test:example FILE

an example task that does something with a file
これはそのタスクにFILEパラメータが必要であることを示す.
This indicated the requirement of a FILE parameter for the task.
thor test:example my_file.rb でタスク自体を実行した場合, You supplied the file: my_file.rb と目にするはずだ.
Now when you run the task itself, with thor test:example my_file.rb you should see You supplied the file: my_file.rb.
もしファイル名を与えずにスクリプトを実行すれば, ひとつ与える必要があるという "example" was called incorrectly. Call as "thor test:example FILE". のメッセージを目にするだろう.
If you run the script without a file supplied, you’ll see a message saying you need to supply one: "example" was called incorrectly. Call as "thor test:example FILE".

タスクのオプション Options For A Task

Thorはまた, ひとつのタスクに対して, もろもろの名前付けしたオプションを与えることもできる. いくつかのメソッドがこれを行っており, より詳細については [[method options]] のWikiページを参照してほしい.
Thor also lets you provide named options for a task. There are several methods of doing this, and more detail can be found on the [[method options]] wiki page.
あなたのtest.thorファイルに以下を書いてみよ:
Put the following into your test.thor file:
class Test < Thor
  desc "example FILE", "an example task"
  method_option :delete, :aliases => "-d", :desc => "Delete the file after parsing it"
  def example(file)
    puts "You supplied the file: #{file}"
    delete_file = options[:delete]
    if delete_file
      puts "You specified that you would like to delete #{file}"
    else
      puts "You do not want to delete #{file}"
    end
  end
end
この例では, 5,6個のパラメータとともに method_option がひとつ指定されている. 第1のパラメータはオプションのフルネームだ. これはコマンドライン上で —オプションひとつに変換される. このケースでは --delete だ. :aliases オプションは, このオプションのために, ひとつかそれ以上の省略形のエイリアス群を提供する. このケースでは -d が省略形として提供される. 最後に :desc でこのオプションの説明を与えた.
In this example, a method_option has been supplied with several parameters. The first parameter is the full name of the option. this is translated into a — option on the command line. In this case, --delete. The :aliases option allows you to provide one or more short-hand aliases for the option. In this case, -d has been provided as a short-hand alias. And last, a description of the option has been provided.
thor help test:example を実行すると, 以下の出力がみれる:
Run thor help test:example and you will see the following output:
Usage:
  thor test:example FILE

Options:
  -d, [--delete=DELETE]  # Delete the file after parsing it

an example task
今, このタスクを —delete オプション有りでも無しでも実行できる.
Now you can run the task with or without the delete option.
thor test:example my_file.rb を実行すると, 以下のようになる:
Run thor test:example my_file.rb, you will see this output:
You supplied the file: my_file.rb
You do not want to delete my_file.rb
thor test:example my_file.rb --delete もしくは thor test:example my_file.rb -d で実行してみると:
Run thor test:example my_file.rb --delete or thor test:example my_file.rb -d and you will see:
You supplied the file: my_file.rb
You specified that you would like to delete my_file.rb
作成したタスク・メソッドひとつにつき, 多くのメソッド・オプションを必要なだけ指定できる. これらをメソッド名に対して積み上げていくだけで, タスク内に現れるだろう.
You can specify as many method options as you need, for a given task method. Just keep piling them on to the method name and they will show up in the task.

ハサミを持って突っ走る Running With Scissors

ここでの諸例は単純で不自然だったが, 今はなにかしら, 実際のthorタスクの機能を使ってみることができるだろう. thorのさまざまなオプションや能力のドキュメントについては, 他のwikiページ (訳注: githubにあるThorのWikiのこと. この訳文群のこと) もチェックしてみること.
Though the examples here are simple and contrived, you should be able to get started with some real functionality for your thor tasks, now. Be sure to check the other wiki pages for documentation on the various options and capabilities of thor.

2014年5月19日月曜日

brew のバージョンに関する部分のソースを読んでみた

前回の記事 を書くにあたって CLT のバージョンの調べ方について調査し、パッケージ管理ツールのひとつである Homebrew による CLT バージョンの取得方法について、github のソースを探索したときのログです。
String#[/re/], extend を使ったモジュールのメソッド定義、定数のエイリアスといった、ちょっとおもしろい記述方法を見つけました。

2014年5月11日日曜日

Mac の Gimp 2.8.10 でヘルプを使えるようにするには

概要

前回、「Mac OSX 10.9 (Mavericks) で 日本語入力可能な日本語化された Gimp 最新版 (2.8.10) を使うには」 にて、 Mac OS X 10.9.x (Mavericks) にインストールした GIMP 2.8.10 は、エラーによりヘルプドキュメントが見れない状態であることがわかった。そこで本記事では、GIMP の使用している GTK に MimeType の設定を行うことにより、(日本語) ヘルプドキュメントを使用できるようにする。これにより、パッチあてやコンパイルなしに、Mac 用の GIMP 2.8.10 で、以下のようなヘルプメニューを使えるようになる: GIMP のヘルプメニューの ヘルプ (Help)F1 をおした時の状況適応ヘルプ (Context Help)今日の技 (Tip of the day)。付録として解析したときのログも掲載した。
この手法は日本語以外の外国語においても有効なはずである。(This solution must be available in any foreign languages other than japanese.)

手法と手順

本記事の手法は、RPM で提供されている GIMP の日本語ヘルプドキュメントを GIMP.app に組み込み、GIMP 内部の GTK の設定に MimeType の追加をすることで、インストール版のヘルプドキュメントを利用可能にするというものです。
以下の手順でおこないます:
  1. 日本語のローカル版ヘルプドキュメントを GIMP.app に追加する
  2. GIMP.app のヘルプの設定
  3. MimeType の設定を GIMP.app が使用している GTK の設定に追加
詳細は、下記のようになります。

1. 日本語のローカル版ヘルプドキュメントを GIMP.app に追加する

前回用意した状態だと、GIMP.app は、一切のヘルプドキュメントを持っていない状態です。なので所定の場所に、ヘルプのデータ (html, 画像) を、ぶち込みます。
GIMP のヘルプドキュメントは別パッケージで提供されていたりします。しかしながら、これをコンパイルするのは結構な手間です。
だから、Linux のディストリビューションで提供されているコンパイル済みパッケージである RPM から、中身を吸い出して流用します。この手順は以下のように行います:
  1. RPM Rawhide gimp-help-ja 2.8.1 noarch rpm などを、適当なディレクトリにダウンロードする
  2. rpm2cpio.pl コマンドをインストールする
    ターミナルから、brew install rpm2cpio (brew の場合) を実行する
  3. RPM をファイルに展開する
    ターミナルから、
    rpm2cpio.pl gimp-help-ja-2.8.1-1.fc21.noarch.rpm | cpio -id
  4. 展開されたデータの help/ 以下を、GIMP.app にコピーする
    前の手順で、カレント・ディレクトリに usr/ 以下に展開されたデータができる。この中にある help ディレクトリを丸ごとコピーする。コピー先は、
    /Applications/GIMP.app/Contents/Resources/share/gimp/2.0/
    で、ここに brushes, dynamics, fonts, …などとおなじように help が配置されるようにする。
※ GIMP.app の中身の開き方は、Finder から /Applications/GIMP.app を選んで、右クリック > パッケージの内容を表示 です。

2. GIMP.app のヘルプの設定

GIMP.app のヘルプ表示の仕方を、ローカルを参照し、また通常の Web ブラウザを使用するように設定します。手順は以下のとおりです:
  1. メニューのGIMP > 設定 (Preferences)… を選ぶ
  2. GIMP の設定 ウィンドウが出てくる
  3. 左ペインのアイコンのリストから、上から 4 番目の ヘルプ を選択
  4. 右ペインの 全般 にある 使用するユーザーマニュアル から、インストール版 を選択
  5. 同じく、ヘルプブラウザー使用するヘルプブラウザー から、ウェブブラウザ を選択
  6. OK ボタンを押して GIMP の設定 ウィンドウを閉じる
  7. GIMP を終了する

確認方法

GIMP を起動して、メニューから GIMP > 設定 > ヘルプ を見てください。
ここまでの手順がうまくできていれば、「電球ユーザーマニュアルはインストール済みです。」という表示が出ます (でも、次の手順が済むまではまだ使えません)。

3. Mime タイプの設定を GIMP.app が使用している GTK の設定に追加

上記までの設定で、GIMP はヘルプファイルを認識します。ユーザが、メニューからヘルプを表示しようとすると、GIMP は、plug-ins/web-browser でドキュメントを開こうとします。ところが、GIMP が使用している GTK がこれを弾いてしまい、下記のようなエラーとなります:
GIMP エラーメッセージ
プロシージャー 'plug-in-web-browser' の呼出し時にエラーが発生しました。
No application is registered as handling this file
Script-Fu から、(plug-in-web-browser "file://localhost/Applications/GIMP.app/Contents/Resources/share/gimp/2.0/help/ja/index.html") として同じエラーを再現できることから、URI のスキーム指定 file: を扱えないことが、原因であることが予想されます。
そこで、GTK がエラーを吐かないように、mimeinfo.cacheopen.desktop の 2 つのファイルに MimeType の追加をする修正を行います。これらのファイルたちが格納されている場所は、/Applications/GIMP.app/Contents/Resources/share/applications です。
まずはターミナルを開いて、カレントディレクトリをこの場所にしてください。
作業の手順は以下のとおりです:
  1. 元のファイルをバックアップします
    cp -p mimeinfo.cache mimeinfo.cache.orig
    cp -p open.desktop open.desktop.orig
  2. mimeinfo.cache の修正
    エディタ (vim, emacs, nano など) で、 mimeinfo.cache の最後に一行x-scheme-handler/file=open.desktop; を追加します
  3. mimeinfo.cache の修正の確認
    $ diff mimeinfo.cache.orig mimeinfo.cache
    32a33
    > x-scheme-handler/file=open.desktop;
  4. open.desktop の修正
    MimeType の設定に、x-scheme-handler/file; を追加します
  5. open.desktop の修正の確認
    $ diff open.desktop.orig open.desktop 7c7 < MimeType=x-scheme-handler/http;x-scheme-handler/https --- > MimeType=x-scheme-handler/file;x-scheme-handler/http;x-scheme-handler/https
これで、GIMP で日本語のヘルプが使えるようになりました!Yay!
GIMP を起動して、メニューから ヘルプ > ヘルプ などを試してみてください。Web ブラウザが立ち上がって、ヘルプのHTMLファイルが表示されます。今日の技 で表示されるウィンドウで 詳しくはこちら を押すと、ブラウザでおなじようにリンクが表示されます。

まとめ

  • GIMP.app に日本語のヘルプドキュメントを組み込む方法を述べた
  • GIMP.app にローカルにインストールしたインストール版ヘルプドキュメントを見るための設定方法を述べた
  • GIMP の使用している GTK に MimeType の設定を追加する方法を述べた
以上です。
以降は、トラブルシューティングしたい場合のなんかの参考に。

PS. Mac OS X での GIMP の設定ファイルの場所

システム側

GIMP.app の中の
  • Resources/etc
  • Resources/share/applications
  • Resources/share/gimp/2.0
などなどにある。

ユーザ側

  • ~/Library/Application\ Support/GIMP/2.8/
の中に、いろいろな rc ファイルが有る。