From 53f75a85cc427a2400dc950e28dde482fa0ce52f Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Mon, 17 Jun 2013 12:06:13 -0400 Subject: [PATCH] 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 ' for usage." or just displaying the lower level error message, such as "Expected whitespace" --- main/command/src/main/scala/sbt/Command.scala | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/main/command/src/main/scala/sbt/Command.scala b/main/command/src/main/scala/sbt/Command.scala index c4f360634..1a5e9355a 100644 --- a/main/command/src/main/scala/sbt/Command.scala +++ b/main/command/src/main/scala/sbt/Command.scala @@ -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 =>