class ArgParser

Public Class Methods

parse(args) click to toggle source

Returns a structure describing the options.

# File lib/argparser.rb, line 35
def self.parse(args)
        # The options specified on the command line will be collected in <b>options</b>.
        # We set default values here.
        options = OpenStruct.new
        options.fast = false 
        options.line = 0 
        options.file = nil

        op = OptionParser.new do |opts|
                opts.banner = "Usage:\t" << "%s -s [textfile] <options>" %($0) <<"\n"

                opts.separator ""
                opts.separator "Specific options:"

                opts.on('-' << "sFILE", '--' << "source FILE", "read this file") do |file|
                        options.file = file
                end

                opts.on('-' << "f", '--' << "fast", "display at once entire lines") do |fast|
                        options.fast = true
                end
                opts.on('-' << 'lLINE', '--' << "line LINE", "start reading file at line 'line'") do |line|
                        options.line = line.to_i - 1
                end

                opts.separator ""
                opts.separator ("Common options") << ':'

                # No argument, shows at tail.  This will print an options summary.
                opts.on_tail(("-h"), ("--help"), ("Show this message") ) do
                        puts opts
                        exit true
                end

                opts.on_tail(("-v"), ("--version"), ("Show version and program information") ) do
                        puts "\t#{APPNAME}, version #{VERSION} (called as #{$0})"
                        puts "\t© 2015-#{YEAR} #{AUTHOR} #{AMAIL}>"
                        exit true
                end
        end

        op.parse!(args)
        options
end