[Compiler-sig] command line interface parser

Martin von Loewis loewis@informatik.hu-berlin.de
Sat, 10 Mar 2001 12:25:53 +0100 (MET)


> I am new to this mailing list, so please excuse my ignorance. I learn about
> Python from a colleague. He says I could use it to build a flexible CLI
> parser in a grammar I specified easily with Python and have 'C' code
> generated for it for Solaris.
> 
> Has anyone done a CLI with Python ? If so, is there a reference
> implementation available ?

Not sure what kind of "command line interface" you are asking for. Do
you want to process a program's argument list (sys.argv), or do you
want to offer an interactive mode for your application?

Depending on the complexity of your language, different approaches
might be appropriate. If the language can be described in terms of
regular expressions, it is probably easiest to use the re module for
the processing. If the language is slightly more complicated, you
could try a few of the Python parser generators (they all generate
Python). I personally found Yapps to be useful for a not-so-small
language; John Aycock presented a "little languages framework" some
years ago.

As for generating C: well, probably not what you've expected. You can
certainly embed Python into your application; that is quite easy to
do. You can also "freeze" the Python modules you use, which involves
generating C code. However, that C code is only large arrays
representing bytecode; you'd still need to link the Python interpreter
into your application.

For providing interactive interfaces, it might be even easier to
decide that the command line language *is* Python. You then only need
to add a few predefined functions that are useful in your application
area (or interface with your software), and you got your CLI. Look Ma,
no compiler.

Regards,
Martin