My problem: Using getopt::Long , using options with both arguments and no arguments

example1: ./ldapadmin --adduser example2: ./ldapadmin --adduser Manuel,Castillo,F

(basically I want the program to know 'adduser flag is on with no arguments' or 'adduser flag called with arguments')

Both of these should work. Reading the Getopt::Long documentation, I noticed that you can set arguments as optional via : character instead of = (ex. 'adduser:s' => \@adduser) however, in example 1, the value of @adduser is false as if it has never been called!

my possible workaround: use $#ARGV and @ARGV before calling getoptions and keep track of flag this way, if $ARGV[0] contains --adduser and value of $#ARGV is 1, then it mustve been called with no arguments.

problem: Am i using getoptions incorrectly? Module documentation doesn't seem to be too informative, too short if you ask me.

here is a piece of my code---------------------------

my $argn = $#ARGV; # num of args stored in $argn my @argvee = @ARGV; # actual arguments stored in @argvee # above was done before GetOptions loads, since it seems to eat ARGV.. +. GetOptions ( 'adduser:s' => \@adduser, 'deluser=s' => \$deluser, 'addgroup=s' => \@addgroup, 'delgroup=s' => \$delgroup, 'addu2g=s' => \@addu2g, 'delu2g=s' => \@delu2g, 'chsh=s' => \@chsh, 'addalias=s' => \@addalias, 'delalias=s' => \@delalias, 'list' => \$list, 'help' => \$help ); @adduser = split(/,/,join(',',@adduser)); #take in info and split on c +ommas

thanks for your help


In reply to using Getopt::Long, arguments and no arguments possible? by dark314

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.