Tips on module selection for shell and/or commandline "subcommand" behavior
am 16.01.2007 11:19:23 von ken1
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
Re: Tips on module selection for shell and/or commandline "subcommand" behavior
am 16.01.2007 18:57:09 von hjp-usenet2
On 2007-01-16 10:19, ken1 wrote:
> 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
Re: Tips on module selection for shell and/or commandline "subcommand" behavior
am 16.01.2007 21:58:36 von Jim Gibson
In article <1168942763.334500.275950@v45g2000cwv.googlegroups.com>,
ken1 wrote:
> 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
Re: Tips on module selection for shell and/or commandline "subcommand" behavior
am 18.01.2007 10:35:50 von ken1
Hi,
Just wanted to thank for the suggestions so far. Any others,
particularly on higher-level 'shell' stuff?
ken1
Re: Tips on module selection for shell and/or commandline "subcommand" behavior
am 20.01.2007 01:36:34 von Ron Savage
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?