Basic named commands now print usage if the argument parser fails on empty input. Fixes #776.

The Help for these commands now needs to be cleaned up, since they were not written with
this feature in mind.  In particular,

* consider adding syntax summaries in the short help strings
* alternatively, add the syntax summary data elsewhere for use specifically by this feature
* display a better message when there is no short help string, such as
   "See 'help <command>' for usage." or just displaying the lower level error message, such as
   "Expected whitespace"
This commit is contained in:
Mark Harrah 2013-06-17 12:06:13 -04:00
parent 684dda144f
commit 53f75a85cc
1 changed files with 7 additions and 2 deletions

View File

@ -13,7 +13,7 @@ sealed trait Command {
def tags: AttributeMap
def tag[T](key: AttributeKey[T], value: T): Command
}
private[sbt] final class SimpleCommand(val name: String, help0: Help, val parser: State => Parser[() => State], val tags: AttributeMap) extends Command {
private[sbt] final class SimpleCommand(val name: String, private[sbt] val help0: Help, val parser: State => Parser[() => State], val tags: AttributeMap) extends Command {
assert(Command validID name, "'" + name + "' is not a valid command name." )
def tag[T](key: AttributeKey[T], value: T): SimpleCommand = new SimpleCommand(name, help0, parser, tags.put(key, value))
def help = const(help0)
@ -72,7 +72,12 @@ object Command
b => () => f(a,b)
def simpleParser(cmds: Seq[SimpleCommand]): State => Parser[() => State] =
simpleParser(cmds.map(sc => (sc.name, sc.parser)).toMap )
simpleParser(cmds.map(sc => (sc.name, argParser(sc) )).toMap )
private[this] def argParser(sc: SimpleCommand): State => Parser[() => State] =
{
def usageError = s"${sc.name} usage:" + Help.message(sc.help0, None)
s => (Parser.softFailure(usageError, definitive = true): Parser[() => State]) | sc.parser(s)
}
def simpleParser(commandMap: Map[String, State => Parser[() => State]]): State => Parser[() => State] =
(state: State) => token(OpOrID examples commandMap.keys.toSet) flatMap { id =>