|
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
|