Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global parser configuration if using Runnable #650

Closed
gitfineon opened this issue Mar 28, 2019 · 10 comments
Closed

Global parser configuration if using Runnable #650

gitfineon opened this issue Mar 28, 2019 · 10 comments
Labels
Milestone

Comments

@gitfineon
Copy link
Contributor

I tried to apply parser configuration e.g. disallow POSIX Clustered Short Option, but I don't know where to place the CommandLine.setPosixClusteredShortOptionsAllowed(false).

I want to keep it simple and straight forward as possible so I started with your README Example (Runnable) and added three boolean switches ... and now I want to achieve that the clustered version of them will result in an "unknown option" + usage (preferred without showing the options clustered)

I search in the documentation but didn't found an explanation.

I would prefer to have this parser configuration set globally for the whole application, but individually for each command would be okay as well.

@gitfineon
Copy link
Contributor Author

@Command(name = "example")
public class Example2 implements Runnable {
	@Option(names = { "-a", "--a-dummy" })
	boolean aVal;
	@Option(names = { "-b", "--b-dummy" })
	boolean bVal;
	@Option(names = { "-c", "--c-dummy" })
	boolean cVal;
	@Option(names = { "-v", "--verbose" }, description = "Verbose mode. Helpful for troubleshooting.")
	private boolean[] verbose = new boolean[0];

	public void run() {
		System.out.println("Hello ");
		if (verbose.length > 0) {
			System.out.println("verbose mode activated");
		}
		if (aVal | bVal | cVal) {
			System.out.println("boolean option(s) selected");
		}
	}

	public static void main(String[] args) {
//		CommandLine.setPosixClusteredShortOptionsAllowed(false);
		CommandLine.run(new Example2(), args);
	}
}

@remkop
Copy link
Owner

remkop commented Mar 28, 2019

Yes, this is a known issue (see #561) and is planned to be fixed in picocli 4.0.

You need to create a CommandLine instance, so you can call setPosixClusteredShortOptionsAllowed on it. Instead of the static run method you’ll need to use the parseWithHandlers method. #556 has an example.

@remkop remkop added the status: duplicate 👨🏻‍🤝‍👨🏻 A duplicate of another issue label Apr 2, 2019
@remkop remkop added this to the 4.0 milestone Apr 2, 2019
@remkop remkop modified the milestones: 4.0, 4.0-alpha-3 Apr 24, 2019
remkop added a commit that referenced this issue Apr 24, 2019
…te` and `tryExecute` methods: configurable convenience methods with improved exit code support.

* The new `execute` and `tryExecute` methods are similar to the `run`, `call` and `invoke` methods, but are not static, so they allow parser configuration.
* In addition, these methods, in combination with the new `IExitCodeGenerator` and `IExitCodeExceptionMapper` interfaces, offer clean exit code support.
* Finally, the `tryExecute` method rethrows any exception thrown from the Runnable, Callable or Method, while `execute` is guaranteed to never throw an exception.
* Many variants of the previous `run`, `call` and `invoke` convenience methods are now deprecated in favor of the new `execute` methods.
* Many methods on `AbstractHandler` are now deprecated.

Still TODO: tests and documentation.
@remkop
Copy link
Owner

remkop commented Apr 24, 2019

An initial version just landed in master. Please check it out if you have a chance.

remkop added a commit that referenced this issue Apr 26, 2019
remkop added a commit that referenced this issue Apr 26, 2019
remkop added a commit that referenced this issue Apr 27, 2019
remkop added a commit that referenced this issue Apr 30, 2019
remkop added a commit that referenced this issue May 1, 2019
…invoke() methods and associated classes and interfaces; update Javadoc
@remkop remkop closed this as completed in 1161b05 May 2, 2019
remkop added a commit that referenced this issue May 3, 2019
…er interface to simplify custom implementations; unwrap the `ExecutionException` in the `CommandLine.execute` method
@remkop
Copy link
Owner

remkop commented May 14, 2019

@gitfineon

picocli-4.0-alpha-3 has been released with a new execute API that facilitates configuration.

This is the last alpha! Please take a look and provide feedback if you have a chance.

@gitfineon
Copy link
Contributor Author

Sry for not responding, the new features are awesome. Especially, the better return code support and exit code help. I'll have a closer look this week. Is there a estimated release date for 4.0 (of course w/o guarantees).

@remkop
Copy link
Owner

remkop commented May 15, 2019

It's a bit hard to tell, depends on how fast I can go through the backlog for 4.0-beta-1 and 4.0. I may drop some of the items that are currently on the 4.0 TODO list. I'd guess roughly 1-2 months to 4.0-GA.
Let me know you you feel like helping out with some PRs. :-)

@gitfineon
Copy link
Contributor Author

Upgraded my project to the use "execute" commandline and it works, but I get some warnings in the beginning. Are you aware of them?

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by picocli.CommandLine$Help$Ansi (file:/C:/Users/.../ext/picocli/picocli.jar) to field java.io.FilterOutputStream.out
WARNING: Please consider reporting this to the maintainers of picocli.CommandLine$Help$Ansi
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

For reference:

public class Tool implements Runnable {

	OutputHelper out;

	/**
	 * Command specification.
	 *
	 * see picocli.CommandLine.Model.CommandSpec
	 */
	@Spec
	CommandSpec commandSpec;

	/**
	 * Option to request UsageHelp of main application
	 *
	 * see picocli.CommandLine.Option.usageHelp
	 */
	@SuppressWarnings("javadoc")
	@Option(names = { "-h", "--help" }, usageHelp = true, description = "Display this help message")
	private boolean usageHelpRequested = false;

	/**
	 * Option to globally request verbose mode
	 */
	@Option(names = { "-v", "--verbose" }, description = { "Enable verbose output" })
	boolean verboseMode = false;

	/**
	 * (non-Javadoc)
	 *
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		...
	}

	/**
	 * The initial main function of the tool is responsible to configure the
	 * command line parser as well as primary output streams
	 *
	 * @param args
	 *            Array of all (whitespace separated) arguments passed
	 */
	public static void main(String[] args) {

		// Prepare PrintWriter with auto-flush option
		PrintWriter out = new PrintWriter(System.out, true);
		PrintWriter err = new PrintWriter(System.err, true);
		// ColorScheme auto-detection of ANSI terminals
		Ansi ansi = Help.Ansi.AUTO;

		CommandLine cmd = new CommandLine(new Tool())
				// set output
				.setOut(out).setErr(err)
				// disable clustering support for short options (e.g. -hv)
				.setPosixClusteredShortOptionsAllowed(false)
				// set color scheme
				.setColorScheme(Help.defaultColorScheme(ansi));

		// Register default result handler and default exception handler.
		int exitCode = cmd.execute(args);

		System.exit(exitCode);
	}
}

Thank you for the timeline.

@remkop
Copy link
Owner

remkop commented May 15, 2019

I hadn’t noticed these warnings yet because I work mostly on Java 8.
I’ll take a look. Thank you!

@remkop
Copy link
Owner

remkop commented May 15, 2019

@gitfineon Would you mind creating a new ticket for this? Please mention which version of Java you are using.

@gitfineon
Copy link
Contributor Author

#695

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants