I am attempting to use OptionParser to read in multiple options and have them all act on a single filename. Ex: myprogram.rb -a -b file.txt How can I make 2 options share a mandatory argument while also allowing things like -h to run without parameters? Currently the above line just makes -a take -b as its parameter.

These commands should be able to run independently as well as concurrently similar to ls -a .., ls -l .. and ls -a -l .. However NEITHER command should work if there is no filename given. Ideally this solution should work with any n number of options.

1 Answer
1

alpha=false
beta=false
optionparser.on("-a", "Do this") do |a|
alpha = a
end
optionparser.on("-b", "Do that") do |b|
beta = b
end
if alpha
#do something with ARGV[0]
end
if beta
#do something with ARGV[0]
end

This code assumes that after the options are taken, the only thing left on the command line should be the filename. I feel like there is a more elegant way of doing this that takes into account other things potentially cluttering the command line and allows error correction for them.