Method: Inmake::Config.fromArguments

Defined in:
lib/inmake/config.rb

.fromArgumentsObject

Raises:



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
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/inmake/config.rb', line 43

def self.fromArguments
  require 'optparse'
  
  opts = {}
  opts[:files] = []
  opts[:variables] = {}

  # Parse special arguments

  OptionParser.new do |parser|
    parser.banner = INMAKE_BANNER

    parser.on('-f', '--file FILENAME', 'Specify a target file.') do |filename|
      opts[:files] << filename
    end

    parser.on('-p', '--prefix PREFIX', 'Find the build command by a prefix.') do |prefix|
      unless opts[:searchMode]
        opts[:searchMode] = :prefix
        opts[:searchArgument] = prefix
      else
        raise MultipleModeException, "Multiple search mode definitions!"
      end
    end

    parser.on('-s', '--suffix SUFFIX', 'Find the build command by a suffix.') do |suffix|
      unless opts[:searchMode]
        opts[:searchMode] = :suffix
        opts[:searchArgument] = suffix
      else
        raise MultipleModeException, "Multiple search mode definitions!"
      end
    end

    
    okDirModes = [:acceptDirs, :ignoreDirs]
    parser.on('-d', '--dir-mode MODE', "Define behavior when dealing with a directory. (default: acceptDirs) Accepted values are: #{okDirModes.join ', '}") do |dm|
      dm = dm.to_sym
      raise InvalidDirMode, "Not a valid directory mode: #{dm}" unless okDirModes.include? dm 
      opts[:dirMode] = dm
    end

    parser.on('-x', '--regex REGEX', 'Specify a regular expression to match the build command.') do |rx|
      unless opts[:searchMode]
        re = Regexp.compile rx
        opts[:searchMode] = :regex
        opts[:searchArgument] = re
      else
        raise MultipleModeException, "Multiple search mode definitions!"
      end
    end

    parser.on('--[no-]strip-matched', 'Strip all found matches on a regex search (default: false)') do |f|
      opts[:stripMatched] = f
    end

    parser.on('--[no-]ignore-nonmatched', 'Silently continue if a file does not seem to have a build command (default: true)') do |f|
      opts[:ignoreNonmatches] = f
    end

    parser.on('--no-vars', 'Disable all replacement variables, even the builtin ones (default: false)') do |v|
      opts[:variablesDisabled] = v
    end

    parser.on('-a', '--add-var KEY=VALUE', 'Add a replacement from {{KEY}} (always allcaps!) to VALUE, much like a C preprocessor definition') do |var|
      key, value = var.split '='
      raise InvalidVarDefinition, "Invalid variable definition syntax: `#{var}`" unless key and value

      opts[:variables][key.upcase] = value
    end

  end.parse!

  # any arguments that weren't flags are just treated as file names

  unless ARGV.empty?
    opts[:files].concat ARGV
  end

  # The default search mode is to find the build command on the second line of 

  # the source code.

  opts[:searchMode] ||= :secondLine 

  # The default file mode means that if a directory is encountered, we recurse

  # on all its files.

  opts[:dirMode] ||= :acceptDirs 

  # Variables are enabled by default.

  opts[:variablesDisabled] ||= false

  # By default, we do not strip the matched string.

  opts[:stripMatched] ||= false

  opts[:ignoreNonmatched] = true unless defined? opts[:ignoreNonmatched]
  
  # Can't run without ANY files...

  raise NoFilesGiven, "No input files specified! Use #{$0} --help for how to add input files." if opts[:files].empty?

  # return a new Config object

  return Config.new opts
end