Class: Kobot::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/kobot/option.rb

Overview

Responsible for parsing the command line options for custom execution.

Class Method Summary collapse

Class Method Details

.parse!Object

Parses command line options and returns a hash containing the options.



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kobot/option.rb', line 10

def parse!
  options = {}
  opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage: #{$PROGRAM_NAME} [options]"

    opt.on('-c', '--clock CLOCK', 'Required; the clock action option: in, out') do |clock|
      options[:clock] = clock
    end

    opt.on('-l', '--loglevel [LEVEL]', 'Specify log level: debug, info, warn, error; default is info') do |level|
      options[:loglevel] = level
    end

    opt.on('-s', '--skip [D1,D2,D3]', Array,
           'Specify dates to skip clock in/out with date format YYYY-MM-DD and',
           'multiple values separated by comma, such as: 2020-05-01,2020-12-31;',
           'weekends and public holidays in Japan will be skipped by default') do |skip|
      options[:skip] = skip
    end

    opt.on('-a', '--auto-skip', 'Enable auto-skipping by reading schedule from the Time Card page') do |auto_skip|
      options[:auto_skip] = auto_skip
    end

    opt.on('-w', '--auto-skip-without [SCHEDULE]',
           'Skip today if the provided text is not contained in the schedule',
           'column on the Time Card page of KOT; by default 裁量労働 is used;',
           'effective only if --auto-skip is passed to enable auto-skipping.') do |schedule|
      options[:auto_skip_without] = schedule
    end

    opt.on('-t', '--to [TO]',
           'Email address to send notification to; by default it is sent to',
           'the same self email account used in SMTP config as the sender') do |to|
      options[:to] = to
    end

    opt.on('-n', '--notify', 'Enable email notification') do |notify|
      options[:notify] = notify
    end

    opt.on('-d', '--dryrun', 'Run the process without actual clock in/out') do |dryrun|
      options[:dryrun] = dryrun
    end

    opt.on('-f', '--force',
           'Run the process forcibly regardless of weekends or public holidays;',
           'must be used together with --dryrun to prevent mistaken operations') do |force|
      options[:force] = force
    end

    opt.on('-x', '--headless', 'Start browser in headless mode') do |headless|
      options[:headless] = headless
    end

    opt.on('-g', '--geolocation', 'Allow browser to use geolocation') do |geolocation|
      options[:geolocation] = geolocation
    end

    opt.on_tail('-h', '--help', 'Show this help message') do
      puts opt
      exit 0
    end

    opt.on_tail('-v', '--version', 'Show current version') do
      puts VERSION
      exit 0
    end
  end
  opt_parser.parse! ARGV
  raise OptionParser::MissingArgument, 'The clock option is required' if options[:clock].nil?
  unless %w[in out].include? options[:clock]
    raise OptionParser::InvalidArgument, 'The clock option must be either: in, out'
  end
  if options[:force] && !options[:dryrun]
    raise OptionParser::InvalidArgument, 'Must specify --dryrun for forcibly running'
  end

  options
rescue OptionParser::MissingArgument, OptionParser::InvalidArgument => e
  puts e
  puts opt_parser.help
  exit 1
end