0

Bear with me for my fledgling knowledge of Python and please feel free to point me to update the title if it sounds incorrect.

I am looking for a python module that can run as an independent thread(Producer) and can accept strings from Terminal and pass those strings to another thread(Consumer) which will ultimately parse these strings and treat them as commands for a specific domain and will ultimately act upon it. I have implemented something similar in recent past in C++ via worker threads and queues and function call-back and will ultimately do so if nothing is found in Python's vast amount of open source archive. But if somebody know of some open source module that can fulfill this functionality, I will be highly obliged.

I have found a similar question on SO here which points to a curses based library but that is not what I am looking for.

I have also found some code which can serve my purpose here and is very similar to the work I have done with C++ but It definitely needs a lot of bells and whistles before converting to a full blown CLI application.

Any pointers are appreciated.

  • I also saw your post on stackoverflow, but unfortunately can't read the comment on the deleted question. If you think it adds information would you mind posting it here? Celery is a common taskqueue, but that might be overkill for your case. queue is threadsave if you only need interthread communication. There is lark is you want a "real parser", but that might be overkill for you. Without details it's hard to recommend more specific libraries. – syntonym Sep 7 '18 at 22:55
  • @syntonym: Thanks for the Comment. May be I was not clear enough. I want a prebuilt library/module/package in Python which shall continuously run either as a standalone application and read terminal strings and pass it over to another python program which I will code myself to handle these strings OR it can be integrated to my main program which will parse the strings delivered to it by this library/module/package. In both cases its job is to continuously monitor the terminal and collect the strings provided by the shell which can or can not be shell commands – Black_Zero Sep 8 '18 at 5:25
  • With terminal do you mean stdin or do you more think of a socket which a client program connects to (like redis-cli, psql, i3-msg, notify-send)? – syntonym Sep 8 '18 at 9:33
  • @syntonym: Terminal mean stdin where I will be starting my application. Actually I am writing a test application in Python which will send some REST request to a server and will store the response in a database. It will provide a CLI interface to users which will be able to modify the database entries via a custom list of commands. Therefore you can say that my whole application has two parts one which will deal with REST requests and Database and the other part which will handle the CLI commands handling and parsing. This is the part for which I am looking for a ready-made solution. – Black_Zero Sep 8 '18 at 18:24
0

If I understood you correctly you are looking for a python library which helps you in parsing the command line. The python standard library has a few modules: argparse is the "standard" one. If you are coming from C/C++ you may be alreaddy familiar with getopt. I think optparse is a "convenience wrapper" around it, although it is deprecated with 2.7/3.2.

Outside of the standard library there are many libraries. click is my default to go for CLI parsing. But there are numerous others, a quick google showed e.g. knack or cement (I just found these so no idea what they actually do).

There are also some projects which do some integration/magic between REST and CLI. In hug you can create APIs which is exposed as a REST an a CLI. [Clam] claims about itself: "Quickly turn command-line applications into RESTful webservices with a web-application front-end".

This assumes that you actually want to create a command line interface (CLI), not a shell (read commands from stdin). For shell a good visualisation/low level toolkit is prompt-toolkit. As far as I know it does not do parsing, but things like input editing (like GNU readline), completion, history etc. While it is named prompt-toolkit I think it can also do TUIs ("ncurses user interfaces"). Via googling I also found ishell which describes itself as following: "ishell helps you to easily create an interactive shell for your application. It supports command completion, dynamic arguments, a command history, and chaining of commands".

If you want to create a TUI I know of three frameworks: urwid, picotui and asciimatics.

  • Thank you very much. Coincidentally, I also found Click during the search which suffices my requirement and can easily be integrated with my application. – Black_Zero Sep 14 '18 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.