Click here to get back home

Tips on module selection for shell and/or commandline 'subcommand' behavior

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Tips on module selection for shell and/or commandline 'subcommand' behavior ken1 01-16-2007
Posted by ken1 on January 16, 2007, 5:19 am
Please log in for more thread options


Hi,

I will develop a commandline tool and am looking for tips on if there
are modules I should be using rather than (re)write my own.

Basically, the tool should behave like a lot of existing tools - the
actual command (accepting a number of generic flags) and a 'subcommand'
(also accepting flags, but specific for the subcommand).

Thus, very much similar to, say CVS ('cvs checkout') or perforce ('p4
client') etc. The programming model would be eased if the subcommands
followed some sort of plugin pattern. Also, I'd like to have a fair
influence on the treatment on flags given to the 'base' command.

Actually, I have a fairly extensive thing for this already - but if
there's a better module I could reuse, so much the better, I guess.
But, going further, something I don't have is that it would also be
nice if a module would support a shell like behavior (an example of
this combination is the 'cleartool' tool used in ClearCase).

Thus, assuming my tool is 'mytool', and I have written a subcommand
'mysubcommand' one way to invoke it should be:

mytool mysubcommand

But I should also be able to do this:

C:\tmp>mytool
mytool>mysubcommand
mytool>exit
C:\tmp>

It should work equally well on Windows or Linux/Unix though.

Any suggestions on in what corner on CPAN I should look?

TIA,

ken1


Posted by Peter J. Holzer on January 16, 2007, 12:57 pm
Please log in for more thread options


> Hi,
>
> I will develop a commandline tool and am looking for tips on if there
> are modules I should be using rather than (re)write my own.
>
> Basically, the tool should behave like a lot of existing tools - the
> actual command (accepting a number of generic flags) and a 'subcommand'
> (also accepting flags, but specific for the subcommand).
>
> Thus, very much similar to, say CVS ('cvs checkout') or perforce ('p4
> client') etc. The programming model would be eased if the subcommands
> followed some sort of plugin pattern. Also, I'd like to have a fair
> influence on the treatment on flags given to the 'base' command.

Getopt::Long is probably the most popular module for processing command
line options, and is quite flexible. For example you could use it like
this:

---8<------8<------8<------8<------8<------8<------8<------8<---
#!/usr/bin/perl
use warnings;
use strict;

use Getopt::Long qw(:config require_order);
use Data::Dumper;

sub usage {
print "Usage: ...\n";
exit(1);
}
my %main_command_options;
my $sub_command;
my %sub_command_options;
GetOptions(\%main_command_options,
         'h|help|?',
         'verbose',
         'd=s',
         'foo=i') or usage();

print Data::Dumper->Dump([\%main_command_options], ['main_command_options']);
if (@ARGV) {
$sub_command = shift;

GetOptions(\%sub_command_options,
         'h|help|?',
         'verbose',
         'f=s',
         'bar=f') or usage();

print "sub command = $sub_command\n";
print Data::Dumper->Dump([\%sub_command_options], ['sub_command_options']);
}
--->8------>8------>8------>8------>8------>8------>8------>8---

to get independent base and subcommand options:

% ./foo -v -d 2 bla -v --bar=3.5
$main_command_options = {
'verbose' => 1,
'd' => '2'
};
sub command = bla
$sub_command_options = {
'verbose' => 1,
'bar' => '3.5'
};


> Actually, I have a fairly extensive thing for this already - but if
> there's a better module I could reuse, so much the better, I guess.
> But, going further, something I don't have is that it would also be
> nice if a module would support a shell like behavior (an example of
> this combination is the 'cleartool' tool used in ClearCase).

I don't know if there is a module for that.

        hp

--
_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR | > ist?
| | | hjp@hjp.at | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ |        -- P. Einstein u. V. Gringmuth in desd

Posted by Jim Gibson on January 16, 2007, 3:58 pm
Please log in for more thread options



> Hi,
>
> I will develop a commandline tool and am looking for tips on if there
> are modules I should be using rather than (re)write my own.

I suggest Term::ReadLine modules for reading the subcommand line in
interactive mode (no options given on original command line). This
allows command editing and retrieval. I have not used any T::RL::x
modules, but I have used the Gnu C readline library in a C program
under unix. Hopefully, T::RL::x gives you the same functionality in
Perl in a portable fashion.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Posted by ken1 on January 18, 2007, 4:35 am
Please log in for more thread options


Hi,

Just wanted to thank for the suggestions so far. Any others,
particularly on higher-level 'shell' stuff?

ken1


Posted by Ron Savage on January 19, 2007, 7:36 pm
Please log in for more thread options


On Tue, 16 Jan 2007 21:19:23 +1100, ken1 wrote:

Hi Ken

> I will develop a commandline tool and am looking for tips on if
> there are modules I should be using rather than (re)write my own.

What about File::Tools?



Similar ThreadsPosted
ISO: deep voodoo debugging tools for tracking down seriously weird module behavior. November 9, 2007, 1:28 pm
words selection September 2, 2006, 10:43 am
CGI popup_menu does not change default selection March 24, 2005, 6:31 pm
How to to many things in a shell.. April 21, 2005, 2:18 am
CPAN shell says "Out of memory!" December 30, 2005, 5:15 pm
Shell::Jobs - request for comments February 12, 2005, 2:33 pm
Installing WWW::Mechanize from CPAN shell May 24, 2007, 4:56 pm
use lib -- different behavior August 13, 2004, 1:47 pm
My Bundle::Modules not installing with the CPANPLUS shell May 19, 2006, 2:55 pm
Required Perl Developer w/ UNIX. SQL and Shell Scripting. March 24, 2006, 6:41 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap