argumentos en linea de comando

F

Foreero Ivan

Hola, me gustaria saber si existe alguna forma de generar mis propios
argumentos en una linea de comando . Algo asi como como se usa el ARGV:

$ ruby miscript.rb --miargumento1=1 --miargumento2=2 ....

para poder usar las variables "miargumentoX" dentro de mi script ruby

Gracias de antemano....


Sorry for my poor english....
Iwould like to know if there is any way to create my own command line
arguments. like ARGV

something like this:

$ ruby myscript.rb --myargument1=1 --myargument2=2 ...

and use the "myargumentX" innto my script ruby
 
J

Jesús Gabriel y Galán

2009/1/20 Foreero Ivan said:
Hola, me gustaria saber si existe alguna forma de generar mis propios
argumentos en una linea de comando . Algo asi como como se usa el ARGV:

$ ruby miscript.rb --miargumento1=3D1 --miargumento2=3D2 ....

para poder usar las variables "miargumentoX" dentro de mi script ruby

Gracias de antemano....

Hola. En Ruby existe un objeto llamado precisamente ARGV que es un
array que contiene los argumentos del programa:

jesus@jesus-laptop:~/temp/ruby$ ruby argv.rb --miargumento1=3D2 --miargumen=
to2=3D3
["--miargumento1=3D2", "--miargumento2=3D3"]
jesus@jesus-laptop:~/temp/ruby$ cat argv.rb
p ARGV

Para facilitar un procesamiento m=E1s complejo, echale un vistazo a
OptionParser, GetOptLong y gemas
como main o trollop, que te ayudan a procesar opciones pasadas como
argumentos. Por ejemplo,
este es un trozo de un script m=EDo usando la gema main

require 'main'

main {
description <<-DESC
Deletes or gzips jhub log files older than the specified dates,
searching the specified directories
recursively. The files should match this regexp
/^(\d\d\d\d-\d\d-\d\d).*\.log(\.gz)?$/
DESC

option("zip", "z") {
argument :required
description "Zip files older than the specified number of days"
cast :int
}
option("delete", "d") {
argument :required
defaults 7
description "Delete files older than the specified number of days.
If --zip option is specified, only delete the files that are in
between both dates"
cast :int
}
argument("directories") {
arity -2
}

def run
folders =3D params[:directories].values
have_to_zip =3D params[:zip].given?
zip =3D params[:zip].value
# Resto del programa
# ...
}


Jesus.
 
R

Rodrigo Bermejo

Foreero said:
Hola, me gustaria saber si existe alguna forma de generar mis propios
argumentos en una linea de comando . Algo asi como como se usa el ARGV:

$ ruby miscript.rb --miargumento1=1 --miargumento2=2 ....

para poder usar las variables "miargumentoX" dentro de mi script ruby

Gracias de antemano....


Sorry for my poor english....
Iwould like to know if there is any way to create my own command line
arguments. like ARGV

something like this:

$ ruby myscript.rb --myargument1=1 --myargument2=2 ...

and use the "myargumentX" innto my script ruby

http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html
Suerte !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top