Class: Senha::CLI::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/senha/cli/application.rb

Overview

The Application class is responsible for the

Instance Method Summary collapse

Constructor Details

#initializeApplication

Creates a new instance of the Application Class



36
37
# File 'lib/senha/cli/application.rb', line 36

def initialize
end

Instance Method Details

#parse_arguments(args) ⇒ Object

Parses the command line arguments



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/senha/cli/application.rb', line 40

def parse_arguments (args)
  begin
    options = {}

    options[:length] = Senha::DEFAULT_LENGTH
    options[:count] = 1

    opts = OptionParser.new do |opt|
      opt.banner = "#{APP_NAME} v#{VERSION}\nJacob Hammack\nhttp://www.hammackj.com\n\n"
      opt.banner << "Usage: #{APP_NAME} <options>"
      opt.separator('')
      opt.separator("Password Options")

      opt.on("-n", "--numbers", "Use digits [0-9] in the password generation") do |n|
        options[:numbers] = n
      end

      opt.on("-p", "--punctuation", "Use punctuation in the password generation") do |p|
        options[:punct] = p
      end

      opt.on("-s", "--symbols", "Use symbols in the password generation") do |s|
        options[:symbols] = s
      end

      opt.on("-l", "--lowercase", "Use lowercase [a-z] in the password generation") do |l|
        options[:lowercase] = l
      end

      opt.on("-u", "--uppercase", "Use uppercase [A-Z] in the password generation") do |u|
        options[:uppercase] = u
      end

      opt.on("-a", "--all-characters", "Use all available character sets for password generationA") do |a|
        options[:all] = a
      end

      opt.separator ''
      opt.separator 'Other Options'

      opt.on("--count COUNT", Integer, "Number of passwords to generate, default is 1") do |count|
        options[:count] = count#.to_i
      end

      opt.on("--length LENGTH", Integer, "Use uppercase in the password generation, default is 10") do |length|
        options[:length] = length#.to_i
      end

      opt.on_tail('-v', '--version', "Shows application version information") do
        puts "#{APP_NAME} - #{VERSION}"
        exit
      end

      opt.on_tail("-?", "--help", "Show this message") do
        puts opt.to_s + "\n"
        exit
      end
    end

    if args.length != 0
       opts.parse! args
     else
       puts opts.to_s + "\n"
        exit
    end

    options
  rescue OptionParser::MissingArgument => m
    puts opts.to_s + "\n"
    exit
  rescue OptionParser::InvalidOption => i
    puts opts.to_s + "\n"
    exit
  end
end

#run(args) ⇒ Object

Main body of the Application class



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/senha/cli/application.rb', line 117

def run(args)
  options = parse_arguments(args)

  if options != nil
    gen = Senha::Base::Generator.new(options)

    options[:count].times do
      puts gen.password(options[:length])
    end
  end
end