Class: Settings
- Inherits:
-
Object
- Object
- Settings
- Defined in:
- lib/tabscroll/settings.rb
Overview
Global configurations of the program, along with a commandline argument parser.
Contains the program’s specific configuration rules.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns a specific setting previously set.
-
#initialize ⇒ Settings
constructor
Creates a configuration, with default values.
-
#parse(args) ⇒ Object
Sets options based on commandline arguments ‘args`.
Constructor Details
#initialize ⇒ Settings
Creates a configuration, with default values.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tabscroll/settings.rb', line 12 def initialize @settings = {} @settings[:filename] = nil # We have a way to test the program. # We're distributing a sample tab file with the Gem so # when users run `tabscroll --sample` they can see # how the program works. # This setting enables it @settings[:run_sample_file] = false # And this is the absolute filename of the file # distributed with the gem. # It'll be `../../sample_tab.txt` based on `settings.rb` path. filename = File.("../../", __FILE__) filename = File.dirname filename filename += "/sample_tab.txt" @settings[:sample_filename] = filename end |
Instance Method Details
#[](name) ⇒ Object
Returns a specific setting previously set.
81 82 83 |
# File 'lib/tabscroll/settings.rb', line 81 def [] name return @settings[name] end |
#parse(args) ⇒ Object
Sets options based on commandline arguments ‘args`. It should be `ARGV`.
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 |
# File 'lib/tabscroll/settings.rb', line 34 def parse args opts = OptionParser.new do |parser| parser. = "Usage: tabscroll [options] filename" # Make output beautiful parser.separator "" parser.separator "Options:" parser.on("--sample", "Run `tabscroll` with sample file") do @settings[:run_sample_file] = true end # These options appear if no other is given. parser.on_tail("-h", "--help", "Show this message") do puts parser exit end parser.on_tail("--version", "Show version") do puts "tabscroll v#{TabScroll::VERSION}" exit end end opts.parse! args # After parsing all dashed arguments we will check if # the user has provided a filename. # # The first argument without a leading '-' will be # considered. args.each do |arg| if arg =~ /^[^-]/ @settings[:filename] = arg break end end if not @settings[:filename] and not @settings[:run_sample_file] puts opts exit 666 end return @settings end |