Sha256: 06f32da140a221e3a22dbfe6145659f05d55b76818d46b0c154b8a4f51594a03

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

require "optparse"

module SCSSBeautifier
  class Options

    attr_reader :options

    def initialize
      @options = {}
      @option_parser = OptionParser.new do |opts|
        opts.version = SCSSBeautifier::VERSION
        add_banner(opts)
        add_config_option(opts)
        add_in_place_option(opts)
        add_generate_config_option(opts)
      end
    end

    def parse(args)
      @option_parser.parse!(args)
      options[:path] = args.first if args.first
      add_defaults
      options
    end

    private

    def add_defaults
      if File.exists?(".scss-beautifier") && options[:config].nil?
        options[:config] = ".scss-beautifier"
      end
    end

    def add_banner(opts)
      opts.banner = unindent(<<-BANNER)
        Beautify your SCSS code
        Usage: #{opts.program_name} [options] [path]
      BANNER
    end

    def add_config_option(opts)
      message = "the configuration file"
      opts.on("-c", "--config config", message, String) do |config|
        self.options[:config] = config
      end
    end

    def add_in_place_option(opts)
      message = "whether to overwrite the file or not"
      opts.on("-i", "--in-place", message) do |bool|
        self.options[:in_place] = bool
      end
    end

    def add_generate_config_option(opts)
      message = "generate a .scss-beautifier config with defaults"
      opts.on("-g", "--gen-config", message) do |bool|
        self.options[:generate_config] = bool
      end
    end

    def unindent(str)
      str.gsub(/^#{str.scan(/^[ ]+(?=\S)/).min}/, "")
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
scss_beautifier-0.1.19 lib/scss_beautifier/options.rb
scss_beautifier-0.1.18 lib/scss_beautifier/options.rb