Module: Sfdown::Options
- Defined in:
- lib/sfdown.rb
Constant Summary collapse
- DEFAULTS =
{ concurrent: 4, link: false, metadata: false, no: false, input: nil, output: ".", timeout: 5, sleep: 0.0 }.freeze
Class Method Summary collapse
-
.parse(argv) ⇒ Object
Parse argv into a Config, or print the banner and exit non-zero on misuse.
-
.parser(opts) ⇒ Object
Build the OptionParser and the options hash it fills.
Class Method Details
.parse(argv) ⇒ Object
Parse argv into a Config, or print the banner and exit non-zero on misuse.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/sfdown.rb', line 70 def self.parse(argv) opts = DEFAULTS.dup parser = parser(opts) rest = parser.parse(argv) raise OptionParser::ParseError, "missing project_name" if rest.empty? raise OptionParser::ParseError, "unexpected arguments: #{rest[1..].join(' ')}" if rest.size > 1 Config.new(project: rest.first, **opts) rescue OptionParser::ParseError => e warn "Error: #{e.}\n\n" warn parser exit 1 end |
.parser(opts) ⇒ Object
Build the OptionParser and the options hash it fills.
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 |
# File 'lib/sfdown.rb', line 40 def self.parser(opts) OptionParser.new do |o| o. = "sfdown v#{Sfdown::VERSION} (#{Sfdown::DATE})\n" \ "Usage: sfdown project_name [-lmn] " \ "[-c concurrent] [-i input] [-o output] [-t timeout] [-s sleep]" o.on("-c", "--concurrent N", Integer, "Number of parallel network workers, both stages (default 4)") do |v| raise OptionParser::InvalidArgument, "#{v} (must be >= 1)" if v < 1 opts[:concurrent] = v end o.on("-i", "--input PATH", String, "Bootstrap download from a metadata JSON file (skips mapping)") { |v| opts[:input] = v } o.on("-l", "--link", "Hard-link duplicate files instead of copying them") { opts[:link] = true } o.on("-m", "--metadata", "Save metadata JSON file to disk at project's root") { opts[:metadata] = true } o.on("-n", "--no", "Only fetch directory tree structure and file metadata, not files") { opts[:no] = true } o.on("-o", "--output PATH", String, "Path to store project's root (default current dir)") { |v| opts[:output] = v } o.on("-t", "--timeout SECS", Integer, "Timeout for each GET request (default 5)") do |v| raise OptionParser::InvalidArgument, "#{v} (must be > 0)" if v <= 0 opts[:timeout] = v end o.on("-s", "--sleep SECS", Float, "Wait in-between requests (default 0)") do |v| raise OptionParser::InvalidArgument, "#{v} (must be >= 0)" if v.negative? opts[:sleep] = v end end end |