メソッドオプション Method Options
Thorでは, タスクにいろいろなオプションを指定できる; これには, もろもろのオプションのHash変数を与える
Thor allows you to specify options for its tasks, using
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は--optionor--option=trueとしてパースされる:stringは--option=VALUEとしてパースされる:numericは--option=Nとしてパースされる:arrayは--option=one two threeとしてパースされる:hashは--option=name:string age:integerとしてパースされる:booleanis parsed as--optionor--option=true:stringis parsed as--option=VALUE:numericis parsed as--option=N:arrayis parsed as--option=one two three:hashis parsed as--option=name:string age:integer
デフォルト値 Default Values
method_option はデフォルト値を指定できる. たとえば:
method_option allows a default value to be given. Example:
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:
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
type is assumed to be string. If you want a required hash with default values
as option, you can use
:option => :required to mark an option as required. Thetype 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:
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:
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:
Or:
method_option :force, :type => :boolean, :aliases => "-f"
エイリアスはお望みの通りいくつでも与えることができる.
You can supply as many aliases as you want.
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.
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タイプは不許可になった. 代わりに
NOTE: Type :optional available in Thor 0.9.0 was deprecated. Use
:string もしくは :boolean を使うこと.NOTE: Type :optional available in Thor 0.9.0 was deprecated. Use
:string or :boolean instead.