Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

I am writing the script for "option" while getting arguments from command line, and below I have code,
use strict; use warnings; use Getopt::Std; my %options; if (scalar @ARGV < 3) {usage()}; sub usage { print STDERR << "EOF"; \nThis program does... usage: $0 [-hcfo] -h : this (help) message -c : config file -o : Fj xcf release file -x : output file EOF exit; } getopts ("hc:f:o:", \%options); usage() if $options{h}; my $source_file= $options{o}; my $dest_file= $options{x}; my $config_file= $options{c};
And in above code, I want to "-o" to be optional for example if user don't provide "-o(filename)" that means value for $source_file then I want to take default file suppose "source.txt"; Could any one guide me how can that be done?Please. Cheers

Replies are listed 'Best First'.
Re: question about getopt
by moritz (Cardinal) on Jun 24, 2009 at 07:08 UTC
    As far as I understand, all options are optional by default. You can check if they are provided by simply looking if $source_file etc. is undef, and if yes, you can supply your own default value - or die if they are mandatory.
      is the general standard way of doing "option" in script,
      use strict; use warnings; use Getopt::Std; my %options; if (scalar @ARGV < 3) {usage()}; sub usage { print STDERR << "EOF"; \nThis program does... usage: $0 [-hcfo] -h : this (help) message -c : config file -o : Fj xcf release file -x : output file EOF exit; } getopts ("hc:f:o:", \%options); usage() if $options{h}; if ($options{o}) { $source_file = $options{o}; } else { $source_file = "as.xcf-dist"; } if ($options{c}) { $config_file = $options{c}; } else { $config_file = 'config.ini'; } if ($options{x}) { $dest_file = $options{x}; } else { usage(); }
      if you have any suggestion, Please let me know, Cheers
        You can always write that more tidily:
        my $source_file = $options{o} || 'as.xcf-dist'; my $config_file = $options{c} || 'config.ini'; my $dest_file = $options{x} || usage();

        Another possibility is to use Getopt::Long, which IMHO has a nicer API.

        I have several suggestions.

        You should probably change 'f' to 'x' in your usage and in your call to getopts. It looks like you have a bug.

        You should probably get rid of the @ARGV check because I doubt it does what you think it does. If you pass these 2 options on your command line, '-c foo -x bar', scalar @ARGV will resolve to 4, not 2, as you might expect. Instead, consider checking the return status of the getopts.

        I make it a habit to check if an option is defined, just in case '0' is a legal value:

        if (defined $options{o})

        Consider using the Core module Pod::Usage instead of rolling your own usage handler. I second the recommendation to switch to Getopt::Long.

Re: question about getopt
by hifirock (Novice) on Jun 24, 2009 at 09:13 UTC
    Initialize the source_file with default value before invoking the getOptions function.
    my $source_file= "source.txt getopts ("hc:f:o:", \%options); usage() if $options{h}; $source_file= $options{o} if (defined($options{o});