Class: RedditImageDownloader::OptionParser
- Inherits:
-
Object
- Object
- RedditImageDownloader::OptionParser
- Defined in:
- lib/reddit_image_downloader/option_parser.rb
Constant Summary collapse
- PAGES =
%W[top new controversial]
- DEFAULT_OPTIONS =
{ subreddits: [], min_width: 0, min_height: 0, destination: Dir.pwd, max_age: nil, page: :top, subcommand: :download, }
- BANNER =
"Usage: reddit_image_downloader [options]"
- SUBREDDIT_FORMATS =
%W[-r --subreddits=SUB1,SUB2]
- SUBREDDIT_DESC =
"List of subreddits to download from"
- PATH_FORMATS =
%W[-d --destination=PATH]
- PATH_DESC =
"Path to download images to (defaults to current directory)"
- WIDTH_FORMATS =
%W[-x --min-width=WIDTH]
- WIDTH_DESC =
"Only download images at least WIDTH wide"
- HEIGHT_FORMATS =
%W[-y --min-height=HEIGHT]
- HEIGHT_DESC =
"Only download images at least HEIGHT tall"
- PAGE_FORMATS =
%W[-p --page=PAGE]
- PAGE_DESC =
"Page to download (top [default], new, controversial)"
- DAYS_FORMATS =
%W[-a --max-age=DAYS]
- DAYS_DESC =
"Purge files in destination folder older than DAYS days"
- VERSION_FORMATS =
%W[-v --version]
- VERSION_DESC =
"Print the version of reddit_image_downloader"
- HELP_FORMATS =
%W[-h --help]
- HELP_DESC =
"Show this message"
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #docs ⇒ Object
-
#initialize(args) ⇒ OptionParser
constructor
A new instance of OptionParser.
- #parse! ⇒ Object
- #parser ⇒ Object
Constructor Details
#initialize(args) ⇒ OptionParser
Returns a new instance of OptionParser.
47 48 49 50 |
# File 'lib/reddit_image_downloader/option_parser.rb', line 47 def initialize(args) @args = args @options = DEFAULT_OPTIONS.dup end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
41 42 43 |
# File 'lib/reddit_image_downloader/option_parser.rb', line 41 def args @args end |
#options ⇒ Object
Returns the value of attribute options.
41 42 43 |
# File 'lib/reddit_image_downloader/option_parser.rb', line 41 def @options end |
Class Method Details
.parse!(*args) ⇒ Object
43 44 45 |
# File 'lib/reddit_image_downloader/option_parser.rb', line 43 def self.parse!(*args) new(*args).tap(&:parse!) end |
Instance Method Details
#docs ⇒ Object
56 57 58 |
# File 'lib/reddit_image_downloader/option_parser.rb', line 56 def docs parser.to_s end |
#parse! ⇒ Object
52 53 54 |
# File 'lib/reddit_image_downloader/option_parser.rb', line 52 def parse! parser.parse!(args) end |
#parser ⇒ Object
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 |
# File 'lib/reddit_image_downloader/option_parser.rb', line 60 def parser @parser ||= ::OptionParser.new do |parser| parser. = BANNER parser.on(*SUBREDDIT_FORMATS, Array, SUBREDDIT_DESC) do |subreddits| [:subreddits] += subreddits end parser.on(*PATH_FORMATS, PATH_DESC) do |destination| [:destination] = destination end parser.on(*WIDTH_FORMATS, Integer, WIDTH_DESC) do |min_width| [:min_width] = min_width end parser.on(*HEIGHT_FORMATS, Integer, HEIGHT_DESC) do |min_height| [:min_height] = min_height end parser.on(*PAGE_FORMATS, PAGES, PAGE_DESC) do |page| [:page] = page end parser.on(*DAYS_FORMATS, Integer, DAYS_DESC) do |days| [:max_age] = days end parser.on_tail(*VERSION_FORMATS, VERSION_DESC) do [:subcommand] = :version end parser.on_tail(*HELP_FORMATS, HELP_DESC) do [:subcommand] = :help end end end |