Sha256: fab56138a16081802f1c5573995f4a09c364bc146a7ab6284dbd2a92d30bdb6c

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module Ratch

  # = Runmode
  #
  # The Runmode class encapsulates common options for command line scripts.
  # The built-in modes are:
  #
  #   force
  #   trace
  #   debug
  #   dryrun -or- noharm
  #   silent -or- quiet
  #   verbose
  # 
  class Runmode

    def self.load_argv!
      options = {
        :force   => %w{--force}.any?{ |x| ARGV.delete(x) },
        :trace   => %w{--trace}.any?{ |x| ARGV.delete(x) },
        :debug   => %w{--debug}.any?{ |x| ARGV.delete(x) },
        :dryrun  => %w{--noharm --dryrun --dry-run}.any?{ |x| ARGV.delete(x) },
        :silent  => %w{--silent --quiet}.any?{ |x| ARGV.delete(x) },
        :verbose => %w{--verbose}.any?{ |x| ARGV.delete(x) }
      }
      new(options)
    end

    def initialize(options={})
      options.rekey(&:to_sym)

      @force   = options[:force]
      @trace   = options[:trace]
      @debug   = options[:debug]
      @noharm  = options[:noharm] || options[:dryrun]
      @silent  = options[:silent] || options[:quiet]
      @verbose = options[:verbose]
    end

    attr_accessor :force

    attr_accessor :trace

    attr_accessor :verbose

    attr_accessor :silent

    attr_accessor :debug

    attr_accessor :noharm

    alias_method :dryrun, :noharm
    alias_method :dryrun=,:noharm

    alias_method :quiet,  :silent
    alias_method :quiet=, :silent=

    def force?   ; @force   ; end
    def trace?   ; @trace   ; end
    def debug?   ; @debug   ; end
    def noharm?  ; @noharm  ; end
    def dryrun?  ; @noharm  ; end
    def quiet?   ; @silent  ; end
    def verbose? ; @verbose ; end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ratch-1.0.0 lib/ratch/runmode.rb