WeaklyTyped
WeaklyTyped

Reputation: 1341

Developing an interactive shell

I have an application; to interact with it I have to develop an interactive shell. The shell will take input from user and dispatch it to the server, which will process and return a response.

In the past I have done such things, but they typically had the following format:

Shell>> COMMAND arg1 arg2 arg3 ..

and my implementation was in a manner similiar to:

/* Mock implementation */
while(true) {
   in = readline()
   tokens = in.split(' ') //split based on space
   switch(token[0])  {
      case "Command_a": processA(token[1], token[2])
      case "Command_b": processB(token[1], token[2], token[3])
   }
}

This time I have to work with more extensive grammar for user input. If I use my current approach, it will make things very difficult with lots of if , if-elseif-else, if-else, switch statements for both parsing and generating response.

How can I approach this problem in a manner that will make the interpreter modular and maintainable? What are some ways in which popular CLI-Interface are implemented? I will really appreciate some code examples.

PS: Language choices limited C++/Python

Upvotes: 0

Views: 1882

Answers (4)

Wai Yip Tung
Wai Yip Tung

Reputation: 18754

Go for the cmd module :

The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes that will later be wrapped in a more sophisticated interface.

Upvotes: 0

Chinmay Kanchi
Chinmay Kanchi

Reputation: 65893

The cmd module in Python, perhaps with the addition of Pyparsing should fit your needs perfectly.

Upvotes: 1

mitchnull
mitchnull

Reputation: 6331

Take a look at boost::spirit, or just embed Lua as others suggested.

Upvotes: 1

piokuc
piokuc

Reputation: 26164

You can develop your own parser and interpreter using bison and flex, but probably wiser is to embed an existing interpreter like Python, Lua or Squirrel.

Upvotes: 2

Related Questions