From a560576158af1f502083a5f6e7bc496f336ce275 Mon Sep 17 00:00:00 2001 From: gotoyuzo Date: Mon, 24 Dec 2007 21:21:16 +0000 Subject: * sample/biorhythm.rb: follow Ruby 1.9 libraries. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14636 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- sample/biorhythm.rb | 115 ++++++++++++++++++---------------------------------- 1 file changed, 40 insertions(+), 75 deletions(-) (limited to 'sample/biorhythm.rb') diff --git a/sample/biorhythm.rb b/sample/biorhythm.rb index c7e26c4fff..f2dff503ee 100644 --- a/sample/biorhythm.rb +++ b/sample/biorhythm.rb @@ -27,21 +27,8 @@ include Math require "date.rb" -require "parsearg.rb" -require "parsedate.rb" - -def usage() - print "Usage:\n" - print "biorhythm.rb [options]\n" - print " options...\n" - print " -D YYYYMMDD(birthday) : use default values.\n" - print " --sdate | --date YYYYMMDD : use system date; use specified date.\n" - print " --birthday YYYYMMDD : specifies your birthday.\n" - print " -v | -g : show values or graph.\n" - print " --days DAYS : graph range (only in effect for graphs).\n" - print " --help : help\n" -end -$USAGE = 'usage' +require "optparse" +require "optparse/date" def printHeader(y, m, d, p, w) print "\n>>> Biorhythm <<<\n" @@ -58,60 +45,52 @@ def getPosition(z) return phys, emot, geist end -def parsedate(s) - ParseDate::parsedate(s).values_at(0, 1, 2) -end - -def name_of_week(date) - Date::DAYNAMES[date.wday] +def prompt(msg) + $stderr.print msg + return gets.chomp end # # main program # -parseArgs(0, nil, "vg", "D:", "sdate", "date:", "birthday:", "days:") - -if $OPT_D - dd = Date.today - bd = Date.new(*parsedate($OPT_D)) - ausgabeart = "g" -else - if $OPT_birthday - bd = Date.new(*parsedate($OPT_birthday)) - else - STDERR.print("Birthday (YYYYMMDD) : ") - unless (si = STDIN.gets.chop).empty? - bd = Date.new(*parsedate(si)) - end - end - if !bd - STDERR.print "BAD Input Birthday!!\n" - exit() +options = { + :graph => true, + :date => Date.today, + :days => 9, +} +ARGV.options do |opts| + opts.on("-b", "--birthday=DATE", Date, "specify your birthday"){|v| + options[:birthday] = v + } + opts.on("--date=DATE", Date, "specify date to show"){|v| + options[:date] = v + } + opts.on("-g", "--show-graph", TrueClass, "show graph (default)"){|v| + options[:graph] = v + } + opts.on("-v", "--show-values", TrueClass, "show values"){|v| + options[:graph] = !v + } + opts.on("--days=DAYS", Integer, "graph range (only in effect for graph)"){|v| + options[:days] = v - 1 + } + opts.on_tail("-h", "--help", "show this message"){puts opts; exit} + begin + opts.parse! + rescue => ex + puts "Error: #{ex.message}" + puts opts + exit end +end - if $OPT_sdate - dd = Date.today - elsif $OPT_date - dd = Date.new(*parsedate($OPT_date)) - else - STDERR.print("Date [ for Systemdate] (YYYYMMDD) : ") - unless (si = STDIN.gets.chop).empty? - dd = Date.new(*parsedate(si)) - end - end - dd ||= Date.today +bd = options[:birthday] || Date.parse(prompt("Your birthday (YYYYMMDD): ")) +dd = options[:date] || Date.today +ausgabeart = options[:graph] ? "g" : "v" +display_period = options[:days] - if $OPT_v - ausgabeart = "v" - elsif $OPT_g - ausgabeart = "g" - else - STDERR.print("Values for today or Graph (v/g) [default g] : ") - ausgabeart = STDIN.gets.chop - end -end if ausgabeart == "v" - printHeader(bd.year, bd.month, bd.day, dd - bd, name_of_week(bd)) + printHeader(bd.year, bd.month, bd.day, dd - bd, bd.strftime("%a")) print "\n" phys, emot, geist = getPosition(dd - bd) @@ -121,21 +100,7 @@ if ausgabeart == "v" printf "Mental: %d%%\n", geist print "\n" else - if $OPT_days - display_period = $OPT_days.to_i - elsif $OPT_D - display_period = 9 - else - STDERR.printf("Graph for how many days [default 10] : ") - display_period = STDIN.gets.chop - if display_period.empty? - display_period = 9 - else - display_period = display_period.to_i - 1 - end - end - - printHeader(bd.year, bd.month, bd.day, dd - bd, name_of_week(bd)) + printHeader(bd.year, bd.month, bd.day, dd - bd, bd.strftime("%a")) print " P=physical, E=emotional, M=mental\n" print " -------------------------+-------------------------\n" print " Bad Condition | Good Condition\n" -- cgit v1.2.3