How to eliminate the need for option name for the command line arguments to be parsed in Perl

Posted on 2012-09-07
Last Modified: 2012-09-07
I use the following code to parse the options:

GetOptions ( 
				'f|file=s' => \$files_in_job
				or die "Error parsing arguments\n";

Open in new window

This code requires -f or -file before the argument.

How can I make it that does not require anything before the argument?

I tried something like the following but it does not work:

GetOptions ( 
				'' => \$files_in_job
				or die "Error parsing arguments\n";

Open in new window

Question by:Tolgar
    1 Comment
    LVL 26

    Accepted Solution

    If -f[ile] is the only options then simply remove GetOptions and use this:
    my $file = shift;
    die "invalid file: $file" unless (-f $file);

    Open in new window

    If it allows multiple files (haven't used GetOptions in a long time) then this is equivalent:
    my @files = @ARGV;
    foreach my $fil (@files) {
        die "invalid file: $fil" unless (-f $fil);

    Open in new window


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now