Password generator using optparse
Author |
Message |
Null
![](http://img118.imageshack.us/img118/214/drzoidberg3in.gif)
|
Posted: Thu Jun 15, 2006 10:46 pm Post subject: Password generator using optparse |
|
|
I had fun writing it, and it might just come in handy, so I thought I'd share.
Examples of usage:
code: |
$ ruby pgen.rb
soovmuvytv
$ ruby pgen.rb --length=20
bynnugipyeeghdbihcdn
$ ruby pgen.rb --length=20 -n
v3ji3awj1trzjlda5oax
$ ruby pgen.rb --length=20 -np
:sa6vafpummpttjp?qo:
|
For a full list of options, do
code: |
$ ruby pgen.rb --help
|
code: |
#!/usr/bin/env ruby
# pgen.rb
# by Jesse H-K on Thursday, June 15 2006
require 'optparse'
class Array
def random_choice
self[rand(self.length)]
end
end
def parse_options(args)
options = {}
options[:length] = 10
opts = OptionParser.new do |opts|
opts.banner = "Usage: pgen.rb [options]"
opts.on("-n",
"--numbers",
"Include the digits [0-9] in the password.") do |n|
options[:numb] = n
end
opts.on("-p",
"--punctuation",
"Include common punctuation characters in the password.") do |p|
options[:punct] = p
end
opts.on("-u",
"--uppercase",
"Include uppercase letters in the password.") do |u|
options[:upcase] = u
end
opts.on("-l",
"--length [LENGTH]",
Integer,
"The length of the password. Default is 10 characters.") do |l|
options[:length] = l
end
end
opts.parse!
options
end
class PasswordGenerator
@@options = { :numb => ('0'..'9').to_a,
:punct => %w(. , ! ? @ & :),
:upcase => ('A'..'Z').to_a }
def initialize(length, *opts)
@possible = *('a'..'z')
@length = length
parse_content_options(opts)
end
def parse_content_options(opts)
opts.each { |opt| @possible.concat(@@options[opt]) if @@options.key? opt }
end
def generate
(1..@length).inject("") { |passwd, e| passwd << @possible.random_choice }
end
private :parse_content_options
end
options = parse_options(ARGV)
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Fri Jun 16, 2006 7:06 am Post subject: (No subject) |
|
|
Excellent.
code: | class Array
def random_choice
self[rand length]
end
end |
![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
Null
![](http://img118.imageshack.us/img118/214/drzoidberg3in.gif)
|
Posted: Fri Jun 16, 2006 10:14 am Post subject: (No subject) |
|
|
Still not as fun as writing
![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Fri Jun 16, 2006 10:19 am Post subject: (No subject) |
|
|
Indeed. |
|
|
|
|
![](images/spacer.gif) |
Null
![](http://img118.imageshack.us/img118/214/drzoidberg3in.gif)
|
Posted: Sat Jun 17, 2006 9:20 pm Post subject: (No subject) |
|
|
!!!
I just realized that the code I posted doesn't work. Somehow, the last line was not included!
here is the correct version:
code: |
#!/usr/bin/env ruby
# pgen.rb
# by Jesse H-K on Thursday, June 15 2006
require 'optparse'
class Array
def random_choice
self[rand(self.length)]
end
end
def parse_options(args)
options = {}
options[:length] = 10
opts = OptionParser.new do |opts|
opts.banner = "Usage: pgen.rb [options]"
opts.on("-n",
"--numbers",
"Include the digits [0-9] in the password.") do |n|
options[:numb] = n
end
opts.on("-p",
"--punctuation",
"Include common punctuation characters in the password.") do |p|
options[:punct] = p
end
opts.on("-u",
"--uppercase",
"Include uppercase letters in the password.") do |u|
options[:upcase] = u
end
opts.on("-l [LENGTH]",
"--length [LENGTH]",
Integer,
"The length of the password. Default is 10 characters.") do |l|
options[:length] = l
end
end
opts.parse!
options
end
class PasswordGenerator
@@options = { :numb => ('0'..'9').to_a,
:punct => %w(. , ! ? @ & :),
:upcase => ('A'..'Z').to_a }
def initialize(length, *opts)
@possible = *('a'..'z')
@length = length
parse_content_options(opts)
end
def parse_content_options(opts)
opts.each { |opt| @possible.concat(@@options[opt]) if @@options.key? opt }
end
def generate
(1..@length).inject("") { |passwd, e| passwd << @possible.random_choice }
end
private :parse_content_options
end
options = parse_options(ARGV)
puts PasswordGenerator.new(options[:length], *options.keys).generate
|
Sorry about that. ![Sad Sad](http://compsci.ca/v3/images/smiles/icon_sad.gif) |
|
|
|
|
![](images/spacer.gif) |
Cowzero
|
Posted: Mon Feb 18, 2019 2:33 am Post subject: RE:Password generator using optparse |
|
|
I don't understand this code. |
|
|
|
|
![](images/spacer.gif) |
|
|