Class: Chords::CommandLineParser

Inherits:
Object
  • Object
show all
Defined in:
lib/chords/command_line_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CommandLineParser

Returns a new instance of CommandLineParser.



7
8
9
10
11
12
13
14
# File 'lib/chords/command_line_parser.rb', line 7

def initialize(args)
  @args = args
  @frets = Chords::Fretboard::DEFAULT_FRETS
  @duplicates = 0
  @max_fret_distance = Chords::Fingering::DEFAULT_MAX_FRET_DISTANCE
  @tuning = Chords::Fretboard::TUNINGS[:standard]
  @formatter = Chords::TextFormatter
end

Instance Method Details

#parseObject



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chords/command_line_parser.rb', line 16

def parse
  @opts = OptionParser.new
  @opts.banner = "Usage: chords [options] CHORD"
  
  @opts.on("-l", "--list", 
          "List tunings and chords.") do
    puts tunings_and_chords
    exit
  end
    
  @opts.on("-f", "--frets NUMBER_OF_FRETS",
          Integer,
          "Number of frets, i.e. max position in fingerings.") do |frets|
    @frets = frets
  end
  
  @opts.on("-d", "--duplicates DUPLICATES",
          Integer,
          "Number of duplicated notes.") do |duplicates|
    @duplicates = duplicates
  end
  
  @opts.on("-m", "--max-fret-distance MAX_FRET_DISTANCE",
          Integer,
          "Maximum distance between positions in fingering.") do |mfd|
    @max_fret_distance = mfd
  end
  
  @opts.on("-t", "--tuning TUNING", 
          "Tuning to use. See -l for list of available tunings and chords.") do |t|
    begin
      @tuning = Fretboard.send(t.downcase).open_notes
    rescue Exception => e
      raise OptionParser::ParseError.new("Invalid tuning")
    end
  end
  
  @opts.on("--pdf", "Output to pdf. Requires Prawn.") do
    begin
      require 'chords/pdf_formatter'
    rescue LoadError => e
      puts "#{e.message}\n\nMake sure you have 'prawn' gem installed."
      exit(1)
    end
    @formatter = Chords::PDFFormatter
  end
  
  @opts.on("--html", "Output to html. Requires RMagick.") do
    begin
      require 'chords/html_formatter'
    rescue LoadError => e
      puts "#{e.message}\n\nMake sure you have 'rmagick' gem installed."
      exit(1)
    end
    @formatter = Chords::HTMLFormatter
  end
  
  @opts.on_tail("-h", "--help", examples) do
    puts @opts
  end
  
  begin
    print_chord @opts.parse!(@args)
  rescue OptionParser::ParseError => e
    puts e.message
  end
end