Class: QuickAndRuby::Time::EpochCli

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_and_ruby/time/epoch_cli.rb

Defined Under Namespace

Classes: Options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv: ARGV, env: ENV) ⇒ EpochCli

Returns a new instance of EpochCli.



26
27
28
29
# File 'lib/quick_and_ruby/time/epoch_cli.rb', line 26

def initialize(argv: ARGV, env: ENV)
  @argv = argv.clone
  @env = env.to_h.clone
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



24
25
26
# File 'lib/quick_and_ruby/time/epoch_cli.rb', line 24

def argv
  @argv
end

#envObject (readonly)

Returns the value of attribute env.



24
25
26
# File 'lib/quick_and_ruby/time/epoch_cli.rb', line 24

def env
  @env
end

Instance Method Details

#parse_options(options: Options.new) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/quick_and_ruby/time/epoch_cli.rb', line 36

def parse_options(options: Options.new)
  parser = OptionParser.new do |opts|
    opts.banner = 'Usage: epoch [options]'

    opts.on('-h', '--help', 'Prints this help') do
      puts parser
      exit
    end

    opts.on('-m', '--millis', 'Display epoch in milliseconds') do |_millis|
      options.millis = true
    end

    opts.on('-u', '--utc', 'Display time in UTC') do
      options.utc = true
    end
  end

  parser.parse!(argv)
  options
end

#runObject



31
32
33
34
# File 'lib/quick_and_ruby/time/epoch_cli.rb', line 31

def run
  options = parse_options
  puts EpochConverter.new(**options.to_h).convert_all(*argv)
end