package org.mskcc.cbio.portal.scripts;

import java.io.IOException;

import joptsimple.OptionParser;

/**
 * RuntimeException to signal invalid command line usage
 *
 * @see {@link ConsoleRunnable}
 */
public class UsageException extends IllegalArgumentException {
    
    // the name of the command
    private String prog;
    // an OptionParser configured with the accepted options
    private OptionParser parser;
    // a string describing the arguments accepted
    private String argspec;
    // short description of the tool to display in the usage line
    private String description;
    // a final error/exit message
    private String msg;
    
    /**
     * Instantiates a UsageException using an OptionParser.
     *
     * @param prog  the name of the command
     * @param description  short description of the tool to display in
     *                     the usage line
     * @param parser  an OptionParser configured with the accepted options
     */
    public UsageException(
            String prog,
            String description,
            OptionParser parser) {
        this.prog = prog;
        this.description = description;
        this.parser = parser;
        this.argspec = null;
    }
    
    /**
     * Instantiates a UsageException using an explicit argument specification.
     *
     * @param prog  the name of the command
     * @param description  short description of the tool to display in
     *                     the usage line
     * @param argspec  the string describing the arguments accepted
     *
     * @see  <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_01">
     *           chapter 12.01 of the POSIX specification, on utility argument
     *           syntax conventions
     *       </a>
     */
    public UsageException(
            String prog,
            String description,
            String argspec) {
        this.prog = prog;
        this.description = description;
        this.argspec = argspec;
        this.parser = null;
    }

    /**
     * Instantiates a UsageException using an OptionParser.
     *
     * @param prog  the name of the command
     * @param description  short description of the tool to display in
     *                     the usage line
     * @param parser  an OptionParser configured with the accepted options
     */
    public UsageException(
            String prog,
            String description,
            OptionParser parser,
            String msg) {
        this(prog, description, parser);
        this.msg = msg;
    }
    
    /**
     * Instantiates a UsageException using an explicit argument specification.
     *
     * @param prog  the name of the command
     * @param description  short description of the tool to display in
     *                     the usage line
     * @param argspec  the string describing the arguments accepted
     * @param msg  a final error/exit message
     *
     * @see  <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_01">
     *           chapter 12.01 of the POSIX specification, on utility argument
     *           syntax conventions
     *       </a>
     */
    public UsageException(
            String prog,
            String description,
            String argspec,
            String msg) {
        this(prog, description, argspec);
        this.msg = msg;
    }
    
    /**
     * Prints a helpful message about command line usage of the utility.
     */
    public void printUsageLine() {
        if (msg != null){
            System.err.printf("%s\n\n", msg);
        }
        if (argspec != null) {
            System.err.printf("usage: %s %s\n", prog, argspec);
        }
        if (description != null) {
            System.err.printf("%s\n", description);
        }
        if (parser != null) {
            try {
                System.err.printf("Command line arguments for %s:\n", prog);
                parser.printHelpOn(System.err);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

@@ -1081,24 +1081,83 @@
 r parser) %7B%0A
+        super(%22Invalid usage of the %22 + prog + %22 script%22);%0A
         this
@@ -1162,32 +1162,32 @@
 is.prog = prog;%0A
-
         this.des
@@ -1244,37 +1244,8 @@
 er;%0A
-        this.argspec = null;%0A
     
@@ -1934,24 +1934,83 @@
  argspec) %7B%0A
+        super(%22Invalid usage of the %22 + prog + %22 script%22);%0A
         this
@@ -2099,36 +2099,8 @@
 ec;%0A
-        this.parser = null;%0A
     
@@ -2552,32 +2552,52 @@
   String msg) %7B%0A
+        super(msg);%0A
         this(pro
@@ -2596,35 +2596,90 @@
 this
-(
+.
 prog
-, description, parser)
+ = prog;%0A        this.description = description;%0A        this.parser = parser
 ;%0A  
@@ -3460,32 +3460,52 @@
   String msg) %7B%0A
+        super(msg);%0A
         this(pro
@@ -3504,36 +3504,92 @@
 this
-(
+.
 prog
-, description, argspec)
+ = prog;%0A        this.description = description;%0A        this.argspec = argspec
 ;%0A  
@@ -3771,16 +3771,16 @@
  null)%7B%0A
+
         
@@ -3806,18 +3806,16 @@
 tf(%22%25s%5Cn
-%5Cn
 %22, msg);

Call superclass constructor in UsageException