Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jps/Arguments.java
41159 views
/*1* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jps;2627import java.io.*;28import java.net.*;29import sun.jvmstat.monitor.*;3031/**32* Class for processing command line arguments and providing method33* level access to the command line arguments.34*35* @author Brian Doherty36* @since 1.537*/38public class Arguments {3940private static final boolean debug = Boolean.getBoolean("jps.debug");41private static final boolean printStackTrace = Boolean.getBoolean(42"jps.printStackTrace");4344private boolean help;45private boolean quiet;46private boolean longPaths;47private boolean vmArgs;48private boolean vmFlags;49private boolean mainArgs;50private String hostname;51private HostIdentifier hostId;5253public static void printUsage(PrintStream ps) {54ps.println("usage: jps [--help]");55ps.println(" jps [-q] [-mlvV] [<hostid>]");56ps.println();57ps.println("Definitions:");58ps.println(" <hostid>: <hostname>[:<port>]");59ps.println(" -? -h --help -help: Print this help message and exit.");60}6162public Arguments(String[] args) throws IllegalArgumentException {63int argc = 0;6465if (args.length == 1) {66if ((args[0].compareTo("-?") == 0)67|| (args[0].compareTo("-h")== 0)68|| (args[0].compareTo("--help")== 0)69// -help: legacy.70|| (args[0].compareTo("-help")== 0)) {71help = true;72return;73}74}7576for (argc = 0; (argc < args.length) && (args[argc].startsWith("-"));77argc++) {78String arg = args[argc];7980if (arg.compareTo("-q") == 0) {81quiet = true;82} else if (arg.startsWith("-")) {83for (int j = 1; j < arg.length(); j++) {84switch (arg.charAt(j)) {85case 'm':86mainArgs = true;87break;88case 'l':89longPaths = true;90break;91case 'v':92vmArgs = true;93break;94case 'V':95vmFlags = true;96break;97default:98throw new IllegalArgumentException("illegal argument: "99+ args[argc]);100}101}102} else {103throw new IllegalArgumentException("illegal argument: "104+ args[argc]);105}106}107108switch (args.length - argc) {109case 0:110hostname = null;111break;112case 1:113hostname = args[args.length - 1];114break;115default:116throw new IllegalArgumentException("invalid argument count");117}118119try {120hostId = new HostIdentifier(hostname);121} catch (URISyntaxException e) {122IllegalArgumentException iae =123new IllegalArgumentException("Malformed Host Identifier: "124+ hostname);125iae.initCause(e);126throw iae;127}128}129130public boolean isDebug() {131return debug;132}133134public boolean printStackTrace() {135return printStackTrace;136}137138public boolean isHelp() {139return help;140}141142public boolean isQuiet() {143return quiet;144}145146public boolean showLongPaths() {147return longPaths;148}149150public boolean showVmArgs() {151return vmArgs;152}153154public boolean showVmFlags() {155return vmFlags;156}157158public boolean showMainArgs() {159return mainArgs;160}161162public String hostname() {163return hostname;164}165166public HostIdentifier hostId() {167return hostId;168}169}170171172