Class: Mirah::Util::ArgumentProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/mirah/util/argument_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state, args) ⇒ ArgumentProcessor

Returns a new instance of ArgumentProcessor.



19
20
21
22
# File 'lib/mirah/util/argument_processor.rb', line 19

def initialize(state, args)
  @state = state
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



24
25
26
# File 'lib/mirah/util/argument_processor.rb', line 24

def args
  @args
end

#stateObject

Returns the value of attribute state.



24
25
26
# File 'lib/mirah/util/argument_processor.rb', line 24

def state
  @state
end

Instance Method Details



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mirah/util/argument_processor.rb', line 89

def print_help
  puts "#{$0} [flags] <files or -e SCRIPT>
  -c, --classpath PATH\tAdd PATH to the Java classpath for compilation
  --cd DIR\t\tSwitch to the specified DIR befor compilation
  -d, --dir DIR\t\tUse DIR as the base dir for compilation, packages
  -e CODE\t\tCompile or run the inline script following -e
  \t\t\t  (the class will be named \"DashE\")
  --explicit-packages\tRequire explicit 'package' lines in source
  -h, --help\t\tPrint this help message
  -I DIR\t\tAdd DIR to the Ruby load path before running
  -j, --java\t\tOutput .java source (compile mode [mirahc] only)
  --jvm VERSION\t\tEmit JVM bytecode targeting specified JVM
  \t\t\t  version (1.4, 1.5, 1.6, 1.7)
  -p, --plugin PLUGIN\trequire 'mirah/plugin/PLUGIN' before running
  -v, --version\t\tPrint the version of Mirah to the console
  -V, --verbose\t\tVerbose logging"
  state.help_printed = true
end


108
109
110
111
# File 'lib/mirah/util/argument_processor.rb', line 108

def print_version
  puts "Mirah v#{Mirah::VERSION}"
  state.version_printed = true
end

#processObject



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
# File 'lib/mirah/util/argument_processor.rb', line 26

def process
  state.args = args
  while args.length > 0 && args[0] =~ /^-/
    case args[0]
    when '--classpath', '-c'
      args.shift
      Mirah::Env.decode_paths(args.shift, $CLASSPATH)
    when '--cd'
      args.shift
      Dir.chdir(args.shift)
    when '--dest', '-d'
      args.shift
      state.destination = File.join(File.expand_path(args.shift), '')
    when '-e'
      break
    when '--explicit-packages'
      args.shift
      Mirah::AST::Script.explicit_packages = true
    when '--help', '-h'
      print_help
      throw :exit
    when '--java', '-j'
      if state.command == :compile
        require 'mirah/jvm/compiler/java_source'
        state.compiler_class = Mirah::JVM::Compiler::JavaSource
        args.shift
      else
        puts "-j/--java flag only applies to \"compile\" mode."
        print_help
        throw :exit
      end
    when '--jvm'
      args.shift
      state.set_jvm_version(args.shift)
    when '-I'
      args.shift
      $: << args.shift
    when '--plugin', '-p'
      args.shift
      plugin = args.shift
      require "mirah/plugin/#{plugin}"
    when '--verbose', '-V'
      Mirah::Typer.verbose = true
      Mirah::AST.verbose = true
      Mirah::JVM::Compiler::JVMBytecode.verbose = true
      state.verbose = true
      args.shift
    when '--version', '-v'
      args.shift
      print_version
    when '--no-save-extensions'
      args.shift
      state.save_extensions = false
    else
      puts "unrecognized flag: " + args[0]
      print_help
      throw :exit
    end
  end
  state.destination ||= File.join(File.expand_path('.'), '')
  state.compiler_class ||= Mirah::JVM::Compiler::JVMBytecode
end