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 4055818
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:44:56+00:00 2026-05-20T14:44:56+00:00

I am trying to use Getopt::Long add command line arguments to my script (seen

  • 0

I am trying to use Getopt::Long add command line arguments to my script (seen below). The problem I am running into is related to multiple commands that do different things. For example I have an option flag that sets the configuration file to use with the script the option is -c [config_path] and I also have -h for help.

The problem I am running into is I need to have a condition that states whether or not the config option has been used AND a config file has been specified. I tried counting the options in @ARGV but found if -h and -c are specifed it causes the script to move on the to the subroutine load_config anyway. Because as seen in the code below when 2 arguments are found in @ARGV it fires the subroutine.

In what way could I fix this? At least in my head specifying -h and -c at the same time sorta contradicts each other. Is there a way to make it so only “informational commands” like help cannot be executed with “operational commands” like -c? Heck is there a way where I get a list of the commands that have been passed? I tried printing the contents of @ARGV but nothing was in it even though I had specified command arguments.

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Term::ANSIColor;
use XML::Simple;
use Net::Ping;
use Net::OpenSSH;
use Data::Dumper;

# Create a new hash to copy XML::Simple configuration file data into
my %config_file;

# Clear the screen and diplay version information
system ("clear");
print "Solignis's Backup script v0.8 for ESX\\ESX(i) 4.0+\n";
print "Type -h or --help for options\n\n";

# Create a new XML::Simple object
my $xml_obj = XML::Simple->new();

# Create a new Net::Ping object
my $ping_obj = Net::Ping->new();

my $config_file;

my $argcnt = $#ARGV + 1;

GetOptions('h|help' => \&help, 
       'c|config=s' => \$config_file
      );

if ($argcnt == 0) {
    print "You must supply a config to be used\n";
} elsif ($argcnt == 2) {
    if (! -e $config_file) {
        print color 'red';
        print "Configuration file not found!\n";
        print color 'reset';
        print "\n";
        die "Script Halted\n";
    } else {
        load_config();
    }
}

sub load_config {

    print color 'green';
    print "$config_file loaded\n";
    print color 'reset';

    my $xml_file = $xml_obj->XMLin("$config_file",
                    SuppressEmpty => 1);

    foreach my $key (keys %$xml_file) {
            $config_file{$key} = $xml_file->{$key};
    }

    print Dumper (\%config_file);
}

sub help {
    print "Usage: backup.pl -c [config file]\n";
}
  • 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-05-20T14:44:57+00:00Added an answer on May 20, 2026 at 2:44 pm

    @ARGV is altered by GetOptions, that is why it seems empty. Rather than counting arguments, just directly check if $config_file is defined.

    BTW, IMO there is no need to try to exclude -c from being used with -h. Normally a “help” just prints the help text and exits without taking any other action, check that first and it shouldn’t matter whether -c is supplied or not.

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

Sidebar

Related Questions

I am trying to collect the values from command line using Getopt::Std in my
I'm trying to use jQuery to format code blocks, specifically to add a <pre>
I'm trying use the modulo operator (%) on long longs, and it seems to
Trying to use the path to the current script, and the path contains spaces
Im trying to use the NSLog, to print console messages. The problem is sometimes
Trying to use an excpetion class which could provide location reference for XML parsing,
Trying to use a guid as a resource id in a rest url but
While trying to use LINQ to SQL I encountered several problems. I have table
I' trying to use a Linq query to find and set the selected value
I trying to use ImageInfo and Jython to get information from a image on

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.