Class: ArgParser

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Logging
Defined in:
lib/argparser.rb

Constant Summary collapse

PREFIX =
'/usr/local'
SUPPLDIR =
"#{PREFIX}/share/#{$APPNAME}"
CODES =
%w[iso-2022-jp shift_jis euc-jp utf8 binary]
CODE_ALIASES =
{ "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
@@log =
self::init_logger()

Class Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Methods included from File_Checking

file_check, #file_check, last_mime_type, magic_check, mime_check

Class Method Details

.parse(args) ⇒ Object

Returns a structure describing the options.



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/argparser.rb', line 46

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.source = './'
  options.target = './'
  options.config = SUPPLDIR + File::Separator + 'html2slideshow.cfg'
  options.debug = false 
  options.debug_dir = nil
  options.source_type = 'HTML' # 'files' if not 'HTML'
  options.recursive = false
  
  # can be a space-separated list.
  options.formats = "jpg"

  op = OptionParser.new do |opts|
    opts.banner = trl("Usage") << ":\truby #{$APPNAME}.rb [options]\n\t" << trl("or") << " #{$APPNAME} [options]"

    opts.separator ""
    opts.separator trl("Specific options") << ':'

    opts.on(trl("-f"), '--' << trl("formats") << " [\"" << Image::FORMATS.keys.collect{|f| f.to_s}.join(', ') << "\"]", trl("specify the comma-separated list of image formats,"), trl("which should be used, others will be ignored.") ) do |iforms|
      
      options.formats = iforms.split(',').collect do |f| 
        f.strip!
        f.downcase!
        f.delete!('e') if f != 'webp'
        format = Image::FORMATS[f.to_sym]
        if !format
          @log.error('Not a supported image format: ' << f << ". Choose from " << Image::FORMATS.keys.collect{|f| f.to_s}.join(', ')) 
          @log.error("Aborting. Bye.")
          exit false
        end
        format 
      end
      @log.debug('storing formats in options : ' << options.to_s)
    end

    opts.on(trl("-d"), '--' << trl("debug [true|FALSE]"), trl("Switch on debug-mode."), trl("Will generate log-messages during processing") ) do |deb|
      options.debug = (deb && deb.chomp.downcase == 'false' ? nil : true)
      @log.level = Logger::DEBUG if options.debug
    end
    opts.on(trl("-l"), '--' << trl("local"), trl("Collect the image-files from the source-directory"), trl("(if not provided, HTML-files are analyzed)") ) do |l|
      options.source_type = 'files'
    end

    opts.on(trl("-r"),'--' << trl("recursive"), trl("When collecting image-files from the source-directory,"), trl("also include the sub-directories.")) do |l|
      options.recursive = true
    end

    opts.on(trl("-s"), '--' << trl("source SOURCE DIR"), trl("Read HTML source-files from this directory")) do |source|
      options.source = File.expand_path(source)
      options.target = options.source
    end
    opts.on(trl("-t"), '--' << trl("target [TARGET DIR]"), trl("Write slideshow-files to this sub-directory"), "  " << trl("(if not provided, SOURCE DIR is used)")) do |target|
      sd = File.expand_path(options.source)
      sd << File::Separator if (!sd.end_with?(File::Separator) )
      options.target = sd << target
    end

=begin
    opts.on("-c", "--config [CONFIG FILE]",
            "Read alternative configuration from this file") do |conf|
      $LOG.debug("config-file should be #{conf}") if $LOG
      options.config = File.expand_path(conf )
            end
    opts.on("-g", "--generic [CONFIG FILE]",
            "Write generic configuration options to this file") do |file|
      options.generic = File.expand_path(file )
            end
=end
    opts.separator ""
    opts.separator trl("Common options") << ':'

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    # show help
    opts.on_tail(trl("-h"), '--' << trl("help"), trl("Show this message")) do
      puts opts
      exit
    end

    # Another typical switch to print the version.
    opts.on_tail('--' << trl("version"), trl("Show version")) do
      msg = "\t#{$APPNAME}, " << trl("version") << " #{$VERSION}"
      msg << "\n\t#{$COPYRIGHT}"
      msg << "\n\n%50s" %($LICENSE)
      puts msg.box(:padding => 2, :left => 2, :top => 1)
      exit
    end
  end

  op.parse!(args)
  options
end