Class: FilterRename::OptParseMain

Inherits:
Object
  • Object
show all
Defined in:
lib/filter_rename/cli.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/filter_rename/cli.rb', line 10

def self.parse(args)
  options = OpenStruct.new
  options.filters = []
  options.files = []
  options.global = {}
  options.operation = :preview
  options.show_macro = ''

  opt_parser = OptionParser.new do |opt|
    opt.banner = 'Usage: filter_rename [-g OPTION1[,OPTION2...]] [FILTER1[,FILTER2...]] <file1>[ <file2>...] [OPERATION]'

    opt.separator ''
    opt.separator 'Operations:'

    opt.on('--apply', 'Apply the changes.') do |c|
      options.operation = :apply
    end

    opt.on('-d', '--dry-run', 'Don\'t apply any change but check for errors') do |c|
      options.operation = :dry_run
    end

    opt.on('-p', '--preview', 'Preview the filter chain applied [DEFAULT]') do |c|
      options.operation = :preview
    end

    opt.on('-t', '--targets', 'List of targets for each file') do |c|
      options.operation = :targets
    end

    opt.on('-g', '--globals', 'List of global variables') do |c|
      options.operation = :globals
    end

    opt.on('-c', '--configs', 'List of filter variables') do |c|
      options.operation = :configs
    end

    opt.on('-w', '--words', 'List of groups of words available for translation') do |c|
      options.operation = :words
    end

    opt.on('-m', '--macros', 'List of available macros') do |c|
      options.operation = :macros
    end

    opt.on('-s' , '--show-macro <MACRO>', 'List of commands used by MACRO') do |c|
      options.operation = :show_macro
      options.show_macro = c
    end

    opt.separator ''
    opt.separator 'Options:'

    opt.on('--global [OPTION:VALUE[,OPTION:VALUE]]', 'Override global options with: "option:value"') do |v|
      v.parametrize.each do |i|
        options.global.store(i.split(':')[0].to_sym, i.split(':')[1])
      end
    end

    opt.separator ''
    opt.separator 'Filters:'

    opt.on('--macro MACRO1,[MACRO2,...]', Array, 'Apply the MACRO' ) do |v|
      options.filters << MacroConfig.create(v) # { FilterRename::MacroConfig => v }
#           options.filters.merge MacroConfig.expand_macros(v)
    end

    Filters.constants.sort.each do |c|

      switch = c.to_s.to_switch
      klass = Object.const_get("FilterRename::Filters::#{c}")

      if klass.params.nil?
        opt.on("--#{switch}", klass.hint) do |v|
          options.filters << { klass => v }
        end
      elsif klass.params == Boolean
        opt.on("--[no-]#{switch}", klass.hint) do |v|
          options.filters << { klass => v }
        end
      else
        opt.on("--#{switch} #{klass.params}", "#{klass.hint}") do |v|
          options.filters << { klass => v.parametrize }
        end
      end
    end

    opt.separator ''
    opt.separator 'Other:'

    opt.on_tail('-h', '--help', 'Show this message') do
      puts opt
      exit
    end
    opt.on_tail('-v', '--version', 'Show version') do
      puts VERSION
      exit
    end
  end

  if (!STDIN.tty? && !STDIN.closed?) || !ARGV.empty?

    opt_parser.parse!(ARGV)

    (ARGV.empty? ? ARGF : ARGV).each do |f|
      f = File.expand_path(f.chomp)

      if File.exists?(f)
        options.files << f
      else
        raise FileNotFound, f
      end
    end if [:apply, :preview, :dry_run, :targets].include? options.operation
  else
    puts opt_parser; exit
  end

  options
end