Search code examples
perlgetopt-long

Can Perl's Getopt::Long parse arguments I don't define ahead of time?


I know how to use Perl's Getopt::Long, but I'm not sure how I can configure it to accept any "--key=value" pair that hasn't been explicitly defined and stick it in a hash. In other words, I don't know ahead of time what options the user may want, so there's no way for me to define all of them, yet I want to be able to parse them all.

Suggestions? Thanks ahead of time.


Solution

  • The Getopt::Long documentation suggests a configuration option that might help:

    pass_through (default: disabled)
                 Options that are unknown, ambiguous or supplied
                 with an invalid option value are passed through
                 in @ARGV instead of being flagged as errors.
                 This makes it possible to write wrapper scripts
                 that process only part of the user supplied
                 command line arguments, and pass the remaining
                 options to some other program.
    

    Once the regular options are parsed, you could use code such as that provided by runrig to parse the ad hoc options.