Class: Sycersion::Options

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

Overview

Parses the commandline arguments provided by the user and provides them to the calling class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options

SEMVER_REGEX = /^(d+.d+.d+)-?(beta|staging|w*)?$/



14
15
16
17
# File 'lib/sycersion/options.rb', line 14

def initialize(argv)
  @options = {}
  parse(argv)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/sycersion/options.rb', line 10

def options
  @options
end

Instance Method Details



85
86
87
88
89
90
91
92
93
94
# File 'lib/sycersion/options.rb', line 85

def banner
  program_name = File.basename($PROGRAM_NAME)
  "    Usage: \#{program_name} [options]\n\n    \#{program_name} supports applications to manage semantic versioning. Details\n    can be found at https://semver.org\n\n  CL_BANNER\nend\n"

#parse(_argv) ⇒ Object



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
79
80
81
82
83
# File 'lib/sycersion/options.rb', line 19

def parse(_argv)
  parser = OptionParser.new do |opts|
    opts.banner = banner

    opts.on('--init',
            'Setup the sycersion environment:',
            '* file where the app version is stored',
            '* initial version will be stored in version file',
            '* code that provides the version to the application') do |opt|
      @options[:init] = opt
    end

    opts.on('-i', '--info [VERSION|SUMMARY]', i[version summary],
            'Print information about the application version and environment:',
            '* current version of the application (default)',
            '* file where the app version is stored',
            '* file where the version is read into the application') do |opt|
      @options[:info] = opt || :version
    end

    opts.on('--inc [MAJOR|MINOR|PATCH]', i[major minor patch],
            'Increment the provided verion element (major, minor, patch)') do |opt|
      @options[:inc] = opt
    end

    opts.on('--set MAJOR.MINOR.PATCH[+BUILD|-PRE_RELEASE+BUILD]',
            Sycersion::SEMVER_REGEX,
            'Set the application version according to semver:',
            '* major.minor.patch optionally add a pre-release and/or build',
            '* major.minor.patch-prerelease+build or',
            '* major.minor.patch+build') do |opt|
      @options[:set] = opt
    end

    opts.on('-p', '--pre PRE_RELEASE', Sycersion::SEMVER_PRE_RELEASE_REGEX,
            'Set a pre-release suffix') do |opt|
      @options[:pre_release] = opt
    end

    opts.on('-b', '--build BUILD', Sycersion::SEMVER_BUILD_REGEX,
            'Set a build suffix') do |opt|
      @options[:build] = opt
    end

    opts.on('-c', '--compare VERSION', Sycersion::SEMVER_REGEX,
            'Compare current version with VERSION regarding precedence') do |opt|
      @options[:compare] = opt
    end

    opts.on('-h', '--help', 'Show this help') do
      puts opts
      exit(0)
    end
  end

  begin
    puts "Run `#{File.basename($PROGRAM_NAME)} --help` for usage information" if ARGV.empty?
    parser.parse!
  rescue OptionParser::ParseError => e
    warn e.message, "\n", options
    parser.opts if ENV['SYC_DEBUG']
    parser.parse(%w[--help])
    exit(1)
  end
end