Completing multiple arguments with values

173 views
Skip to first unread message

Bryan Bende

unread,
Jan 30, 2018, 10:34:43 AM1/30/18
to jline-users

Using JLine 3.5.2, I'm trying to setup a completer for something like this:

cmd1 -argA argAvalue -argB argBvalue -argC argCvalue
cmd2 -argX argXvalue -argY argYvalue -argZ argZvalue

The order of the arguments does not matter, so I wanted to be able to handle argA, argB, and argC being in any order.

I attempted to create an ArgumentCompleter for each command, and then combine them in an aggregate, like this:

https://github.com/bbende/jline-examples/blob/master/src/main/java/com/bbende/jline/MultiArgumentCompleterIssue.java#L34-L49

With the code exactly as it is, the completion of the commands will work until you have typed the value of the first argument:

cm1 -argA foo

At that point, no more completion occurs because "foo" does not match anything. If you only type "cmd -argA " and didn't type foo, then you would be able to hit tab again and see the other available args for cmd1.

If call setStrict(false) on each of the argument completers in the above code, it allows completion to continue working, but has the side effect that completion is no longer limited to the given command.

Typing tab after entering "cmd1 " will display the args of both commands.

Typing tab after entering "cmd1 -argA foo -arg" will display the args of both commands.

Any thoughts how to achieve this?

Thanks.

Guillaume Nodet

unread,
Jan 30, 2018, 10:55:23 AM1/30/18
to jline...@googlegroups.com
JLine only provides bits and pieces when it comes to completers.  The reason is that it heavily depends on how your commands are built, how the completion options are written, etc...
The only completer which can actually be used to perform full command completion is the the Completers.Completer one, but it's missing half the code.  That's the one which is used by gogo in the jline demo. See https://github.com/jline/jline3/wiki/Completion#completer for pointers.
Another one you could look at, which is the non-trivial version of the JLine's ArgumentCompleter is the Karaf's ArgumentCompleter:
You'll see it looks inside command fields to grab each option / argument completer and build a full completer.

JLine does not provide any shell related code, that's why you'll need to look a bit outside the project for real completers use cases.

--
You received this message because you are subscribed to the Google Groups "jline-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jline-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bryan Bende

unread,
Jan 30, 2018, 1:53:48 PM1/30/18
to jline-users
Thanks for the feedback and pointers.

I just spent some time writing a custom Completer and was able to make it work for what I wanted to do.

It may not be the cleanest solution, but does the job for what I need.

Thanks.
To unsubscribe from this group and stop receiving emails from it, send an email to jline-users...@googlegroups.com.

Nguyễn Nam PTIT

unread,
Jun 2, 2020, 6:43:59 AM6/2/20
to jline-users
Hi Bryan Bende,
How did you solve this problem? Do you have available code on github?

Vào 01:53:48 UTC+7 Thứ Tư, ngày 31 tháng 1 năm 2018, Bryan Bende đã viết:
Reply all
Reply to author
Forward
0 new messages