Module: ArgParser

Defined in:
lib/canuby/argparser.rb

Overview

Parses command line arguments

Class Method Summary collapse

Class Method Details

.min(args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/canuby/argparser.rb', line 96

def self.min(args)
  options = OpenStruct.new
  options.config_version = Canuby::CONFIG_VERSION
  options.target = 'thirdparty'
  options.yml_file = 'canuby.yml'

  parser = OptionParser.new do |opts|
    opts.on('-c', '--config', 'Use a custom config file.') do
      Rake.application.tasks.each do |c|
        options.yml_file = c
      end
    end
  end
  options
end

.parse(args) ⇒ Object



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
# File 'lib/canuby/argparser.rb', line 26

def self.parse(args)
  options = OpenStruct.new
  options.config_version = Canuby::CONFIG_VERSION
  options.target = 'thirdparty'
  options.yml_file = 'canuby.yml'

  parser = OptionParser.new do |opts|
    opts.banner = "\n"
    opts.separator 'Usage: canuby [options]'
    opts.separator "\nBuild options:"

    opts.on('-t', '--target [Target]', 'Choose a target to build') do |t|
      options.target = if t.to_s.match?(/^thirdparty:/)
                         t
                       else
                         "thirdparty:#{t}"
                       end
    end

    opts.separator "\nOther @options:"
    opts.on('-c', '--config', 'Use a custom config file.') do
      Rake.application.tasks.each do |c|
        options.yml_file = c
      end
    end

    opts.on('-d', '--debug', 'Show debug log in console.') do
      ENV['DEBUG'] = 'true'
    end

    opts.on('-l', '--list', 'List all available targets') do
      require 'canuby/tasks'
      Rake.application.tasks.each do |t|
        # puts "#{t}".sub /:[a-z]{0,}$/, ''
        # puts "#{t}".match /^[A-z]{0,}:[A-z1-9]{0,}/
        puts t.to_s.yellow + ' ' * (45 - t.to_s.length) + t.comment.to_s unless t.comment.nil?
      end
      exit
    end

    opts.on('--list-all', 'List all available tasks') do
      Rake.application.tasks.each do |t|
        puts t
      end
      exit
    end

    opts.separator "\n"
    opts.on_tail('-h', '--help', 'Show this help message') do
      puts opts
      exit
    end

    opts.on_tail('-v', '--version', 'Show version') do
      puts Canuby::VERSION
      exit
    end
  end
  begin
    parser.parse!(args)
  rescue OptionParser::InvalidOption => e
    logger.error('Canuby does not understand that argument. We just assume it belongs to ruby.')
    logger.error(e)
    if File.basename($PROGRAM_NAME) == 'ruby'
      exit 1
    end
  end
  options
end