module Notes::Options

Constants

ALL_FLAGS
DEFAULT_OPTIONS
EXCLUDE_FLAGS
FLAG_FLAGS

Public Instance Methods

arg_groups(args) click to toggle source

Parse ARGV into a directory and list of argument groups For example, given [‘app/’, -f’, ‘refactor’, ‘broken’, ‘–exclude’, ‘tmp’, ‘log’]:

> [ [‘app/’], [‘-f’, ‘refactor’, ‘broken’], [‘–exclude’, ‘tmp’, ‘log’] ]

# File lib/notes-cli/options.rb, line 30
def arg_groups(args)
  result = []
  buf    = []

  # No dir was passed, use default
  if args.empty? || args.first.start_with?('-')
    result << [ Notes.root ]
  end

  args.each do |arg|
    if ALL_FLAGS.include?(arg)
      result << buf unless buf.empty?
      buf = []
    end
    buf << arg
  end

  result << buf
end
default_excludes() click to toggle source
# File lib/notes-cli/options.rb, line 18
def default_excludes
  if Notes.rails?
    %w(tmp log vendor)
  else
    []
  end
end
defaults() click to toggle source

Return the default set of flags and locations

# File lib/notes-cli/options.rb, line 73
def defaults
  parse({})
end
parse(args) click to toggle source

Append received command line arguments to a default set of arguments Returns Hash

# File lib/notes-cli/options.rb, line 52
def parse(args)
  arg_list = arg_groups(args)
  options  = DEFAULT_OPTIONS.dup
  options[:exclude] += default_excludes
  options[:locations] = arg_list.shift

  arg_list.reject(&:empty?).each do |set|
    flag, *args = set
    args.map! { |arg| arg.delete("/") } # "log/" => "log"

    case flag
    when '-f', '--flags'   then options[:flags] += args
    when '-e', '--exclude' then options[:exclude] += args
    else puts "Unknown argument: #{flag}"
    end
  end

  options
end