Class: MyOptParser

Inherits:
Object
  • Object
show all
Defined in:
lib/org_lang_stats/my_opt_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMyOptParser

Returns a new instance of MyOptParser.



7
8
9
10
11
# File 'lib/org_lang_stats/my_opt_parser.rb', line 7

def initialize
    @options = { org: nil, debug: false, threads: 10, token: nil, percent: false }
    parse_options
    validate_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/org_lang_stats/my_opt_parser.rb', line 5

def options
  @options
end

Instance Method Details

#parse_optionsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/org_lang_stats/my_opt_parser.rb', line 13

def parse_options

    opt_parser = OptionParser.new do |opts|
        opts.banner = "\nExample usage: org_lang_stats --org workable --threads 10 --token yourtokenhere --debug --percent\n\nOptions:"

        opts.on('--org string', String, 'Organization name as appeared on github (required)') { |org| @options[:org] = org }
        opts.on('--token string', String, 'Github Personal Access Token') { |token| @options[:token] = token }
        opts.on('--threads [1..100]', Integer, 'Adjusts the concurrency (default: 10)') { |threads| @options[:threads] = threads }
        opts.on('--debug', 'If enabled, adds debug messages to the output (will not produce valid json)') { @options[:debug] = true }
        opts.on('--percent', 'If enabled, converts absolute values (bytes) to percent values') { @options[:percent] = true }
    end

    opt_parser.parse!
end

#validate_optionsObject



28
29
30
31
# File 'lib/org_lang_stats/my_opt_parser.rb', line 28

def validate_options
    abort '--org option missing' if @options[:org].nil?
    abort '--threads should be between 1 and 100' unless (1..100).cover?(@options[:threads])
end