GetoptLong, but not on ARGV

T

Trans

Is there a way to utilize Getoptlong on an arbitrary command var, ie.
not on ARGV. I figure I can alway do something like:

require 'getoptlong'

my_argv = Shellwords.shellwords( "foo -x" )

const_set:)ARGV, my_argv)

opts = GetoptLong.new(
[ '-x', GetoptLong::NO_ARGUMENT ]
)

But it just doesn't seem proper to reset ARGV.

Thanks,
T.
 
T

Trans

Is there a way to utilize Getoptlong on an arbitrary command var, ie.
not on ARGV. I figure I can alway do something like:
require 'getoptlong'
my_argv = Shellwords.shellwords( "foo -x" )
const_set:)ARGV, my_argv)
opts = GetoptLong.new(
[ '-x', GetoptLong::NO_ARGUMENT ]
)
But it just doesn't seem proper to reset ARGV.
Thanks,
T.

Basically did
%s/ARGV/(@arglist||ARGV)/g
and inserted the attribute accessor, funny thing seems to work ;)

opts = GetoptLong.new(
[ '-x', GetoptLong::NO_ARGUMENT ]
)
opts.arglist = Shellwords.shellwords( "foo -x" )

Ah, so we need a patch. Okay well I guess I'll post that up on the
board too.

Thanks Robert,
T.
 
N

Nobuyoshi Nakada

Hi,

At Thu, 21 Jun 2007 17:04:15 +0900,
Robert Dober wrote in [ruby-talk:256352]:
It does *not* temper with internal state, just allows an array to be
passed to #each or #get. I tested it with this use case:

You forget about #terminate.
 
T

Trans

Ah, so we need a patch. Okay well I guess I'll post that up on the
board too.

Yes definitely ARGV is hardcoded, I was however fooling around, trying
to make a patch in less than a minute, which worked ;).

If you really want to send up a patch, please consider this one (it is
a correct patch created with diff -u this time too):

It does *not* temper with internal state, just allows an array to be
passed to #each or #get. I tested it with this use case:

------------------ 8< -------------------------
require 'getoptlong'

opt = GetoptLong.new([ '-x', GetoptLong::NO_ARGUMENT ],[ '-b',
GetoptLong::REQUIRED_ARGUMENT])

blk = lambda do
|*v|
puts v.join(": ")
end
opt.dup.each %w{ -x -b 1024}, &blk

opt.dup.each %w{ -b 4096 }, &blk
------------------------------ 8<---------------------------

Please note the necessity of dup for reuse, I did not want to bother
with internal state of a class I do not understand and I have no time
to dig into :(.

Cheers
Robert

I think i would be better to do:

def argslist
@argslist ||= ARGV # or ARGV.dup
end

def argslist=(array)
@argslist = array.to_ary
end

and then /argslist/ARGV/g

T.
 
R

Robert Dober

I think i would be better to do:

def argslist
@argslist ||= ARGV # or ARGV.dup
end

def argslist=(array)
@argslist = array.to_ary
end

and then /argslist/ARGV/g

Hmm more in the spirit of the original class probably.
Anyway if Nobu really wants this little patch I can adapt the patch to
your approach if he prefers.

Robert
 
N

Nobuyoshi Nakada

Hi,

At Thu, 21 Jun 2007 19:45:08 +0900,
Robert Dober wrote in [ruby-talk:256371]:
Thanks nobu, does this fix it?

Almost, but #terminate can be called outside #get. I think
getoptlong isn't designed for such usage.
@non_option_arguments.reverse_each do |argument|
- ARGV.unshift(argument)
+ args.unshift(argument)
end

In short, this can be:

args.unshift(*@non_option_arguments)
 
R

Robert Dober

Hi,

At Thu, 21 Jun 2007 19:45:08 +0900,
Robert Dober wrote in [ruby-talk:256371]:
Thanks nobu, does this fix it?

Almost, but #terminate can be called outside #get. I think
getoptlong isn't designed for such usage.
@non_option_arguments.reverse_each do |argument|
- ARGV.unshift(argument)
+ args.unshift(argument)
end

In short, this can be:

args.unshift(*@non_option_arguments)
I did not want to lecture ;)
Hmm I am a little bit confused now because I have provided #terminate
with a defaulted parameter for exactly the reason above.
Anyway I think Tom's idea to use an instance variable @args (defaulting to ARGV)
with an attribute accessor reflects the spirit of the original design
much better.
I will try to find half an hour to do that for fun this weekend and I
will send it, feel free to use it or to loose it ;).

Cheers
Robert
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top