module DotOpts
Constants
- OPTIONS_FILES
Configuration file names.
@note Sorry, I could not decide between these two.
Public Class Methods
apply(argv, env={})
click to toggle source
Apply arguments and environment options.
@return [void]
# File lib/dotopts/api.rb, line 67 def self.apply(argv, env={}) env.each{ |k,v| ENV[k.to_s] = v.to_s } ARGV.concat(argv) #ARGV.replace(argv + ARGV) end
command()
click to toggle source
Get the current command.
@note This is take from basename of ‘$0`. In the future, we may
need to find a way to tweak this to somehow include parrent directories.
@todo Is ENV okay? Maybe [‘dotopts_command’] would be better?
# File lib/dotopts/command.rb, line 10 def self.command ENV['cmd'] || File.basename($0) end
configure!(io=nil)
click to toggle source
Configure
@param [String] file
The configuration file to load. (optional)
@return [void]
# File lib/dotopts/api.rb, line 15 def self.configure!(io=nil) text, file = read_config(io) if text cmds = Parser.parse(text) applicable = cmds.select do |c| c.current? end applicable.each do |c| argv = c.arguments env = c.environment debug(file, argv, env) apply(argv, env) end end end
debug(file, argv, env)
click to toggle source
Print message to stderr if dopts_debug flag it set.
@return [void]
# File lib/dotopts/api.rb, line 76 def self.debug(file, argv, env) return unless ENV['dotopts_debug'] $stderr.puts "dotopts file: #{file}" unless argv.empty? msg = argv.join(' ') $stderr.puts "dotopts argv: " + msg end unless env.empty? msg = env.map{ |k,v| "#{k}=#{v.inspect}" }.join(' ') $stderr.puts "dotopts env: " + msg end end
options_file()
click to toggle source
Returns the options file of the current project.
@return [String] The options file of the project.
# File lib/dotopts/api.rb, line 38 def self.options_file if project_root OPTIONS_FILES.each do |optfile| file = File.join(project_root, optfile) return file if File.exist?(file) end end end
project_root(start_dir=Dir.pwd)
click to toggle source
Find the root directory of the current project.
@return [String,nil] The root directory of the project.
# File lib/dotopts/api.rb, line 50 def self.project_root(start_dir=Dir.pwd) dir = start_dir home = File.expand_path('~') until dir == home || dir == '/' OPTIONS_FILES.each do |optfile| if File.exist?(File.join(dir, optfile)) return dir end end dir = File.dirname(dir) end nil end
read_config(io)
click to toggle source
Take an IO object and read it in. If it is a File object also return the file name. Strings are passed through untouched.
@return [Array<String>]
# File lib/dotopts/api.rb, line 97 def self.read_config(io) text, file = nil, '(io)' case io when String text = io when File text = io.read file = io.path when nil if file = options_file text = File.read(file) end else text = io.read end return text, file end