Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2011 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 package com.google.caliper.util;
     16 
     17 import com.google.common.collect.ImmutableList;
     18 
     19 import java.io.PrintWriter;
     20 import java.util.List;
     21 
     22 /**
     23  * Exception that signifies that the <i>user</i> has given an invalid argument string.
     24  */
     25 @SuppressWarnings("serial") // who would serialize a command-line parsing error?
     26 public class InvalidCommandException extends RuntimeException {
     27   private ImmutableList<String> usage;
     28 
     29   public InvalidCommandException(String message, Object... args) {
     30     super(String.format(message, args));
     31   }
     32 
     33   public void setUsage(List<String> usage) {
     34     this.usage = ImmutableList.copyOf(usage);
     35   }
     36 
     37   public void display(PrintWriter writer) {
     38     writer.println(getMessage());
     39     if (usage != null) {
     40       writer.println();
     41       displayUsage(writer);
     42     }
     43   }
     44 
     45   protected final void displayUsage(PrintWriter writer) {
     46     for (String line : usage) {
     47       writer.println(line);
     48     }
     49   }
     50 
     51   public int exitCode() {
     52     return 1;
     53   }
     54 }
     55