\section{結果} \subsection{開発の結果} 開発の結果コマンドを以下のように書き換えることができた \begin{table}[htbp]\begin{center} \caption{} \begin{tabular}{llll} \hline 変更前 &変更後 &コマンド一覧 \\ \hline add & &add sources info \\ checkdb & &check database file \\ datebase FILE & &read datebase file \\ display FILE &  &display converted hikifile \\ edit FILE &open &open file \\ euc FILE & &translate file to euc \\ help [COMMAND] & &Describe available commands or one specific command \\ list [FILE] &ls &list files \\ move [FILE] &mv| move file \\ remove [FILE] &rm &remove files \\ rsync & &rsync files \\ show & &lsとかぶる. \\ target VAL &cd &set target id \\ update FILE &touch &update file \\ version & &show program version \\ \hline \end{tabular} \label{default} \end{center}\end{table} %for inserting separate lines, use \hline, \cline{2-3} etc. hikiutilsのコマンドライン解析ツールをoptparseからthorに換えることでコマンドの書き換えを行うことができた. また,プログラムのコードもoptparseよりthorのほうが短く書け,簡単にコマンドを作成することができた. \subsection{thorとoptparseのコードの比較} \subsubsection{Thorとは} Thorとは,コマンドラインツールの作成を支援するライブラリのことである. gitやbundlerのようにサブコマンドを含むコマンドラインツールを簡単に作成することができる[4]. \paragraph{Thorの基本的な流れ} \begin{enumerate} \item Thorを継承したクラスのパブリックメソッドがコマンドになる \item クラス.start(ARGV)でコマンドラインの処理をスタートする \end{enumerate} という流れである[4]. \subsubsection{optparseとは} optparseモジュールとは,getoptよりも簡便で,柔軟性に富み,かつ強力なコマンドライン解析ライブラリである. optparseでは,より宣言的なスタイルのコマンドライン解析手法,すなわちOptionParserのインスタンスでコマンドラインを解析するという手法をとっている. これを使うと,GNU/POSIX構文でオプションを指定できるだけでなく,使用法やヘルプメッセージの生成も行える[5]. \paragraph{optparseの基本的な流れ} \begin{enumerate} \item OptionParserオブジェクトoptを生成する \item オプションを取り扱うブロックをoptに登録する \item opt.parse(ARGV)でコマンドラインを実際にparseする \end{enumerate} という流れである[6]. \subsubsection{コードの解説} \paragraph{Thorの定義} \begin{figure}[htbp]\begin{center} \includegraphics[width=6cm,bb=0 0 442 500]{../figs/./hikiutils_yamane.003.jpg} \caption{} \label{default}\end{center}\end{figure} \begin{enumerate} \item Hikithor::CLI.start(ARGV)が呼ばれる \item initializeメソッドが呼ばれる \item これではThorのinitializeメソッドが呼ばれない \item superを書くことでThorのinitializeメソッドが呼ばれる \end{enumerate} optparseではrequireでoptparseを呼ぶだけでいいが,ThorではrequireでThorを呼びCLIクラスで継承しinitializeメソッドにsuperを書くことでThorのinitializeを呼ぶ必要がある. \paragraph{実際のコード}\begin{lstlisting}[style=customRuby] # -*- coding: utf-8 -*- require 'thor' require 'kconv' require 'hikidoc' require 'erb' require "hikiutils/version" require "hikiutils/tmarshal" require "hikiutils/infodb" require 'systemu' require 'fileutils' require 'yaml' require 'pp' module Hikithor DATA_FILE=File.join(ENV['HOME'],'.hikirc') attr_accessor :src, :target, :editor_command, :browser, :data_name, :l_dir class CLI < Thor def initialize(*args) super @data_name=['nick_name','local_dir','local_uri','global_dir','global_uri'] data_path = File.join(ENV['HOME'], '.hikirc') DataFiles.prepare(data_path) file = File.open(DATA_FILE,'r') @src = YAML.load(file.read) file.close @target = @src[:target] @l_dir=@src[:srcs][@target][:local_dir] browser = @src[:browser] @browser = (browser==nil) ? 'firefox' : browser p editor_command = @src[:editor_command] @editor_command = (editor_command==nil) ? 'open -a mi' : editor_command end \end{lstlisting} \paragraph{hikiutilsの実行} \begin{itemize} \item Thor \end{itemize} \begin{figure}[htbp]\begin{center} \includegraphics[width=6cm,bb=0 0 442 500]{../figs/./hikiutils_yamane.004.jpg} \caption{} \label{default}\end{center}\end{figure} \begin{enumerate} \item hiki\_thorのHikithor::CLI.start(ARGV)でhikiutils\_thor.rbのCLIクラスを呼ぶ\verb|{{br}}| \item hikiutils\_thor.rbのCLIクラスのメソッドを順に実行していく\verb|{{br}}| \end{enumerate} \begin{itemize} \item optparse \end{itemize} \begin{figure}[htbp]\begin{center} \includegraphics[width=6cm,bb=0 0 442 500]{../figs/./hikiutils_yamane.001.jpg} \caption{} \label{default}\end{center}\end{figure} \begin{enumerate} \item HikiのHikiUtils::Command.run(ARGV)でhikiutils.rbのrunメソッドを呼ぶ \item new(argv).executeでexecuteメソッドが実行される \end{enumerate} このようにoptparseでは実行を行うためのメソッドが必要であるが,Thorではクラスのメソッドを順に実行していくため runメソッドとexecuteメソッドは必要ない. \paragraph{実際のコード} \begin{itemize} \item Thor \end{itemize}\begin{lstlisting}[style=customRuby] #!/usr/bin/env ruby require "hikiutils_thor" Hikithor::CLI.start(ARGV) \end{lstlisting}\begin{lstlisting}[style=customRuby] # -*- coding: utf-8 -*- require 'thor' require 'kconv' require 'hikidoc' require 'erb' require "hikiutils/version" require "hikiutils/tmarshal" require "hikiutils/infodb" require 'systemu' require 'fileutils' require 'yaml' require 'pp' module Hikithor DATA_FILE=File.join(ENV['HOME'],'.hikirc') attr_accessor :src, :target, :editor_command, :browser, :data_name, :l_dir class CLI < Thor def initialize(*args) super @data_name=['nick_name','local_dir','local_uri','global_dir','global_uri'] data_path = File.join(ENV['HOME'], '.hikirc') DataFiles.prepare(data_path) file = File.open(DATA_FILE,'r') @src = YAML.load(file.read) file.close @target = @src[:target] @l_dir=@src[:srcs][@target][:local_dir] browser = @src[:browser] @browser = (browser==nil) ? 'firefox' : browser \end{lstlisting} \begin{itemize} \item optparse \end{itemize}\begin{lstlisting}[style=customRuby] #!/usr/bin/env ruby require "hikiutils" HikiUtils::Command.run(ARGV) \end{lstlisting}\begin{lstlisting}[style=customRuby] def self.run(argv=[]) print "hikiutils: provide utilities for helping hiki editing.\n" new(argv).execute end def execute @argv << '--help' if @argv.size==0 command_parser = OptionParser.new do |opt| opt.on('-v', '--version','show program Version.') { |v| opt.version = HikiUtils::VERSION puts opt.ver } opt.on('-s', '--show','show sources') {show_sources} opt.on('-a', '--add','add sources info') {add_sources } opt.on('-t', '--target VAL','set target id') {|val| set_target(val) } opt.on('-e', '--edit FILE','open file') {|file| edit_file(file) } opt.on('-l', '--list [FILE]','list files') {|file| list_files(file) } opt.on('-u', '--update FILE','update file') {|file| update_file(file) } opt.on('-r', '--rsync','rsync files') {rsync_files} opt.on('--database FILE','read database file') {|file| db_file(file)} opt.on('--display FILE','display converted hikifile') {|file| display(f\ ile)} opt.on('-c', '--checkdb','check database file') {check_db} opt.on('--remove FILE','remove file') {|file| remove_file(file)} opt.on('--move FILES','move file1,file2',Array) {|files| move_file(file\ s)} opt.on('--euc FILE','translate file to euc') {|file| euc_file(file) } opt.on('--initialize','initialize source directory') {dir_init() } end begin command_parser.parse!(@argv) rescue=> eval p eval end dump_sources exit end \end{lstlisting} \paragraph{コマンドの表示と実行} \begin{itemize} \item Thor \end{itemize} \begin{figure}[htbp]\begin{center} \includegraphics[width=6cm,bb=0 0 442 500]{../figs/./hikiutils_yamane.002.jpg} \caption{} \label{default}\end{center}\end{figure} \begin{enumerate} \item コマンド名,コマンドの説明を一覧に表示させる \item パブリックメソッドのコマンドを別のコマンド名でも実行できるようにする \item コマンドの命令の実行コード \end{enumerate} \begin{itemize} \item optparse \end{itemize} \verb|{{attach_view(hikiutils_yamane.005.jpg)}}| よって,optparseではOptionParserオブジェクトoptを生成を行い,コマンドをoptに登録して一覧に表示するメソッドとそれぞれコマンドの実行処理が書かれたメソッドがあるが, thorではそれぞれのdescで一覧を表示しmapとパブリックメソッドでコマンドの実行処理を行うためコードが短くなる. \paragraph{実際のコード} \begin{itemize} \item Thor \end{itemize}\begin{lstlisting}[style=customRuby] desc 'show,--show', 'show sources' map "--show" => "show" def show printf("target_no:%i\n",@src[:target]) printf("editor_command:%s\n",@src[:editor_command]) @i_size,@n_size,@l_size,@g_size=3,5,30,15 #i,g_size are fixed n_l,l_l=0,0 @src[:srcs].each_with_index{|src,i| n_l =(n_l= src[:nick_name].length)>@n_size? n_l:@n_size l_l =(l_l= src[:local_dir].length)>@l_size? l_l:@l_size } @n_size,@l_size=n_l,l_l command = Command.new header = command.display_format('id','name','local directory','global uri',@i_size,@n_size,@l_size,@g_size) puts header puts '-' * header.size @src[:srcs].each_with_index{|src,i| target = i==@src[:target] ? '*':' ' id = target+i.to_s name=src[:nick_name] local=src[:local_dir] global=src[:global_uri] puts command.display_format(id,name,local,global,@i_size,@n_size,@l_size,@g_size) } end \end{lstlisting} \begin{itemize} \item optparse \end{itemize}\begin{lstlisting}[style=customRuby] def execute @argv << '--help' if @argv.size==0 command_parser = OptionParser.new do |opt| opt.on('-v', '--version','show program Version.') { |v| opt.version = HikiUtils::VERSION puts opt.ver } opt.on('-s', '--show','show sources') {show_sources} opt.on('-a', '--add','add sources info') {add_sources } opt.on('-t', '--target VAL','set target id') {|val| set_target(val)} opt.on('-e', '--edit FILE','open file') {|file| edit_file(file) } opt.on('-l', '--list [FILE]','list files') {|file| list_files(file)} opt.on('-u', '--update FILE','update file') {|file| update_file(file) } opt.on('-r', '--rsync','rsync files') {rsync_files} opt.on('--database FILE','read database file') {|file| db_file(file)} opt.on('--display FILE','display converted hikifile') {|file| display(file)} opt.on('-c', '--checkdb','check database file') {check_db} opt.on('--remove FILE','remove file') {|file| remove_file(file)} opt.on('--move FILES','move file1,file2',Array) {|files| move_file(files)} opt.on('--euc FILE','translate file to euc') {|file| euc_file(file)} opt.on('--initialize','initialize source directory') {dir_init() } end begin command_parser.parse!(@argv) rescue=> eval p eval end dump_sources exit end def show_sources() printf("target_no:%i\n",@src[:target]) printf("editor_command:%s\n",@src[:editor_command]) check_display_size() header = display_format('id','name','local directory','global uri') puts header puts '-' * header.size @src[:srcs].each_with_index{|src,i| target = i==@src[:target] ? '*':' ' id = target+i.to_s name=src[:nick_name] local=src[:local_dir] global=src[:global_uri] puts display_format(id,name,local,global) } end def add_sources cont = {} @data_name.each{|name| printf("%s ? ", name) tmp = gets.chomp cont[name.to_sym] = tmp } @src[:srcs] << cont show_sources end \end{lstlisting}