views:

169

answers:

2

I am looking for a library or set of libraries that will help me write a forgiving command interpreter.

A forgiving command interpreter would be a command interpreter which can deal with simple and even not so simple spelling and word order mistakes in the input.

My goal is to have an interpreter which would take the input (a command) from a user and then:

  1. execute the command, if it is correct.

  2. apply corrections to the command, until a correct command is generated and then present that command to the user to confirm whether it is 'what the user meant'.

For example:

The GlassFish Server has a command-line administrative interface, named asadmin. It allows users to script interaction with the server and work with it interactively.

An asadmin command has a fairly rigid structure:

asadmin <asadmin options> <subcommand> <subcommand-options> <operand>

The asadmin command currently attempts to help users that misspell a command, like this:

vkraemer$ ../../GlassFish3.0.1.b12/glassfish/bin/asadmin creat-domain --user foobar --portbase 2600 fubar
Deprecated syntax, instead use:
asadmin --user foobar creat-domain [options] ...
Remote server does not listen for requests on [localhost:4848].
Is the server up?
Unable to get remote commands. 
Closest matching local command(s): 
    create-domain
Command creat-domain failed.

This is a pretty good catch. It is not as smart when it comes to misspelled options.

I would like to remove almost all the constraints on the input, to allow users to make multiple mistakes in a command, but still get to the point where they have a chance to execute a command.

Consider this example HCI:

vkraemer$ ../../GlassFish3.0.1.b12/glassfish/bin/asadmin create-domain foobar
Enter admin user name [Enter to accept default "admin" / no password]> 
Domain foobar already exists in /Users/vkraemer/GlassFish3.0.1.b12/glassfish/domains. Use a different domain name or the --domaindir option.
CLI130 Could not create domain, foobar
Command create-domain failed.
VBKMacBookPro:web-main vkraemer$ ../../GlassFish3.0.1.b12/glassfish/bin/asadmin create-domain foobar --domaindi /tmp/test
Enter admin user name [Enter to accept default "admin" / no password]> 
Command create-domain only accepts one operand
Usage: asadmin [asadmin-utility-options] create-domain
    [--adminport <adminport(default:4848)>]
    [--instanceport <instanceport(default:8080)>] [--portbase <portbase>]
    [--profile <profile>] [--template <template>] [--domaindir <domaindir>]
    [--savemasterpassword[=<savemasterpassword(default:false)>]]
    [--domainproperties <domainproperties>]
    [--keytooloptions <keytooloptions>]
    [--savelogin[=<savelogin(default:false)>]]
    [--checkports[=<checkports(default:true)>]]
    [--nopassword[=<nopassword(default:false)>]]
    [-?|--help[=<help(default:false)>]] domain_name
Command create-domain failed.

It would be nice to be able to allow the second command to complete successfully, even though the option --domaindir is spelled incorrectly and is in the 'wrong place' in the command.

For example the output of the second create-domain command might be

VBKMacBookPro:web-main vkraemer$ ../../GlassFish3.0.1.b12/glassfish/bin/asadmin create-domain foobar --domaindi /tmp/test
Enter admin user name [Enter to accept default "admin" / no password]> 
Did you mean "create-domain --domaindir /tmp/test foobar"? (yes)
A: 

Have a look at Peter Norvig's How to Write a Spelling Corrector. His algorithm should help you catch simple typos.

McDowell
+1  A: 

The command interpreter in JNode does command completion that is aware of each command's syntax and command argument semantics. It does not deal with spelling/typing mistakes, but if you use completion they are less of a problem.

Stephen C