Class: Minglr::OptionsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/minglr/options_parser.rb

Class Method Summary collapse

Class Method Details

.parse(args, *required_by_command) ⇒ Object



5
6
7
8
9
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
# File 'lib/minglr/options_parser.rb', line 5

def self.parse(args, *required_by_command)
  project_options = []

  if Resources::Base.site
    begin
      project_options = Resources::PropertyDefinition.project_options
    rescue ActiveResource::UnauthorizedAccess => exception
      puts "Connection #{exception.message} to #{Resources::Base.site.to_s}"
      puts "Did you set 'basic_authentication_enabled: true' in your auth_config.yml file?"
      exit 1 unless MINGLR_ENV == "test"
    end
  end
  
  command_options = {}
  
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: minglr [action] [options]"
    opts.separator ""
    opts.separator "Valid Commands Are: #{Minglr::Action.valid_actions.join(", ")}"
  
    opts.on("-n NAME", String, "Short name of card") do |card_name|
      command_options[:name] = card_name.strip
    end
  
    opts.on("-d DESCRIPTION", String, "Description of card") do |card_description|
      command_options[:description] = card_description.strip
    end
  
    opts.on("-t TYPE", String, "Type of card") do |card_type|
      command_options[:card_type_name] = card_type.strip
    end
  
    opts.on("-c COMMENT", String, "Comment") do |comment|
      command_options[:comment] = comment.strip
    end
  
    opts.on("-f FILE", String, "File to attach") do |file|
      command_options[:file_attachment] = file.strip
    end
  
    unless project_options.empty?
      opts.separator ""
      opts.separator "Project Specific Options"
    
      project_options.each do |option_pair|
        option_switch = option_pair[1].downcase.gsub(/\s|_/, "-").gsub(/--|---/, '-').gsub(/--/, '-')
        opts.on("--#{option_switch}", String, "Set the #{option_pair[1]} for a card") do |option|
          command_options[option_pair[0]] = option
        end
      end
    
      opts.separator ""
    end

    opts.on_tail("-h", "--help", "Show this help message.") do |help|
      puts opts
      exit 0 unless MINGLR_ENV == "test"
    end
  
    opts.on_tail("--version", "Show version") do
      version = YAML.load(File.read(File.join(File.dirname(__FILE__), "..", "..", "VERSION.yml")))
      puts "#{version[:major]}.#{version[:minor]}.#{version[:patch]}"
      exit 0 unless MINGLR_ENV == "test"
    end
  
    if args.empty?
      puts opts
      exit 0 unless MINGLR_ENV == "test"
    end
  end

  parser.parse! args
  command_options
end