ClearBlog/lib/post/options_parse.rb

58 lines
1.2 KiB
Ruby

require "optparse"
require "date"
module Post
class Options
attr_reader :input, :title, :date, :colorize
def parse!
@colorize = true
OptionParser.new do |parser|
parser.banner = "Usage: post.rb [options]"
parser.on("--no-color", "Don't colorize output") do
@colorize = false
end
parser.on("-n", "--today", "Set date as today") do
@date = Date.today
end
parser.on("-d=DATE", "--date=DATE", "Set date") do |date|
@date = Date.parse(date)
end
parser.on("-t=TITLE", "--title=TITLE", "Set the title/topic") do |title|
@title = title
end
parser.on("-f=PATH", "--file=PAT", "Read a file as inut") do |path|
@input = File.read(path)
end
parser.on("--stdin", "Read the input from stdin") do
@input = nil
end
end.parse!
if @title && @date
raise "You may NOT choose a title AND a date"
end
fill_input!
self
end
def fill_input!
if !input
input = ""
while (line = $stdin.gets)
input << line
end
@input = input
end
self
end
end
end