Class: JIRADiff::OptParse

Inherits:
Object
  • Object
show all
Defined in:
lib/jira_diff/options.rb

Class Method Summary collapse

Class Method Details

.default_optionsObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/jira_diff/options.rb', line 6

def self.default_options
  {
    debug:     false,
    directory: ".",
    dryrun:    false,
    master:    "master",
    source:    [],
    verbose:   true
  }
end

.parse(argv_opts = [], unit_testing = false) ⇒ Object



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
# File 'lib/jira_diff/options.rb', line 17

def self.parse(argv_opts = [], unit_testing = false)
  opt_parse = DevTools::OptParse.new({ name:     PROGRAM_NAME,
                                       version:  VERSION,
                                       testing:  unit_testing,
                                       defaults: default_options })

  parser = opt_parse.parser

  parser.banner = "Usage: #{DevTools::PROGRAM} [OPTIONS]"

  parser.separator ""
  parser.separator "[OPTIONS]"

  parser.separator ""
  parser.separator "Specific Options:"

  parser.on("-d", "--directory DIR", "Use DIR as our source directory") do |dir|
    dir = File.expand_path(dir.strip)
    if Dir.exist?(dir)
      Options.directory = dir
    else
      raise ArgumentError, "ENOEXIST: Directory does not exist -> #{dir}"
    end
  end

  parser.on("-m", "--master BRANCH", "Specify a master branch (default: master)") { |m| Options.master = m }
  parser.on("-s", "--source BRANCH",
            "Use BRANCH as the source to compare against (may be used more than once)") do |branch|
    Options.source << branch unless Options.source.include?(branch)
  end

  parser.separator ""
  parser.separator "Common Options:"

  parser.parse!(argv_opts)

  validate_options(Options)
end

.validate_options(opts) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/jira_diff/options.rb', line 56

def self.validate_options(opts)
  if opts.source.include?(opts.master)
    raise RuntimeError, "Source branches cannot include the master branch"
  end

  opts.source = ["develop"] if opts.source.empty?
  opts.master = "master" if opts.master.nil? || opts.master.strip == ""

  opts
end