CMDSyntax

Home David Projects CMDSyntax
Updated: 2016-01-05

Introduction

Python programs which run from the command line have traditionally relied on a variety of libraries and methods to read and interpret the arguments passed to the program when it is executed. Such libraries offer fairly convenient facilities for checking the presence of options, commands and associated parameters but still require the developer to do much of the work of making sense of this information.

For more complex programs, the logic surrounding the options available becomes increasingly difficult to manage as developers must check for invalid combinations of large numbers of options. Therefore, for certain types of command line tool, with syntaxes which offer many conflicting choices for the user, a solution which scales to large numbers of options is required.

This module provides an alternative way to collect arguments passed to a script from the command line. Rather than treating the command line as a pool of arguments, the contents are matched against a specified syntax string. Any valid matches are presented to the program as dictionaries containing values referenced by keys in the syntax string. Since contradictory or inconsistent use of options is eliminated by the matching process, the developer can concentrate on providing the features of the program rather than having to validate the user input.

The values obtained from the command line are not converted from the default string type. It may be beneficial to use features of other option parsing libraries to provide this functionality.

Methods of input

The module allows the user to pass arguments to a script in the conventional manner from the command line according to an agreed style of command line arguments.

The use of a syntax definition to validate the command line input is also useful for users of graphical user interfaces (GUIs) since it may be used to automate the construction of simple GUI forms. Therefore, some level of consistency is introduced between the two alternative interfaces with little effort from the developer. Additionally, the information obtained from invalid input can be used to assist the user by incorporating the user's valid input into the form being built.

The GUI form generation is achieved using the Tkinter module to utilise the Tk toolkit. This toolkit is in widespread use and is therefore likely to be available on the user's machine.

A simple example

Using an existing tool demonstrates the capabilities of the library. The tool in question, impconvert (see this page), converts documents stored in a legacy document format to a series of files corresponding to the pages in the document. The tool creates a Syntax object from the definition given:


  syntax = "<Input file> <Output directory> <File extension>" + \
           " [-s <Starting page>] [-f <Finishing page>]"

Consider the case where the user invokes the impconvert tool with the following arguments:

  impconvert /home/anon/input/myfile,bc5 /home/anon/output/

This input is incomplete and, as a result, the syntax object created by the tool will not return any matches. However, the tool can do two things to help the user supply valid input: it can generate a form for the user to fill in and it can interpret the failed input to provide useful default values for various parameters which the user specified correctly.

Figure 1 shows the form generated from the syntax definition given above. The labels, <Input file>, <Output directory> and <File extension> are translated to pairs of Label and Entry widgets since the user is expected to supply information in a textual form for those arguments. Whether the information is to be converted to another more suitable form (coercing strings to integers, for example) is outside the scope of this module.

Options -s and -c are represented as simple Label widgets rather than as Checkbutton widgets. This is because the optional nature of these elements is encapsulated in the square brackets which surround them and their following parameters, <Starting page> and <Finishing page>.



Figure 1: A simple form generated from a syntax definition, presented using Tkinter.

More descriptive GUI forms

Using a modified style which allows commands (arguments which must be supplied exactly as specified) to contain whitespace, more elaborate forms can be generated, although this is not the primary goal of the module.

Consider the following syntax definition:


    syntax = """
    
        "Woodland census 2002"
        (
            
            [ --Squirrels=Number ]
            [ --Badgers=Number ]
            [ --Hippos=Number ]
            
            "Types of trees in the forest"
            {
                --Oak
                --Birch
                --Sycamore
                --Fir
            }
        )
    
    """

Figure 2 shows the form which is generated in this case.

Note the use of "long" option names to provide descriptive labels in the generated form. Those options which require values are converted to pairs of labels and input fields; in this case, QLabel and QLineEdit widgets are used.

Parentheses are used in the definition to group options together, only changing the meaning of the syntax without affecting how it may be presented in a form. However, the use of the {...} pair both changes the meaning of the options enclosed and the way they are presented. This grouping requires at least one of the options to be selected.



Figure 2: A more elaborate form generated from a stylised syntax definition.

License

I have adopted an MIT-style license for CMDSyntax:

  
  CMDSyntax License:
  
  Copyright (c) 2002-3, David Boddie
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.
  

Download

An experimental version, containing an example script to compare against other libraries is available, a version of which can be found on the Bake-off page. cmdsyntax-0.91.tar.gz

A more detailed description of the library and further examples can be found in the CMDSyntax Usage and Reference Guide.