class ArgParser

Public Class Methods

parse(args) click to toggle source

Returns a structure describing the options.

# File lib/argparser.rb, line 33
def self.parse(args)
        # The options specified on the command line will be collected in <b>options</b>.
        # Set default values here, before the call to OptionParser.new.
        options = OpenStruct.new
                
        op = OptionParser.new do |opts|
                
                opts.banner = "\nUsage" << ":\t%s <options>" %($0) 
                opts.banner << "\n  or" << ":\t%s -s [source] -t [target] -b" %($0)

                opts.separator ""
                opts.separator "Specific options:"
                opts.on('-c' << "DIRECTORY", '--' << "conf DIRECTORY", "Manipulate files in the named directory (Default: #{$DEF_CONF_DIR})") do |dir|
                        options.conf_dir = dir
                end
                opts.on('-b', '--buttons', "Copy UserButton definitions") do 
                        options.section = :buttons
                end

                opts.on('-s' << " SOURCE-FILE", '--source' << " SOURCE-FILE", "The configuration-file which contains the original sections (or a short name)") do |source|
                        options.source = source
                        options.action = :copy
                end

                opts.on('-t' << " TARGET-FILE", '--target' << " TARGET-FILE", "The configuration-file to which the copied sections shall be appended (or a short name)") do |target|
                        options.target = target
                        options.action = :copy
                end

                opts.on('-l', '--list', "list the available configuration files") do 
                        options.action = :list_conf_files
                end

                opts.on('-n', '--names', "list the short names for the available programs") do 
                        options.action = :list_short_names
                end

                opts.on('-a', '--all', "list available config-files, names and program versions") do 
                        options.action = :list_all
                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
                        puts
                        exit true
                end

                opts.on_tail(("-v"), ("--version"), ("Show version and program information") ) do
                        UserIO::msg_version
                        exit true
                end
        end

        begin
                op.parse!(args)
        rescue OptionParser::MissingArgument => err
                UserIO::msg_option_parse_error(err)
                exit false
        rescue OptionParser::InvalidOption => err
                UserIO::msg_option_parse_error(err)
                exit false
        end
        options
end