• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *    Marc R. Hoffmann - initial API and implementation
10  *
11  *******************************************************************************/
12 package org.jacoco.cli.internal;
13 
14 import java.io.PrintWriter;
15 import java.io.StringWriter;
16 
17 import org.kohsuke.args4j.Option;
18 
19 /**
20  * Common interface for all commands.
21  */
22 public abstract class Command {
23 
24 	/**
25 	 * Common command line prefix.
26 	 */
27 	public static final String JAVACMD = "java -jar jacococli.jar ";
28 
29 	/**
30 	 * Flag whether help should be printed for this command.
31 	 */
32 	@Option(name = "--help", usage = "show help", help = true)
33 	public boolean help = false;
34 
35 	/**
36 	 * Flag whether output to stdout should be suppressed.
37 	 */
38 	@Option(name = "--quiet", usage = "suppress all output on stdout")
39 	public boolean quiet = false;
40 
41 	/**
42 	 * @return Short description of the command.
43 	 */
description()44 	public abstract String description();
45 
46 	/**
47 	 * @return name of the command
48 	 */
name()49 	public String name() {
50 		return getClass().getSimpleName().toLowerCase();
51 	}
52 
53 	/**
54 	 * @param parser
55 	 *            parser for this command
56 	 * @return usage string displayed for help
57 	 */
usage(final CommandParser parser)58 	public String usage(final CommandParser parser) {
59 		final StringWriter writer = new StringWriter();
60 		parser.printSingleLineUsage(writer, null);
61 		return JAVACMD + name() + writer;
62 	}
63 
64 	/**
65 	 * Executes the given command.
66 	 *
67 	 * @param out
68 	 *            std out
69 	 * @param err
70 	 *            std err
71 	 * @return exit code, should be 0 for normal operation
72 	 * @throws Exception
73 	 *             any exception that my occur during execution
74 	 */
execute(PrintWriter out, PrintWriter err)75 	public abstract int execute(PrintWriter out, PrintWriter err)
76 			throws Exception;
77 
78 	/**
79 	 * Prints textual help for this command.
80 	 *
81 	 * @param writer
82 	 *            output destination
83 	 */
printHelp(final PrintWriter writer)84 	protected void printHelp(final PrintWriter writer) {
85 		final CommandParser parser = new CommandParser(this);
86 		writer.println(description());
87 		writer.println();
88 		writer.println("Usage: " + parser.getCommand().usage(parser));
89 		parser.printUsage(writer, null);
90 	}
91 
92 }
93