Module: Notes::Options

Extended by:
Options
Included in:
Options
Defined in:
lib/notes-cli/options.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :flags     => %w(TODO FIXME OPTIMIZE),
  :exclude   => [],
  :files     => [],
  :directory => '',
}
FLAG_FLAGS =
['-f', '--flags']
EXCLUDE_FLAGS =
['-e', '--exclude']
ALL_FLAGS =
FLAG_FLAGS + EXCLUDE_FLAGS

Instance Method Summary collapse

Instance Method Details

#arg_groups(args) ⇒ Object

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’] ]



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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_excludesObject



18
19
20
21
22
23
24
# File 'lib/notes-cli/options.rb', line 18

def default_excludes
  if Notes.rails?
    %w(tmp log)
  else
    []
  end
end

#defaultsObject

Return the default set of flags and locations



73
74
75
# File 'lib/notes-cli/options.rb', line 73

def defaults
  parse({})
end

#parse(args) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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