Module: Sfdown::Options

Defined in:
lib/sfdown.rb

Constant Summary collapse

DEFAULTS =
{
  concurrent: 1,
  metadata: false,
  no: false,
  output: ".",
  timeout: 5,
  sleep: 0.0
}.freeze

Class Method Summary collapse

Class Method Details

.parse(argv) ⇒ Object

Parse argv into a Config, or print the banner and exit non-zero on misuse.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sfdown.rb', line 63

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.message}\n\n"
  warn parser
  exit 1
end

.parser(opts) ⇒ Object

Build the OptionParser and the options hash it fills.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sfdown.rb', line 36

def self.parser(opts)
  OptionParser.new do |o|
    o.banner = "Usage: sfdown project_name [-mn] " \
               "[-c concurrent] [-o output] [-t timeout] [-s sleep]"

    o.on("-c", "--concurrent N", Integer, "Number of parallel downloads (default 1) (unimplemented)") do |v|
      raise OptionParser::InvalidArgument, "#{v} (must be >= 1)" if v < 1

      opts[:concurrent] = v
    end
    o.on("-m", "--metadata", "Save metadata to disk at project's root (unimplemented)") { 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