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.
The first step is to describe the program options in a XML file. The XML elements have to follow the program
XML Schema.
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 (Bash) shell script
samples/miniapp/miniapp.body.sh
## 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
To glue the XML definition with the bash code, We will write a small XML file (a .xsh)
This command line tool will generate the usage and option parsing code and append your code.
The result can be found here: samples/miniapp/miniapp.sh
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 stylesheet to transform the option specification file
Then, include the generated file in your current environment
And try typing
The shell will propose...
See the Bash auto-complete file generation for more details.
You can automatically build a XUL application without more work. Follow the XUL Tutorial to learn how.