Class: WGif::ArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/wgif/argument_parser.rb

Constant Summary collapse

DEFAULTS =
{
  trim_from: '00:00:00',
  duration: 1.0,
  dimensions: '480'
}

Instance Method Summary collapse

Constructor Details

#initializeArgumentParser

Returns a new instance of ArgumentParser.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
61
62
63
64
# File 'lib/wgif/argument_parser.rb', line 13

def initialize
  @options = {}
  @parser = OptionParser.new do |opts|
    opts.on('-f N',
            '--frames N',
            'Number of frames in the final gif. (Default 20)') {
      |n|  @options[:frames] = n.to_i
    }
    opts.on('-s HH:MM:SS.SSSS',
            '--start HH:MM:SS.SSSS',
            'Start creating gif from input video at this timestamp. (Default 00:00:00)') {
      |ts| @options[:trim_from] = ts
    }
    opts.on('-d seconds',
            '--duration seconds',
            'Number of seconds of input video to capture. (Default 1.0)') {
      |d|  @options[:duration] = d.to_f
    }
    opts.on('-w pixels',
            '--width pixels',
            'Width of the gif in pixels. (Default 480px)') {
      |gs| @options[:dimensions] = gs
    }
    opts.on('-u',
            '--upload',
            'Upload finished gif to Imgur') {
      |u| @options[:upload] = u
    }
    opts.on('-i',
            '--info',
            'Displays info about finished gif (currently just file size)') {
      |i| @options[:info] = i
    }
    opts.on('-p',
            '--preview',
            'Preview finished gif with Quick Look') {
      |p| @options[:preview] = p
    }
    opts.on_tail('-h',
                 '--help',
                 'Print help information.') {
      print_help
      exit
    }
    opts.on_tail('-v',
                 '--version',
                 'Print version.') {
      print_version
      exit
    }
  end
end

Instance Method Details

#argument_summaryObject



76
77
78
# File 'lib/wgif/argument_parser.rb', line 76

def argument_summary
  @parser.summarize
end

#parse(args) ⇒ Object



66
67
68
69
70
# File 'lib/wgif/argument_parser.rb', line 66

def parse(args)
  options = parse_args(args)
  validate(options)
  options
end

#parse_args(args) ⇒ Object



80
81
82
83
# File 'lib/wgif/argument_parser.rb', line 80

def parse_args(args)
  options = DEFAULTS.merge(parse_options args)
  options.merge(url: args[0], output: args[1])
end

#parse_options(args) ⇒ Object



85
86
87
88
# File 'lib/wgif/argument_parser.rb', line 85

def parse_options(args)
  @parser.parse! args
  @options
end


94
95
96
97
98
99
100
101
102
103
# File 'lib/wgif/argument_parser.rb', line 94

def print_help
  puts 'Usage: wgif [YouTube URL] [output file] [options]', "\n"
  puts argument_summary, "\n"
  puts <<-example
Example:

$ wgif https://www.youtube.com/watch?v=1A78yTvIY1k bjork.gif -s 00:03:30 -d 2.2 -w 400 --upload

  example
end


90
91
92
# File 'lib/wgif/argument_parser.rb', line 90

def print_version
  puts "wgif #{WGif::VERSION}"
end

#validate(args) ⇒ Object



72
73
74
# File 'lib/wgif/argument_parser.rb', line 72

def validate(args)
  WGif::Validator.new(args).validate
end