-
Notifications
You must be signed in to change notification settings - Fork 1
LegacyPythonTutorial
This tutorial will demonstrate how to use the The program interface definition framework to write a Python script where usage and command line option parsing is automatically generated.
- [Creating a Python script](#Creating a Python script)
- [Step 1. Defining the options](#Step 1. Defining the options)
- [Step 2. Write your code](#Step 2. Write your code)
- [Step 3. Build](#Step 3. Build)
- [Step 4. Run!](#Step 4. Run!)
- [Step 5. Beyond the Python script](#Step 5. Beyond the Python script)
- [Auto complete](#Auto complete)
The first step is to describe the program options in a XML file. The XML elements have to follow the program interface XML Schema. samples/miniapp/miniapp.xml
#!xml
<?xml version="1.0" encoding="utf-8"?>
<prg:program version="2.0" xmlns:prg="http://xsd.nore.fr/program">
<prg:name>miniapp</prg:name>
<prg:version>1.0</prg:version>
<prg:options>
<prg:switch>
<prg:databinding>
<prg:variable>displayHelp</prg:variable>
</prg:databinding>
<prg:names>
<prg:long>help</prg:long>
<prg:short>h</prg:short>
</prg:names>
</prg:switch>
<prg:argument>
<prg:databinding>
<prg:variable>arg</prg:variable>
</prg:databinding>
<prg:names>
<prg:long>some-arg</prg:long>
<prg:short>a</prg:short>
</prg:names>
</prg:argument>
</prg:options>
</prg:program>
You can use any editor to write this file but an XML editor with auto-completion and XML Schema support will greatly increase the writing speed.
Let's write a little and useless Python script
#!Python
#!/usr/bin/env python
import Program
import sys
p = Program.Program()
"""Create an instance of your program description"""
result = p.parse(sys.argv[1:len(sys.argv)])
"""Call the command line parser"""
if result.options.displayHelp:
print p.usage(result, Program.Usage.Abstract)
"""Print the help message"""
exit(0)
if not result.is_valid:
for e in result.issues["errors"]:
print e
exit(1)
print "--some-arg: " + str(result.options.arg)
#!bash
${NS_XML_PATH}/ns/sh/build-python.sh -x miniapp.xml -p miniapp.py
This command line tool will generate a Python module folder where the miniapp.py
resides. This module contains a copy of the ${NS_XML_PATH}/ns/python/program/${SCHEMA_VERSION}
folder and an additional auto-generated Program.py
#!bash
./miniapp.py --help --some-arg "Bleeeh Blaaah"
You should try to type invalid options or forget the --some-arg
argument to see what's happen.
To get a bash auto-completion command file, use the bashcompletion.xsl
style sheet to transform the option specification file
#!bash
xsltproc -o miniapp-autocomplete.inc.sh --stringparam prg.bash.completion.programFileExtension ".py" ${NS_XML_PATH}/ns/xsl/program/${SCHEMA_VERSION}/bashcompletion.xsl miniapp.xml
Then, include the generated file in your current environment
#!bash
. miniapp-autocomplete.inc.sh
And try typing
#!bash
./miniapp.py -<TAB>
The shell will propose...
#!bash
$ ./miniapp.py -
-a -h --help --some-arg
See the Bash auto-complete file generation for more details.