そこで、Ruby で Build Awesome Command-Line Applications を勉強しています。81 ページでは、STDIN を使用して複数のタスクをプロジェクトに入力することになっています。
File.open(global_options[:filename], 'a+') do |todo_file|
if task_names.empty?
puts "Reading new tasks from stdin..."
task_names = STDIN.readlines.map {|a| a.chomp}
end
tasks = 0
task_names.each do |task|
todo_file.puts [task, Time.now].join(', ')
tasks+=1
end
if tasks == 0
raise "You must provide tasks on the command-line or standard input"
end
end
プロジェクトにタスクを入力する通常の方法は次のようなもの$todo new "Rake leaves
ですが、上記のコードを使用すると、以下の例の内容を実行できます。
それは機能します。しかし、STDIN にリスニングを停止するように指示するにはどうすればよいでしょうか。それを使用する方法の例はこれです...
$ todo new
Rake leaves
Take out trash
Clean garage
Put away dishes
^D
は何^D
を表しているのですか?