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

How to suggest options and sub-command at the same time? #827

Closed
zlxie opened this issue Sep 27, 2019 · 23 comments
Closed

How to suggest options and sub-command at the same time? #827

zlxie opened this issue Sep 27, 2019 · 23 comments
Labels
status: declined ❌ A suggestion or change that we don't feel we should currently apply theme: auto-completion An issue or change related to auto-completion

Comments

@zlxie
Copy link

zlxie commented Sep 27, 2019

Hi,
I have command line like "icmd -v -h -o sub1 sub2", and sub1 and sub2 are sub commands. I tried to use picocli(version is 4.0.4) to generate auto completion script, and it only works as below:
->icmd TAB TAB
sub1 sub2
->icmd -TAB
-v -h -o
But actually I want to show all options and sub-command as below. How should I do? Thanks.
->icmd
-v -h -o sub1 sub2

@remkop
Copy link
Owner

remkop commented Sep 27, 2019

Hi, that was done to allow default bash completions.
This can get quite complex. See the discussion in #461

My understanding is that the current picocli completion behaviour is consistent with bash-completion. I am open to improvements though.

@remkop remkop added the theme: auto-completion An issue or change related to auto-completion label Sep 27, 2019
@DaitariJena1505
Copy link

Hi,

I have sping boot + picocli application. I want to implement TAB auto completion feature to show the params and options in my application. Could you please help me how to do that. I am using picocli-3.8.2.jar.

Your help is most appriciated.

Thanks
Daitari Jena

@remkop
Copy link
Owner

remkop commented Nov 18, 2019

Hi @DaitariJena1505, I would always recommend upgrading to the latest version (picocli 4.0.4). Is there anything preventing you from upgrading?

About your question, have you had a chance to read the autocompletion user manual?

@DaitariJena1505
Copy link

No the problem is when i am executing the command : java -cp "picocli-3.8.2.jar:my.jar" picocli.AutoComplete -n login /BOOT-INF/classes/com.xyz.123.cli.command.LoginCommand , it is showing the error

java.lang.ClassNotFoundException: /BOOT-INF/classes/com.xyz.123.cli.command.LoginCommand

@remkop
Copy link
Owner

remkop commented Nov 18, 2019

You need to specify a fully qualified class name. Have you tried without the “ /BOOT-INF/classes/” prefix?

java -cp "picocli-3.8.2.jar:my.jar" picocli.AutoComplete -n login com.xyz.123.cli.command.LoginCommand

@DaitariJena1505
Copy link

Yes, i have tried that one also but same issue.

@remkop
Copy link
Owner

remkop commented Nov 19, 2019

Surely it cannot be the same; at least the error message should be different... Can you let me know the exact error message?

Is there any way for me to try to reproduce the issue? Could you create a test project?

@remkop
Copy link
Owner

remkop commented Nov 19, 2019

Also, can you post the result of jar -tvf myjar.jar? I would like to see where the com.xyz.123.cli.command.LoginCommand class is located inside the jar.

@DaitariJena1505
Copy link

5372 Mon Nov 18 12:53:26 GMT 2019
BOOT-INF/classes/com/xyz/123/cli/command/LoginCommand.class

@remkop
Copy link
Owner

remkop commented Nov 19, 2019

Ok that’s the problem. That’s a Spring executable jar, not a normal jar.

Your LoginCommand class is not visible on the classpath.

Can you extract your class and run the autocompletion generator again?

@DaitariJena1505
Copy link

After extraction, where i will put the class ?

@remkop
Copy link
Owner

remkop commented Nov 19, 2019

Just any directory is fine. For example:

.
├── classes
│   └── com
│       └── xyz
│           └── 123
│               └── cli
│                   └── command
│                       └── LoginCommand.class
└──  picocli-4.0.4.jar

Then, run:

java -cp "picocli-4.0.4.jar:classes" picocli.AutoComplete -n login \ 
  com.xyz.123.cli.command.LoginCommand

@remkop
Copy link
Owner

remkop commented Nov 19, 2019

@DaitariJena1505 By the way, see AutoCompletion distribution for an alternative way to distribute autocompletion scripts with your application: the idea is to give your top-level command a subcommand that end-users can use to generate and install the autocompletion script. (Just an idea.)

@DaitariJena1505
Copy link

Sorry to ask again, after extract the LoginCommand.class i have to create folder structure as above and re-build the jar again ?

@remkop
Copy link
Owner

remkop commented Nov 19, 2019

After you have run java -cp "picocli-4.0.4.jar:classes" picocli.AutoComplete -n login com.xyz.123.cli.command.LoginCommand you should have a completion script that you can distribute with your application. If the old (Spring executable) jar myjar.jar works for you, then just keep using that, I guess. I am not very familiar with Spring executable jars.

@DaitariJena1505
Copy link

Do you have any sample project for Auto completion ? If you can share it would be a great help for me.

@DaitariJena1505
Copy link

After generating the completion script, how will i use that script ?

@remkop
Copy link
Owner

remkop commented Nov 20, 2019

@DaitariJena1505 You install it by sourcing the script. Then you can use tab completion for that command in that shell session.

@DaitariJena1505
Copy link

You mean to say i have to run -> source login_completion
The run the jar -> java -jar myjar.jar
After that project will ask for login using login command that time tab completion will work ?

@remkop
Copy link
Owner

remkop commented Nov 20, 2019

No, if you run java -jar x.jar it won’t work.
Please look at the documentation: you need to create a named command in Unix. You can use an alias or a script.

If your command is login, create a script with that name that invokes java -jar x.jar or something to invoke your java program. This script then becomes the command that end users use to run your program.

When you create an autocompletion script you need to specify the command name. This command name (basically your script) is associated with a specific completion script and only when users enter that command it will trigger the bash completion for that command.

@DaitariJena1505
Copy link

Hi,
Just want to clarify the below points.

Step 1 : alias login='java -cp "picocli-3.8.2.jar:myproject.jar" com.test.LoginCommand'

Step 2 : java -cp "picocli-3.8.2.jar:myproject.jar" picocli.AutoComplete -n login com.xyz.123.cli.command.LoginCommand

Step 3: Auto Completion script got created with the name - login_completion

Step 4 : i have write a script where i have to write the below code.

echo "alias login='java -cp "picocli-1.0.0.jar;myproject.jar" com.test.LoginCommand'" >> ~/.bashrc
echo ". ogin_completion" >> ~/.bashrc

Could you please confirm if the above steps are correct for auto completion.

@remkop
Copy link
Owner

remkop commented Nov 21, 2019

Looks more or less okay. Some details:

Use full path to the jars in the alias: if the user invokes the login command, the shell will try to run java -cp "picocli-3.8.2.jar:myproject.jar" com.test.LoginCommand. This may succeed, if those two jars exist in the current directory. It may be better to specify the full path to these jar files in the alias.

In the alias, the class name is com.test.LoginCommand, but your completion script is created for com.xyz.123.cli.command.LoginCommand. Should these not be the same? (This may not be a problem if these have the same options and positional parameters).

echo ". ogin_completion" >> ~/.bashrc
should be
echo ". login_completion" >> ~/.bashrc

@remkop remkop added the status: declined ❌ A suggestion or change that we don't feel we should currently apply label Jan 9, 2020
@remkop
Copy link
Owner

remkop commented Jan 9, 2020

Closing for now: current picocli completion behaviour is consistent with bash-completion.

@remkop remkop closed this as completed Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined ❌ A suggestion or change that we don't feel we should currently apply theme: auto-completion An issue or change related to auto-completion
Projects
None yet
Development

No branches or pull requests

3 participants