1 /*
   2  * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.jps;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import sun.jvmstat.monitor.*;
  31 
  32 /**
  33  * Class for processing command line arguments and providing method
  34  * level access to the command line arguments.
  35  *
  36  * @author Brian Doherty
  37  * @since 1.5
  38  */
  39 public class Arguments {
  40 
  41     private static final boolean debug = Boolean.getBoolean("jps.debug");
  42     private static final boolean printStackTrace = Boolean.getBoolean(
  43             "jps.printStackTrace");
  44 
  45     private boolean help;
  46     private boolean quiet;
  47     private boolean longPaths;
  48     private boolean vmArgs;
  49     private boolean vmFlags;
  50     private boolean mainArgs;
  51     private String hostname;
  52     private HostIdentifier hostId;
  53 
  54     public static void printUsage(PrintStream ps) {
  55       ps.println("usage: jps [-? -h --help]");
  56       ps.println("       jps [-q] [-mlvV] [<hostid>]");
  57       ps.println();
  58       ps.println("Definitions:");
  59       ps.println("    <hostid>:      <hostname>[:<port>]");
  60       ps.println("    -? -h --help:  Prints this help message.");
  61     }
  62 
  63     public Arguments(String[] args) throws IllegalArgumentException {
  64         int argc = 0;
  65 
  66         if (args.length == 1) {
  67             if ((args[0].compareTo("-?") == 0)
  68                 || (args[0].compareTo("-h")== 0)
  69                 || (args[0].compareTo("--help")== 0)
  70                 // -help: legacy. Undocumented.
  71                 || (args[0].compareTo("-help")== 0)) {
  72               help = true;
  73               return;
  74             }
  75         }
  76 
  77         for (argc = 0; (argc < args.length) && (args[argc].startsWith("-"));
  78                 argc++) {
  79             String arg = args[argc];
  80 
  81             if (arg.compareTo("-q") == 0) {
  82               quiet = true;
  83             } else if (arg.startsWith("-")) {
  84                 for (int j = 1; j < arg.length(); j++) {
  85                     switch (arg.charAt(j)) {
  86                     case 'm':
  87                         mainArgs = true;
  88                         break;
  89                     case 'l':
  90                         longPaths = true;
  91                         break;
  92                     case 'v':
  93                         vmArgs = true;
  94                         break;
  95                     case 'V':
  96                         vmFlags = true;
  97                         break;
  98                     default:
  99                         throw new IllegalArgumentException("illegal argument: "
 100                                                            + args[argc]);
 101                     }
 102                 }
 103             } else {
 104                 throw new IllegalArgumentException("illegal argument: "
 105                                                    + args[argc]);
 106             }
 107         }
 108 
 109         switch (args.length - argc) {
 110         case 0:
 111             hostname = null;
 112             break;
 113         case 1:
 114             hostname = args[args.length - 1];
 115             break;
 116         default:
 117             throw new IllegalArgumentException("invalid argument count");
 118         }
 119 
 120         try {
 121             hostId = new HostIdentifier(hostname);
 122         } catch (URISyntaxException e) {
 123             IllegalArgumentException iae =
 124                     new IllegalArgumentException("Malformed Host Identifier: "
 125                                                  + hostname);
 126             iae.initCause(e);
 127             throw iae;
 128         }
 129     }
 130 
 131     public boolean isDebug() {
 132         return debug;
 133     }
 134 
 135     public boolean printStackTrace() {
 136         return printStackTrace;
 137     }
 138 
 139     public boolean isHelp() {
 140         return help;
 141     }
 142 
 143     public boolean isQuiet() {
 144         return quiet;
 145     }
 146 
 147     public boolean showLongPaths() {
 148         return longPaths;
 149     }
 150 
 151     public boolean showVmArgs() {
 152         return vmArgs;
 153     }
 154 
 155     public boolean showVmFlags() {
 156         return vmFlags;
 157     }
 158 
 159     public boolean showMainArgs() {
 160         return mainArgs;
 161     }
 162 
 163     public String hostname() {
 164         return hostname;
 165     }
 166 
 167     public HostIdentifier hostId() {
 168         return hostId;
 169     }
 170 }