Class: Giblish::CmdLine

Inherits:
Object
  • Object
show all
Defined in:
lib/giblish/cmdline.rb

Overview

parse the cmd line

Defined Under Namespace

Classes: Options

Instance Method Summary collapse

Instance Method Details

#parse(args) ⇒ Object

converts the given cmd line args to an Options instance.

Raises MissingArgument or InvalidArgument if the cmd line arg validation fails.

Returns

the CmdLine::Options instance corresponding to the given cmd line args



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/giblish/cmdline.rb', line 235

def parse(args)
  @cmd_opts = Options.new
  @args = OptionParser.new do |parser|
    @cmd_opts.define_options(parser)
    parser.parse!(args)

    # take care of positional arguments
    raise OptionParser::MissingArgument, "Both srcdir and dstdir must be provided!" unless args.count == 2

    # we always work with absolute paths
    @cmd_opts.srcdir, @cmd_opts.dstdir = [args[0], args[1]].collect do |arg|
      d = Pathname.new(arg)
      d = Pathname.new(Dir.pwd) / d unless d.absolute?
      d.cleanpath
    end

    validate_options(@cmd_opts)
  end
  @cmd_opts
end