Re: Getopt::Long
by ibanix (Hermit) on Dec 16, 2002 at 08:07 UTC
|
I don't quite understand, is your shell also written in perl? And it executes other perl scripts inside of that?
If that's the case, you can tell your perl script that v = view, a = add, etc, etc. (Actual details of code left up to you).
I could be wrong, but this doesn't sound like a Getopt::Long problem at all, since you actually want to run a different command based on character entry, not do different things with the same program based upon the parameters passed to it.
If the shell is smart enough, (or your OS), why not just use aliases?
HTH,
ibanix
$ echo '$0 & $0 &' > foo; chmod a+x foo; foo; | [reply] [d/l] [select] |
|
|
first, my shell is also written in perl, and it's executing functions depends on the command the user enters, the function behaves according to the options to the command.
Like Getopt::Long doesn't build aliases for the options or telling the script that p = percentage, a = active ... I don't won't to do that either. I just wondered whether I can combine somehow the module to help me with that, since it does what I want with the options (never said that it's a problem with the module).
UPDATE:
this is an example to what I want:
# suppose I have the above commands (view, add, edit, set ,show)
# since no command except 'add' starts with the letter 'a', I want tha
+t both will work
myshell> add -name test -value 40 # first
myshell> a -name test -value 40 # second
# in the GetOptions() function, I only have for the 'add' command, the
+ followings:
if ($command eq 'add') {
if (GetOptions(\%hash, 'name=s', 'value=i')) {
&doSomething();
}
} elsif ($command eq 'edit') {
...
} ...
Currently I identify that 'add' was called and the first if is processed with it's options. $command is NOT an option.
I hope that now it's clearer.
Hotshot | [reply] [d/l] |
|
|
But how are you looking for input in your interactive shell? Expect? (Which would be the logical choice).
IMHO, Getopt::Long is not made for what you're trying to do. Make aliases for whatever your shell is expecting. Use a hash as a lookup table or something.
# Not tested, or even sure if it makes sense.
%commands = (
'add' => '&add',
'view' => '&view',
'delete' => '&delete',
'a' => '\$commands{add}',
'v' => '\$commands{view}',
'd' => '\$commands{delete}',
);
Cheers,
ibanix
$ echo '$0 & $0 &' > foo; chmod a+x foo; foo; | [reply] [d/l] [select] |
|
|
Well then, the "cheap" way to do it is:
if ($command eq 'add' || $command eq 'a') {
if (GetOptions(\%hash, 'name=s', 'value=i')) {
&doSomething();
}
} elsif ($command eq 'edit' || $command eq 'e') {
...
}
Of course, I think a better way to do it would be something like the following. (Note: My understanding of perl's refrences lacks something and this should be taken with a grain of salt).
# not tested
%commands = (
'add' => '&add',
'a' => '\$command{add}',
'view' => '&view',
'v' => '\$command{view}',
);
while (<STDIN>) { # or whatever
$command{$_}; # or is it '\$command{$_}' ?
...
}
sub add {
GetOptions(\%hash, 'name=s', 'value=i');
# your code here
}
sub view {
GetOptions( ... );
# your code here
}
Cheers,
ibanix
$ echo '$0 & $0 &' > foo; chmod a+x foo; foo; | [reply] [d/l] [select] |
|
|
|
|
Re: Getopt::Long
by tachyon (Chancellor) on Dec 16, 2002 at 09:16 UTC
|
At the risk of being branded a heretic I have never been much of a fan of the Getopt modules. Why not just parse @ARGV yourself to generate exactly the behaviour you want?
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] |
|
|
Um... no, sorry. It's just not worth the effort.
I'll agree with the fact that in some respect the Getopt modules leave a bit to be desired.
I'm in two minds about it. On one hand, every time I need to use them, I need to consult the documentation. I don't know whether this is because they are not intuitive enough to use, or whether I don't use them enough to memorise all the minor details. This is especially true of Getopt::Long. Things like switch bundling don't work "out of the box." You have to flick a switch (so to speak) in order to get what I perceive to be the natural behaviour.
On the other hand, they do what they are designed to do. They deal with a chore that I can't be (or don't want to be) bothered with. I'm perfectly willing to bend a bit to the way they do things, rather than spend too much time getting things just right, if that frees up my time to concentrate on the core of the program. I don't think anyone should be parsing the command line by themselves these days... it's just too costly in programmer time for such a minor return on investment.
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
| [reply] |
Re: Getopt::Long
by PodMaster (Abbot) on Dec 16, 2002 at 10:45 UTC
|
I think this is what you're hinting at ( i can't be sure ):
use Getopt::Long;
use Data::Dumper;
my %menu;
my @root = ( \%menu, qw[add! remove! die! warn!] );
PROMPT:
print "what u want(add remove die warn)?: ";
while(<>){
chomp;
$_ = "-$_" unless /^-/; # fake it
local @ARGV = $_;
undef %menu;
if(GetOptions( @root )){
print "\n\n", Dumper \%menu;
if(exists $menu{add})
{
print "\n\nadding\n\n";
}
elsif(exists $menu{remove})
{
print "\n\nremoving\n\n";
}
elsif(exists $menu{warn})
{
warn "\n\nwarning\n\n";
}
elsif(exists $menu{die})
{
die "\n\ndying!\n\n";
}
}else{
warn "\n\nerror parsing options [$!][$@]\n\n";
}
goto PROMPT;
}
__END__
Alternatively, i'd either use
http://search.cpan.org/author/PRL/Term-Interact-0.44/Interact.pm
or
http://search.cpan.org/author/EVO/Term-ShellKit-1.002/
to develop this, possibly incorporating
http://search.cpan.org/author/DROLSKY/Params-Validate-0.24/
or
http://search.cpan.org/author/ABIGAIL/Regexp-Common-1.20/
to do validation.
Also, i'd check out the
Getopt
distributions and see if any of the modules listed suit your needs.
MJD says you
can't just make shit up and expect the computer to know what you mean, retardo!
** The Third rule of perl club is a statement of fact: pod is sexy.
|
| [reply] [d/l] |
|
|
now your talking, I guess I wasn't clear enough at the begining. Thanks a lot, it seems like a good starting point for me I'll check also the URLs you mentioned.
and again, thanks.
Hotshot
| [reply] |
Re: Getopt::Long
by duelafn (Parson) on Dec 16, 2002 at 22:44 UTC
|
Why not just ignore the unnecessary parts?
my $command = shift;
%commands = (
v => \&view,
a => \&add,
se => \&set,
sh => \&show
);
($command) = grep $command =~ /$_/, keys %commands;
&{$commands{$command}} if exists $commands{$command};
If we didn't reinvent the wheel, we wouldn't have rollerblades. | [reply] [d/l] |