[前][次][番号順一覧][スレッド一覧]

ruby-changes:3144

From: ko1@a...
Date: 25 Dec 2007 06:21:30 +0900
Subject: [ruby-changes:3144] gotoyuzo - Ruby:r14636 (trunk): * sample/biorhythm.rb: follow Ruby 1.9 libraries.

gotoyuzo	2007-12-25 06:21:16 +0900 (Tue, 25 Dec 2007)

  New Revision: 14636

  Modified files:
    trunk/ChangeLog
    trunk/sample/biorhythm.rb

  Log:
    * sample/biorhythm.rb: follow Ruby 1.9 libraries.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/sample/biorhythm.rb?r1=14636&r2=14635
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14636&r2=14635

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 14635)
+++ ChangeLog	(revision 14636)
@@ -1,3 +1,7 @@
+Tue Dec 25 06:19:04 2007  GOTOU Yuuzou  <gotoyuzo@n...>
+
+	* sample/biorhythm.rb: follow Ruby 1.9 libraries.
+
 Tue Dec 25 06:15:01 2007  Koichi Sasada  <ko1@a...>
 
 	* vm.c: add dummy toplevel frame.
Index: sample/biorhythm.rb
===================================================================
--- sample/biorhythm.rb	(revision 14635)
+++ sample/biorhythm.rb	(revision 14636)
@@ -27,22 +27,9 @@
 
 include Math
 require "date.rb"
-require "parsearg.rb"
-require "parsedate.rb"
+require "optparse"
+require "optparse/date"
 
-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'
-
 def printHeader(y, m, d, p, w)
   print "\n>>> Biorhythm <<<\n"
   printf "The birthday %04d.%02d.%02d is a %s\n", y, m, d, w
@@ -58,60 +45,52 @@
   return phys, emot, geist
 end
 
-def parsedate(s)
-  ParseDate::parsedate(s).values_at(0, 1, 2)
+def prompt(msg)
+  $stderr.print msg
+  return gets.chomp
 end
 
-def name_of_week(date)
-  Date::DAYNAMES[date.wday]
-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
+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
-  if !bd
-    STDERR.print "BAD Input Birthday!!\n"
-    exit()
-  end
+end
 
-  if $OPT_sdate
-    dd = Date.today
-  elsif $OPT_date
-    dd = Date.new(*parsedate($OPT_date))
-  else
-    STDERR.print("Date        [<RETURN> 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 @@
   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"

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml

[前][次][番号順一覧][スレッド一覧]