What silly mistake did I do with this program?
#!/usr/bin/env ruby
# This is opex.rb
require 'optparse'
ruser='yoshiko'
op=OptionParser.new
op.on('-u','--user USERNAME') { |v| ruser=v }
op.parse!(ARGV)
puts ruser
Invoking it with
opex.rb --user mika
opex.rb --user=mika
opex.rb -u mika
I would expect the output to be ‘mika’, but it is always ‘yoshiko’.
I found the solution (by trial and error):
I need to remove the argument to ‘parse!’, i.e. it should be
op.parse!
instead of
op.parse!(ARGV)
I’m still wondering why it didn’t work the original form. After all,
leaving out the argument just means that the default will be used, which
is ARGV, isn’t it?
Ronald F. wrote in post #1142336:
I’m still wondering why it didn’t work the original form. After all,
leaving out the argument just means that the default will be used, which
is ARGV, isn’t it?
Works for me:
$ ./o.rb -u foo
foo
$ ./o.rb
yoshiko
$ cat -n o.rb
1 #!/usr/bin/env ruby
2 # This is opex.rb
3 require ‘optparse’
4 ruser=‘yoshiko’
5 op=OptionParser.new
6 op.on(’-u’,’–user USERNAME’) { |v| ruser=v }
7 op.parse!(ARGV)
8 puts ruser
Robert K. wrote in post #1142351:
Works for me:
Weird. Works for me too now!!! Thanks for trying this out. Seems that
we have a non-issue here.
I think it’s the best to delete the whole thread. Do you think it is
appropriate to do so, or is thread deletion only used for, say,
offending threads?
Ronald
Ronald F. wrote in post #1142440:
I think it’s the best to delete the whole thread. Do you think it is
appropriate to do so, or is thread deletion only used for, say,
offending threads?
No idea. I’d would just leave it alone.