Reputation: 2720
I want to handle a feature which seems to me almost natural with programs, and I don't know how to handle it with Getopt perl package (no matter Std ot Long).
I would like something like:
./perlscript <main option> [some options like -h or --output-file some_name]
Options will be handled with - or --, but I want to be able to let the user give me the main and needed option without dashes.
Is Getopt able to do that, or do I have to handle it by hand?
Upvotes: 2
Views: 881
Reputation: 42421
It sounds as though you are talking about non-options -- basic command-line arguments. They can be accessed with @ARGV
. The Getopt
modules will pass regular arguments through to your script unmolested:
use strict;
use warnings;
use Getopt::Long;
GetOptions (
'foo' => \my $foo,
'bar=s' => \my $bar,
);
my @main_args = @ARGV;
# For example: perl script.pl --foo --bar XXX 1 2 3
# Produces: foo=1 bar=XXX main_args=1 2 3
print "foo=$foo bar=$bar main_args=@main_args\n";
Upvotes: 8
Reputation: 14396
If you want to have it written without a -
, and it's also not optional (as you specifiy), then by any reasoning it isn't an option at all, but an argument. You should simply read yourself via
my $mainarg = shift
and then let Getopt do its thing. (You might want to check $#ARGV
afterwards to verify that the main argument was actually given.)
Upvotes: 3