~davewalker/etherpad/ubuntu-unlimited-max-users-and-revisions

« back to all changes in this revision

Viewing changes to infrastructure/net.appjet.common.cli/cli.scala

  • Committer: James Page
  • Date: 2011-04-13 08:00:43 UTC
  • Revision ID: james.page@canonical.com-20110413080043-eee2nq7y1v7cv2mp
* Refactoring to use native Ubuntu Java libraries. 
* debian/control:
  - use openjdk instead of sun's java
  - update maintainer
* debian/etherpad.init.orig, debian/etherpad.upstart:
  - move the init script out of the way
  - create a basic upstart script
  - note that the open office document conversion daemon was dropped
    from the upstart configuration; if this behavior is desired, please
    create a separate upstart job for it
* debian/rules:
  - just use basic dh_installinit, as it will pick up the new upstart job
* New release
* Changed maintainer to Packaging
* Fixed installation scripts
* Initial Release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2009 Google Inc.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS-IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
package net.appjet.common.cli;
 
18
 
 
19
import org.apache.commons.lang.WordUtils;
 
20
 
 
21
class CliOption(val name: String, val description: String, val argName: Option[String]);
 
22
 
 
23
class ParseException(message: String) extends RuntimeException(message);
 
24
 
 
25
class CliParser(predef: Array[CliOption]) {
 
26
  val displayWidth = 80;
 
27
  val options = Map((for (opt <- predef) yield ((opt.name, opt))): _*);
 
28
 
 
29
  def parseOptions(args0: Array[String]): (Map[String, String], Array[String]) = {
 
30
    val (opts, args) = args0.partition(_.startsWith("-"));
 
31
    (Map((for (arg <- opts) yield {
 
32
      val parts = arg.split("=", 2);
 
33
      val name = "-+".r.replaceFirstIn(parts(0), "");
 
34
      if (parts.length == 1 && options.get(name).map(_.argName.isDefined).exists(x => x))
 
35
        throw new ParseException("Missing argument for flag: "+name);
 
36
      (name, parts.orElse(Map(1 -> "true"))(1));
 
37
    }): _*),
 
38
     args.toArray);
 
39
  }
 
40
 
 
41
  def dprint(prefix: String, value: String) = {
 
42
//    println(prefix+": "+value+"\n");
 
43
    value;
 
44
  }
 
45
 
 
46
  def usage = {
 
47
    val sb = new StringBuilder();
 
48
    var maxLength = predef.map(opt => 2 + opt.name.length + opt.argName.map(_.length + 1).getOrElse(0) ).reduceRight(Math.max)+2;
 
49
    for ((n, opt) <- options) {
 
50
      sb.append("  --"+n+opt.argName.map("=<"+_+">").getOrElse("")+"\n");
 
51
      sb.append("     "+WordUtils.wrap(opt.description, displayWidth-5).split("\n").mkString("\n     "));
 
52
      sb.append("\n\n");
 
53
    }
 
54
    sb.toString();
 
55
  }
 
56
}