DSLでコマンドラインオプションを解析するchoice.rb
yasnippet.elについて調べていたら、おもしろいライブラリをみつけたので紹介。
choiceというライブラリは、コマンドラインオプションを解析するDSL、いわばoptparseのDSL版と考えることができる。
使用例はこんな感じ。 明白すぎてコードのコメントは不要だろう。
RubyGemsパッケージが用意されているので、「gem install choice」で簡単インストール。
require 'choice' def parse_options # cited from # http://yasnippet.googlecode.com/svn/trunk/extras/textmate_import.rb Choice.options do header '' header 'Standard Options:' option :snippet_dir do short '-d' long '--snippet-dir=PATH' desc 'Tells the program the directory to find the TextMate Snippets' default '.' end option :output_dir do short '-o' long '--output-dir=PATH' desc 'What directory to write the new YASnippets to' end option :snippet do short '-f' long '--file=SNIPPET FILE NAME' desc 'A specific snippet that you want to copy or a glob for various files' default '*.{tmSnippet,plist}' end option :print_pretty do short '-p' long '--pretty-print' desc 'Pretty prints multiple snippets when printing to standard out' end option :convert_bindings do short '-b' long '--convert-bindings' desc "TextMate \"keyEquivalent\" keys are translated to YASnippet \"# binding :\" directives" end option :info_plist do short '-g' long '--info-plist' desc "Attempt to derive group information from \"info.plist\" type-file PLIST" end separator '' separator 'Common options: ' option :help do long '--help' desc 'Show this message' end end end # コマンドラインオプションに --snippet-dir /usr -o /tmp を指定した場合を想定する ARGV.replace %w[--snippet-dir /usr -o /tmp] parse_options c = Choice.choices # => {"snippet_dir"=>"/usr", "output_dir"=>"/tmp", "snippet"=>"*.{tmSnippet,plist}"} c.class # => Choice::LazyHash c.snippet_dir # => "/usr" c.snippet # => "*.{tmSnippet,plist}" c.output_dir # => "/tmp"