Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:30:35+00:00 2026-06-01T12:30:35+00:00

I have this getopt: GetOptions( GetOptions (library=s => \@libfiles); @libfiles = split(/,/,join(‘,’,@libfiles)); help =>

  • 0

I have this getopt:

GetOptions(  GetOptions ("library=s" => \@libfiles);
    @libfiles = split(/,/,join(',',@libfiles));
     "help" => \$help,
     "input=s" => \$fileordir,
     "pretty-xml:4" => \$pretty
);

Is it possible for Getopt::Long::GetOptions to detect if the same option is provided on the command line multiple times? For example, I would like the following to generate an error:

perl script.pl --input=something --input=something

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:30:36+00:00Added an answer on June 1, 2026 at 12:30 pm

    I don’t think there is direct way but you have two options:

    • Use an array and check after processing the options

      #!/usr/bin/perl
      
      use warnings;
      use strict;
      
      use Getopt::Long;
      
      my @options;
      my $result = GetOptions ('option=i' => \@options);
      
      if ( @options > 1 ) {
         die 'Error: --option can be specified only once';
      }
      
    • Use a subroutine and check if the option is already defined

      #!/usr/bin/perl
      
      use warnings;
      use strict;
      
      use Getopt::Long;
      
      my $option;
      my $result = GetOptions (
          'option=i' => sub {
              if ( defined $option) {
                  die 'Error: --option can be specified only once';
              } else {
                  $option = $_[1]; 
              }
          }
      );
      

      In this case you can use an exclamation mark ! at the beginning of the die and the error will be catched and reported as a usual Getopt error (see the documentation of Getopt::Long for the details)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this xml response to which I will do a foreach($response->entry->item as $data)
Is it posseble to have a --help argument with getopts? I'm currently using this
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have this code in jQuery, that I want to reimplement with the prototype
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM
I have this line in a javascript block in a page: res = foo('<%=
I have this setup where in my development copy I can commit changes on
I have this string 'john smith~123 Street~Apt 4~New York~NY~12345' Using JavaScript, what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.