• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Marc R. Hoffmann - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.cli.internal;
14 
15 import java.io.IOException;
16 import java.io.PrintWriter;
17 import java.io.Writer;
18 
19 import org.kohsuke.args4j.Argument;
20 import org.kohsuke.args4j.CmdLineException;
21 
22 /**
23  * Entry point for all command line operations.
24  */
25 public class Main extends Command {
26 
27 	private static final PrintWriter NUL = new PrintWriter(new Writer() {
28 
29 		@Override
30 		public void write(final char[] arg0, final int arg1, final int arg2)
31 				throws IOException {
32 		}
33 
34 		@Override
35 		public void flush() throws IOException {
36 		}
37 
38 		@Override
39 		public void close() throws IOException {
40 		}
41 	});
42 
43 	private final String[] args;
44 
Main(final String... args)45 	Main(final String... args) {
46 		this.args = args;
47 	}
48 
49 	@Argument(handler = CommandHandler.class, required = true)
50 	Command command;
51 
52 	@Override
description()53 	public String description() {
54 		return "Command line interface for JaCoCo.";
55 	}
56 
57 	@Override
usage(final CommandParser parser)58 	public String usage(final CommandParser parser) {
59 		return JAVACMD + "--help | <command>";
60 	}
61 
62 	@Override
execute(PrintWriter out, final PrintWriter err)63 	public int execute(PrintWriter out, final PrintWriter err)
64 			throws Exception {
65 
66 		final CommandParser mainParser = new CommandParser(this);
67 		try {
68 			mainParser.parseArgument(args);
69 		} catch (final CmdLineException e) {
70 			((CommandParser) e.getParser()).getCommand().printHelp(err);
71 			err.println();
72 			err.println(e.getMessage());
73 			return -1;
74 		}
75 
76 		if (help) {
77 			printHelp(out);
78 			return 0;
79 		}
80 
81 		if (command.help) {
82 			command.printHelp(out);
83 			return 0;
84 		}
85 
86 		if (command.quiet) {
87 			out = NUL;
88 		}
89 
90 		return command.execute(out, err);
91 	}
92 
93 	/**
94 	 * Main entry point for program invocations.
95 	 *
96 	 * @param args
97 	 *            program arguments
98 	 * @throws Exception
99 	 *             All internal exceptions are directly passed on to get printed
100 	 *             on the console
101 	 */
main(final String... args)102 	public static void main(final String... args) throws Exception {
103 		final PrintWriter out = new PrintWriter(System.out, true);
104 		final PrintWriter err = new PrintWriter(System.err, true);
105 		final int returncode = new Main(args).execute(out, err);
106 		System.exit(returncode);
107 	}
108 
109 }
110