Creating a shell script

This tutorial will demonstrate how to use the program interface definition framework to write a Bash or Zsh shell script where usage and command line option parsing is automatically generated.

Step 1. Defining the options

The first step is to describe the program options in a XML file. The XML elements have to follow the program XML Schema.


<?xml version="1.0" encoding="utf-8"?>
<prg:program version="2.0" xmlns:prg="">

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.

Step 2. Write your code

Let's write a little and useless (Bash) shell script


																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																											## Just list the command line argumentsecho "program was called with ${#} argument(s): ${@}"i=1while [ ${i} -le $# ]doecho $i:${!i}i=$(expr $i + 1)done## Parsing the command lineif ! parse "${@}"thenparse_displayerrorsexit 1fi# parse and parse_displayerrors functions will be generated automatically## Let's see what we getecho "Sub command: ${parser_subcommand}"echo "Values (${#parser_values[*]})"for v in "${parser_values[@]}"doecho " - ${v}"done# ${parser_subcommand}, ${parser_values} are defined by the parser# - ${parser_subcommand} contains the name of the sub command (if any)# - ${parser_values} is an array of anonymous values (command line arguments which are not options nor option arguments)## Display program usage if asked ${displayHelp} && usage# ${displayHelp} is defined automatically when reading your XML file# It will be set to true if the --help option is given on the command line# usage function is also generated automatically.if [ ! -z "${arg}" ]thenecho "You set the --some-arg option to ${arg}"fi# Like ${displayHelp}, ${arg} is automatically defined and its value set if the # command line contains --some-arg "arg_value" exit 0

Step 3. A little glue file

To glue the XML definition with the bash code, We will write a small XML file (a .xsh)


<?xml version="1.0" encoding="UTF-8"?>
<sh:program interpreter="/usr/bin/env bash"
  <xi:include href="miniapp.xml" />
<xi:include href="./" parse="text" />

Step 4. Build

${NSXML_PATH}/ns/sh/ -x miniapp.xml -s miniapp.xsh -o

This command line tool will generate the usage and option parsing code and append your code.

The result can be found here: samples/miniapp/

Step 5. Run!

./ --help --some-arg "Bleeeh Blaaah"

You should try to type invalid options or forget the --some-arg argument to see what's happen.

Step 6. Beyond the bash

Auto complete

To get a bash auto-completion command file, use the bashcompletion.xsl stylesheet to transform the option specification file

xsltproc -o --stringparam prg.bash.completion.programFileExtension ".sh" ${NS_XML_PATH}/ns/xsl/program/${SCHEMA_VERSION}/bashcompletion.xsl miniapp.xml

Then, include the generated file in your current environment


And try typing

./ -<TAB>

The shell will propose...

$ ./ -
-a           -h           --help       --some-arg  

See the Bash auto-complete file generation for more details.

XUL Frontend

You can automatically build a XUL application without more work. Follow the XUL Tutorial to learn how.

The program interface definition framework