Class: Pandata::ArgvParser

Inherits:
Object
  • Object
show all
Defined in:
lib/pandata/argv_parser.rb

Overview

Parses command-line input.

Class Method Summary collapse

Class Method Details

.parse(argv) ⇒ Hash

Parses an ARGV array for options.

Parameters:

  • argv (Array)

    an ARGV array

Returns:

  • (Hash)
    • :opts [OptionParser]

    • :user_id [String]

    • :output_file [String]

    • :data_to_get [Array]

    • :get_all_data [Boolean]

    • :help [Boolean]

    • :version [Boolean]

    • :return_as_json [Boolean]



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
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
97
98
99
100
101
102
103
# File 'lib/pandata/argv_parser.rb', line 21

def self.parse(argv)
  options = { data_to_get: [] }
  get_all_data = false

  options[:opts] = OptionParser.new do |opts|
    opts.banner = 'Pandata: A tool for downloading Pandora.com data'
    opts.define_head 'Usage: pandata <email|webname> [options]'
    opts.separator <<-END

Examples:
  pandata [email protected] --liked_tracks
  pandata my_webname --all -o my_pandora_data.txt
  pandata my_webname -lL --json

Options:
    END

    opts.on('--all', 'Get all data') do
      get_all_data = true
    end

    opts.on('-F', '--followers', "Get all user's followers") do
      options[:data_to_get] << :followers
    end

    opts.on('-f', '--following', 'Get all users being followed by user') do
      options[:data_to_get] << :following
    end

    opts.on('-j', '--json', 'Return the results as JSON') do
      options[:return_as_json] = true
    end

    opts.on('-L', '--liked_artists', 'Get all liked artists') do
      options[:data_to_get] << :liked_artists
    end

    opts.on('-l', '--liked_tracks', 'Get all liked tracks') do
      options[:data_to_get] << :liked_tracks
    end

    opts.on('-m', '--liked_albums', 'Get all liked albums') do
      options[:data_to_get] << :liked_albums
    end

    opts.on('-n', '--liked_stations', 'Get all liked stations') do
      options[:data_to_get] << :liked_stations
    end

    opts.on('-o', '--output_file PATH', 'File to output the data into') do |path|
      options[:output_file] = path
    end

    opts.on_tail("-h", "--help", "Show this message") do
      options[:help] = true
    end

    opts.on_tail("--version", "Show version") do
      options[:version] = true
    end
  end

  options[:opts].parse(argv)

  # User ID is the first argument.
  options[:user_id] = argv[0]

  if get_all_data
    options[:data_to_get] = [
      :liked_tracks,
      :liked_artists,
      :liked_albums,
      :liked_stations,
      :followers,
      :following
    ]
  else
    # Remove any duplicates caused by supplying flags multiple times.
    options[:data_to_get].uniq!
  end

  options
end