Good day! To execute my script / program, the following command line argument construction is required:

ruby shell.rb <module_name> <options_for_module> 

An example of launching an application with the backup and rpmbuild modules:

 ruby shell.rb backup --dest=/mnt/backup --source=/var/www --exclude-list=list01 --gzip --ssh='user@foo.server.example.com' ruby shell.rb rpmbuild --builddir=/home/rpm --tool=mock 

Ie: The parameters following the module name depend on the module itself and relate only to it, the module I described something like this:

 module Backup ... class OptparseBase ... # тут описание параметров end # class end # module 

Parsil arguments with optparse.

The problem is that I am not a programmer and I cannot think of a way to read these external modules (Backup, Rpmbuild ...) depending on the first parameter. I would also like to make an internal separation in the module itself, for example, when starting ruby shell.rb backup::freebsd [opt] and ruby shell.rb backup::linux [opt] must be different variables or methods.

I hope that clearly explained the task, please tell me the direction.

 #Читать первый параметр, аперкейсить его и передавать в load #{name}.rb - это мне кажется слишком просто, думаю в ruby есть более красивое решение, за этим и обратился. # 

    1 answer 1

    Since all variations are made based on the first argument, you can manually check it:

     #!/usr/bin/env ruby require 'optparse' module_name = ARGV.shift options = {} if module_name == 'backup' OptionParser.new do |opts| opts.banner = "Usage: shell.rb backup::freebsd [options]" # opts.on ... end.parse! elsif module_name == 'rpmbuild' OptionParser.new do |opts| opts.banner = "Usage: shell.rb backup::linux [options]" # opts.on ... end.parse! else puts "Unknown module: #{module_name}" exit 1 end