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

Hello monks,

I'm a beginner with perl so I was hoping one of you could help me with my problem. I've created a program that connects to an sftp server and sends a file to it or gets one from it. What I would like to do is create a command line interface where the user of the program defines what the username password and host name of the server is and which file should be copied to it or from it in the CLI. So running the program would look something like this: program.pl -host -user -pass -location of program to copy.

This is the code I have so far:
use Net::SSH2; $open=$ARGV[0]; if ($open eq "connect"){ my $ssh2 = Net::SSH2->new(); $ssh2->connect($host) or die; if ($ssh2->auth_password($user, $pass)) { print "Authorization successful\n"; $put=$ARGV[0]; $get=$ARGV[1]; # Send file if ($put eq "send") { $ssh2->scp_put("local file path","remote file path") or warn "Could not copy the file to the server"; } #Get file from server elsif ($get eq "get") { print "File Copied"; $ssh2->scp_get("remote file", "local file") or warn "Could not copy the file from the server"; } else { print "Error: Arguments entered must be either send or get\n"; exit; } } else { print "Authorization failure\n"; } } else { print "Error: Argument entered must be connect\n"; exit; }
EDIT:

Well dasgar was right all I needed to do was look through Getopt::Long and I found my answer. Thank you dasgar. For anybody else having problems just read about the Getopt::Long module its within the first couple of paragraphs that you find the answer.

Thank you dasgar!

Replies are listed 'Best First'.
Re: CLI with ARGV
by dasgar (Priest) on Aug 08, 2012 at 06:14 UTC

    Perhaps I missed it, but I didn't see a question in your post.

    I'm guessing that you're wanting help with command line arguments. If so, you should check out Getopt::Long, which is a core module for Perl.

      I'm sorry if I wasn't clear. My question was how I would go about creating a variable for instance $host that is defined by the user in the command line argument. So that the $host that was defined by the user would be used later in the program as a host in the ssh2 connection. "program.pl -host 10.1.1.1" and now this ip would be the $host variable used with the ssh2 connection in the code.
Re: CLI with ARGV
by influx (Beadle) on Aug 08, 2012 at 08:20 UTC

    If it's simple arguments you want, you can do this kind of thing

    my $host; my $pass; while(my $arg = shift @ARGV) { for ($arg) { if (/-h|--host/) { $host = shift @ARGV; } elsif (/-p|--pass/) { $pass = shift @ARGV; } } }

    This allows you to use -h hostname OR --host hostname, and -p pass OR --pass pass. Both arguments would be stored in $host and $pass respectively

      I actually used the Getopt::Long; module to set everything and it worked fine. But I have another problem now I've created three ARGV[] arguments.

      $list=$ARGV[0]; if ($list eq "list") { my $chan = $ssh2->channel(); $chan->blocking(1); $chan->shell(); print $chan "cd ..\n"; print $chan "ls\n"; print " $_" while <$chan>; print "Type the name of the directory you would like to enter: "; my $dir = <STDIN>; print $chan "cd '/'$dir\n"; print $chan "ls\n"; print " $_" while <$chan>; } $delete=$ARGV[1]; if ($delete eq "delete") { unlink($delete_file); } $upload=$ARGV[2]; #Send file if ($upload eq "upload") { $ssh2->scp_put($upload_local,$upload_remote) or warn "Could not copy the file"; } $download=$ARGV[3]; #Get file if ($download eq "download") { $ssh2->scp_get($download_remote, $download_local) or warn "Could not copy the file"; }

      Its made so that I can call a download or upload of some file or to list all the files in a folder.

      The problem I have is that I can't seem to figure out how to set the ARGV part so that I can run any of these arguments at any time. Right now for me to run download for instance I would first have to run list and delete and upload before I could run download.

      Is there some way to set the ARGV command to be able to run multiple commands without being dependant on the order by which they are run? So I could run list then download then upload then delete.