From: Tom Felker Date: 2003-04-07T06:35:52+09:00 Subject: Re: snippet: modifications to GetoptLong to allow processing of method arguments On Thu, 2003-04-03 at 21:16, Albert Chou wrote: > I don't know exactly how useful any of you out there will find the > following, but I thought it was interesting enough to share. The main > modification is to allow the 'get' and 'each' methods to take an > argument -- an array of strings akin to ARGV. If this argument is > present, it is processed by the Getoptlong object instead of ARGV. If > the argument is not present, ARGV is processed as in the original > version. I overrode the existing get method, copying it almost verbatim > except for basically substituting the passed-in argument for ARGV > everywhere. The following code can be put in your code to gain the > enhancement in behavior. > > Why did I do this? I had an existing program whose code was not > encapsulated in a class, and I needed to do so in order to enable > someone else to use this code. I didn't want to lose the ability to > flexibly process arbitrary arguments, so I decided to see how hard it > would be to modify Getoptlong. As it turns out, it was pretty easy. > Within 90 minutes I had worked out all the little kinks (e.g., is it > *args or args in my method definition?), resulting in a new version > which was 100% compatible with existing clients' uses of the code and > also cleanly usable as an object by new clients. > > > Al Wouldn't it make more sense to, since you've already s/ARGV/arguments/, add a function use_args(array) to set what arguments points to? You could make initialize set arguments = ARGV, and if user code wanted to parse a different array, it would cal use_args(differentArray). It might be more sane to only allow use_args before processing has started. This way, it still wouldn't change client code, but you wouldn't have to pass the array for each get. I think the library should be like that in the first place. -- Tom Felker It might look like I'm standing motionless, but I'm actively waiting for my problems to go away.