Class: Changelog::OptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/changelog/option_parser.rb

Overview

Class responsible for parsing options and detecting arguments passed for Changelogko.

Defined Under Namespace

Classes: Options

Class Method Summary collapse

Class Method Details

.help_option(opts) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/changelog/option_parser.rb', line 59

def self.help_option(opts)
  opts.on('-h', '--help', 'Show help.') do
    $stdout.puts opts
    $stdout.puts 'Adding a .changelogko file in the root of the working directory will allow to automatically ' \
                 'append the contents of the file as options to every `changelogko` (or `cko`) command. ' \
                 'For example, assume the .changelogko file contains "--no-archive". ' \
                 'For every changelogko command, it will automatically append --no-archive, ' \
                 'as such: bundle exec changelogko -r --no-archive.'
    raise ProcessEnded
  end
end

.no_archive_option(opts, options) ⇒ Object



53
54
55
56
57
# File 'lib/changelog/option_parser.rb', line 53

def self.no_archive_option(opts, options)
  opts.on('-A', '--no-archive', 'Do not archive the release after creating it.') do
    options.no_archive = true
  end
end

.option_parser(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/changelog/option_parser.rb', line 19

def self.option_parser(options)
  ::OptionParser.new do |opts|
    opts.banner = "Usage: (changelogko|cko) [options]\nOptions:\n"

    release_option(opts, options)
    type_option(opts, options)
    types_option(opts)
    no_archive_option(opts, options)
    help_option(opts)
  end
end

.parse(argv) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/changelog/option_parser.rb', line 7

def self.parse(argv)
  options = Options.new

  parser = option_parser(options)

  argv = ['-h'] if argv.empty?
  parser.parse!(argv)

  options.title = argv.join(' ').strip.squeeze(' ').tr("\r\n", '') unless options.release
  options
end

.release_option(opts, options) ⇒ Object



31
32
33
34
35
# File 'lib/changelog/option_parser.rb', line 31

def self.release_option(opts, options)
  opts.on('-r', '--release', 'Create release from unreleased directory.') do
    options.release = true
  end
end

.type_option(opts, options) ⇒ Object



37
38
39
40
41
42
# File 'lib/changelog/option_parser.rb', line 37

def self.type_option(opts, options)
  type_desc = 'Type of changelog. Use -T, --types to display all available types.'
  opts.on('-t', '--type [title]', String, type_desc) do |value|
    options.type = value
  end
end

.types_option(opts) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/changelog/option_parser.rb', line 44

def self.types_option(opts)
  opts.on('-T', '--types', 'List available types.') do
    TYPES.each do |type|
      $stdout.puts "#{type.name}#{type.description}"
    end
    raise ProcessEnded
  end
end