I want to make a script invoked with this syntax:
script.rb [common-options] arg1 [arg1-options] [arg2 [arg2-options] …]
For example:
script.rb --verbose --dry-run a1 --outfile c1 a2 --reprocess --outfile c2 a3 a4 --outfile b42
Returning something like this to my code:
options = {verbose: true, :'dry-run' => true}
args = {
'a1' => {outfile: 'c1'},
'a2' => {outfile: 'c2', reprocess: true},
'a3' => {},
'a4' => {outfile: 'b42'},
}
I can't figure out from the OptionParser documentation whether it's possible at all.
Does anyone know a solution for this?
Finally found solution in "Using ruby's OptionParser to parse sub-commands".
I need to use the poorly documented
ordermethod which will parse options only up to first positional argument.parsewill parse the whole ARGV:If I execute my script like this:
I will get:
And this help text will be generated: