Sha256: 05bf8a9bad6ad8c2887b837afd2d01dad71c495cf7d3e09eb0a988adda427b2e

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

require 'optparse'
require 'optparse/time'

module Sredder
  class ArgParser

    def options
      @options ||= defaults
    end

    def valid?
      options[:date]    &&
      options[:title]   &&
      options[:message] &&
      options[:hours]
    end

    # Print the usage and quit
    def validate!
      parse(['--help']) unless valid?
    end

    def parse(args)

      OptionParser.new do |opts|

        opts.banner = "Usage: sredder [options]"

        opts.on("-t", "--title TITLE",
                "The TITLE field to add to the wrike task") do |title|
          options[:title] = title
        end

        opts.on("-m", "--message MESSAGE",
                "The MESSAGE field to add to the wrike task") do |message|
          options[:message] = message
        end

        opts.on("-h", "--hours N", Float, "The number of HOURS spent on the task") do |n|
          options[:hours] = n
        end

        opts.on("-d", "--date DATE", Time, "The DATE (YY/MM/DD) on which the task was performed (default today)") do |date|
          options[:date] = Util.time_stamp(date)
        end

        opts.on("-f", "--folder FOLDER", "The wrike FOLDER in which to place the task (default #{defaults[:folder]})") do |folder|
          options[:folder] = folder
        end

        opts.on_tail("--help", "Show this message") do
          puts opts
          exit
        end

        opts.on_tail("--version", "Show version") do
          puts Sredder::VERSION
          exit
        end

      end.parse(args)

      options
    end

    private

      def defaults
        {
          :folder => 'Programming',
          :date   => Util.time_stamp
        }
      end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sredder-0.0.2 lib/sredder/arg_parser.rb
sredder-0.0.1 lib/sredder/arg_parser.rb