Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e1aa1fafd8efb1d6271c332b92dfae08a7d6e564 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*******************************************************************************
* Copyright (c) 2018 protos software gmbh (http://www.protos.de).
* All rights reserved.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* CONTRIBUTORS:
*           Jan Belle (initial contribution)
*
 *******************************************************************************/

package org.eclipse.etrice.generator.base.cli;

import java.util.Formatter;

import org.eclipse.etrice.generator.base.args.Option;
import org.eclipse.etrice.generator.base.args.Options;
import org.eclipse.etrice.generator.base.args.StringArrayOption;

/**
 * Simple implementation for command line help formatting.
 */
public class HelpFormatter implements IHelpFormatter {
	
	public HelpFormatter() {
	}
	
	@Override
	public String getHelp(String name, Options options, StringArrayOption defaultOption) {
		try(Formatter formatter = new Formatter()) {
			formatter.format("%s usage: [options] %s...%n", name, defaultOption.getName());
			formatter.format("Options:%n");
			for(Option<?> opt: options) {
				if(opt != defaultOption) {
					String optStr = "-" + opt.getName();
					if(!opt.getType().equals(Boolean.class)) {
						optStr += " <" + opt.getArgumentName() + ">";
					}
					formatter.format(" %-30s %s%n", optStr, opt.getDescription());
				}
			}
			return formatter.toString();
		}
	}
	
}

Back to the top