Is there a good (ideally CPAN) way to process arbitrary command line options with Perl?
E.g., take a string "-a 1 -b 'z -x' -c -d 3 4"
and generate a GetOpt::Long
- like data structure:
{ a=>1, b=>"z -x", c=>1, d=>[3,4] } # d=>"3 4" is acceptable
The caveat is that
the set of options is NOT known in advance; so we seemingly can't use
GetOpt::Long
as-is.The values themselves can contain other options, so simply parsing the string for
#\b+-(\S+)\b#
pattern to find all the options in THAT string also seems to not be possible, and further complicated that some parameters are of =s type, some =s@, some of "-x a b c" type.Moreover, even if we could do #2, does
GetOptionsFromString
support correct tokenizing that respects quoted values?
NOTE: Assume for the purpose of exercise that ALL arguments are "options", in other words, if you split up the string into (possibly-quoted) tokens, your structure is always
"-opt1 arg1a [arg1b] -opt2 ....".
In other words, any word/token that starts with a dash is a new option, and all the subsequent words/tokens that do NOT start with a dash are values for that option.