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

Hierarchical commands easy setup #90

Closed
adibacco opened this issue Jan 12, 2017 · 3 comments
Closed

Hierarchical commands easy setup #90

adibacco opened this issue Jan 12, 2017 · 3 comments
Milestone

Comments

@adibacco
Copy link

Setting up a Completer is very flexible but sometimes it generates a long piece of code that is also quite difficult to read. It would be really nice to have a Completer that can be setup with a tree-like structure whose nodes contain a List of Candidates. This approach doesn't cover all possible scenarios but it is very useful in most cases. Consider a Cisco-like CLI for example.

I think the same approach is used in https://github.com/dparrish/libcli

@gnodet
Copy link
Member

gnodet commented Jan 13, 2017

I see the use case. I haven't run into yet though, mostly because most of the commands I define take options, so that a strict hierarchical completion does not really help.
What could be interesting also is a slightly more complicate completer that you could configure using a syntax such as:

(C1 | C2 | C3)* C4? C5+

which would mean : 0 or more occurrences of c1 or c2 or c3, eventually followed by c4, followed by one or more c5, where c[1-9] would be keys into a Map<String, Completer>.

@gnodet gnodet closed this as completed in 104a5eb Jan 13, 2017
@gnodet gnodet changed the title Hierarchical commands easy setup (feature request) Hierarchical commands easy setup Jan 13, 2017
@gnodet gnodet added this to the 3.2.0 milestone Jan 13, 2017
@gnodet
Copy link
Member

gnodet commented Jan 13, 2017

See

case "tree":
completer = new TreeCompleter(
node("Command1",
node("Option1",
node("Param1", "Param2")),
node("Option2"),
node("Option3")));
break label;
case "regexp":
Map<String, Completer> comp = new HashMap<>();
comp.put("C1", new StringsCompleter("cmd1"));
comp.put("C11", new StringsCompleter("--opt11", "--opt12"));
comp.put("C12", new StringsCompleter("arg11", "arg12", "arg13"));
comp.put("C2", new StringsCompleter("cmd2"));
comp.put("C21", new StringsCompleter("--opt21", "--opt22"));
comp.put("C22", new StringsCompleter("arg21", "arg22", "arg23"));
completer = new Completers.RegexCompleter("C1 C11* C12+ | C2 C21* C22+", comp::get);
break label;

@adibacco
Copy link
Author

adibacco commented Jan 13, 2017

Really neat way to specify a tree-like commands structure. Just for people that would need "hint" for certain values to insert I created a class like this:

package org.jline.reader;

import java.util.Objects;

public class Hint extends Candidate {

	public Hint(String suggestion) {
		super("", suggestion, null, "", null, null, false);
	}
	public Hint(String suggestion, String description) {
		super("", suggestion, null, description, null, null, false);
	}
}

and then I modified Completer.java like this:


                    if (res instanceof Candidate) {
                        candidates.add((Candidate) res);
                    } else if (res instanceof Hint) {
                        candidates.add((Hint) res);
                    } else if (res instanceof String) {

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

No branches or pull requests

2 participants