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.list = LIST
                options.html = TARGET
                options.delim = DELIM
                options.template = SOURCE
                options.log_level = Logger::INFO
                options.dir = nil

                op = OptionParser.new do |opts|
                        opts.banner = "\nUsage" << ":\t" << "%s <options> [directory]" %($0) 
1
                        opts.separator ""
                        opts.separator "Specific options:"

                        opts.on('-d' << "DIRECTORY", '--' << "dir DIRECTORY", "MANDATORY! Create an inventory of this directory") do |dir|
                                options.dir = dir
                        end

                        opts.on('-l' << "FILE", '--' << "list FILE", "Read or write the directory-listing from/to this file") do |file|
                                options.list = file
                        end

                        opts.on('-o' << "FILE", '--' << "output FILE", "Write the HTML inventory to this file") do |file|
                                options.html = file
                        end

                        opts.on('-t' << "FILE", '--' << "template FILE", "Use this HTML-file as template") do |file|
                                options.template = file
                        end

                        opts.on('-s' << "SYMBOL", '--' << "delim SYMBOL", "Expect this field delimiter in the template-file") do |delim|
                                options.delim = delim
                        end

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

                        # No argument, shows at tail.  This will print an options summary.
                        opts.on_tail(("-p"), ("--protocol"), ("Be verbose")) do
                                options.log_level = Logger::DEBUG
                        end
                        # No argument, shows at tail.  This will print an options summary.
                        opts.on_tail(("-h"), ("--help"), ("Show this message") ) do
                                puts opts
                                puts "\nDefaults (see constants.rb):\n\tTemplate-file:\t\t%s\n\tField-delimiter:\t%s\n\tHTML-output:\t\t%s\n\tList-file:\t\t%s" %[SOURCE, DELIM, TARGET, LIST]
                                puts "\nRecognized file-extensions (see constants.rb): %s" %EXTENSIONS.join(' ')
                                puts
                                exit true
                        end

                        opts.on_tail(("-v"), ("--version"), ("Show version and program information") ) do
                                puts "\t#{$0}"
                                puts "\tThis program is free software. Use, modify and distribute it\n\tunder the terms of the Gnu General Public License,\n\tversion 3 or later."
                                puts "\t© 2015 Michael Uplawski <michael.uplawski@souris-libre.fr>"
                                exit true
                        end
                end

                op.parse!(args)
                options
        end