Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

How to write minimal command line interface?

by kandelabr (Novice)
on Dec 27, 2003 at 01:32 UTC ( #317148=perlquestion: print w/replies, xml ) Need Help??

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

For now I wrote a scarry while() loop with "parsing" done by regular expressions and Term::ReadLine for prompt/editing/command history.

For example: if(/^l (\w+)/) implements command 'l' that accepts an argument (nevermind detail such as validating input for now).

I am sure that this is not the best way to implement input handling but I am stucked, and google/cpan gladly supply too many links to psh. Psh is neat, but I know enough perl to use Term::Readline, and not quite enough to get ideas from much bigger programs. Melp!

- Maciek

  • Comment on How to write minimal command line interface?

Replies are listed 'Best First'.
Re: How to write minimal command line interface?
by Aristotle (Chancellor) on Dec 27, 2003 at 06:24 UTC
    Maybe you're looking for Term::Shell?

    Makeshifts last the longest.

      Instead of using Term::Shell you might want to use PPM::Term::Shell or copy the into Same module, but the latter is a newer version.
Re: How to write minimal command line interface?
by bl0rf (Pilgrim) on Dec 28, 2003 at 02:26 UTC
    It really depends on what you want to achieve with this shell. If you have a reliable shell you want to piggyback then your approach is as good as mine... (except instead of having a regex for each command, do the UNIX modular design: make some small commands and one regex for a whole group of them)

    If you want to enhance a shell ( DOS comes to mind ) you need to decide what to emulate. Make some recursive subs that break a command into parts, emulate pipes "|" by openning things with  open(PRINTER, "| lpr -Plp1") Perl pipe opens, emulate file io ">","<" with... I guess pipe opens again. To run commands you can have a variable with the directory that contains all the executables, upon command submittal you can check whether the typed command is one of those executables and then run it.

Re: How to write minimal command line interface?
by delirium (Chaplain) on Dec 29, 2003 at 13:31 UTC
    This style of a command line interface works well for me:

    perl -e 'while(1) {print "::> "; $_=<STDIN>; print $/,eval $_ || '0'; +print "\n$@"}'

    ...but perhaps you could tell us more about what you are trying to do, some code you have for it, etc.

      Ahh, thanks, that is neat!

      I want that shell to present some kind of user interface for inputting a very limited set of commands. These commands will controll display of logs from about 30 machines. I have logs cleaned up, and ready to be displayed in a sanitized state with some minimal statistics as number of connections, top 10 speakers, etc.

      The shell should be able to parse something like:

      #select subset of data to work on
      show smtpproxy top10
      #display top 10 smtpproxy users for

      Assume that in the above case I would have a smtpproxy function take the domain and the option (top10) as arguments, and display formated data.

      Term::Shell might be the answer, I missed it in my search somehow. At the same time I would be glad to use my own and learn something in the process.

      -- maciek

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://317148]
Approved by bart
Front-paged by bart
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-06-28 05:39 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (90 votes). Check out past polls.