Also, I found out that I can use '--' option. But it really feels to me as a workaround and I don't want to make the users use it.

I don't think it's a workaround, using -- to indicate "stop processing options here" is a really common thing - perl, git, GNU getopt, and many other tools do it, which is why Getopt::Long supports it.

I also don't think messing with @ARGV is a bad thing to do, especially since you'd just be looking for a fixed string.

use warnings; use strict; use List::Util qw/first/; @ARGV = qw/create --folder empty --size 50 --additional hey how ---are --you 555/; my $i = first { $ARGV[$_] eq '--additional' } 0..$#ARGV; my (undef,@add) = defined $i ? splice @ARGV, $i, $#ARGV-$i+1 : (); use Data::Dump; dd \@ARGV, \@add; __END__ ( ["create", "--folder", "empty", "--size", 50], ["hey", "how", "---are", "--you", 555], )

Admittedly not the most beautiful code, but it works. If you want more elegance, I'd suggest either -- or hippo's suggestion...


In reply to Re: GetOpt unknown args by haukex
in thread GetOpt unknown args by ovedpo15

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.