class ROTP::Arguments

Attributes

arguments[R]
filename[R]

Public Class Methods

new(filename, arguments) click to toggle source
# File lib/rotp/arguments.rb, line 6
def initialize(filename, arguments)
  @filename = filename
  @arguments = Array(arguments)
end

Public Instance Methods

options() click to toggle source
# File lib/rotp/arguments.rb, line 11
def options
  parse
  options!
end
to_s() click to toggle source
# File lib/rotp/arguments.rb, line 16
def to_s
  parser.help + "\n"
end

Private Instance Methods

bold(string) click to toggle source
# File lib/rotp/arguments.rb, line 78
def bold(string)
  "\033[1m#{string}\033[22m"
end
default_options() click to toggle source
# File lib/rotp/arguments.rb, line 28
def default_options
  OpenStruct.new time: true, counter: 0, mode: :time
end
green(string) click to toggle source
# File lib/rotp/arguments.rb, line 82
def green(string)
  "\033[32m#{string}\033[0m"
end
options!() click to toggle source
# File lib/rotp/arguments.rb, line 24
def options!
  @options ||= default_options
end
parse() click to toggle source
# File lib/rotp/arguments.rb, line 32
def parse
  return options!.mode = :help if arguments.empty?

  parser.parse arguments
rescue OptionParser::InvalidOption => exception
  options!.mode = :help
  options!.warnings = red(exception.message + '. Try --help for help.')
end
parser() click to toggle source
# File lib/rotp/arguments.rb, line 41
def parser
  OptionParser.new do |parser|
    parser.banner = ''
    parser.separator green('  Usage: ') + bold("#{filename} [options]")
    parser.separator ''
    parser.separator green '  Examples:   '
    parser.separator '    ' + bold("#{filename} --secret p4ssword") + '                       # Generates a time-based one-time password'
    parser.separator '    ' + bold("#{filename} --hmac --secret p4ssword --counter 42") + '   # Generates a counter-based one-time password'
    parser.separator ''
    parser.separator green '  Options:'

    parser.on('-s', '--secret [SECRET]', 'The shared secret') do |secret|
      options!.secret = secret
    end

    parser.on('-c', '--counter [COUNTER]', 'The counter for counter-based hmac OTP') do |counter|
      options!.counter = counter.to_i
    end

    parser.on('-t', '--time', 'Use time-based OTP according to RFC 6238 (default)') do
      options!.mode = :time
    end

    parser.on('-m', '--hmac', 'Use counter-based OTP according to RFC 4226') do
      options!.mode = :hmac
    end

    parser.on_tail('-h', '--help', 'Show this message') do
      options!.mode = :help
    end

    parser.on('-d', '--digest [ALGORITHM]', 'Use algorithm for the digest (default sha1)') do |digest|
      options!.digest = digest
    end
  end
end
red(string) click to toggle source
# File lib/rotp/arguments.rb, line 86
def red(string)
  "\033[31m#{string}\033[0m"
end