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

AutoComplete Command not found #636

Closed
mrdantutunaru opened this issue Feb 20, 2019 · 28 comments
Closed

AutoComplete Command not found #636

mrdantutunaru opened this issue Feb 20, 2019 · 28 comments

Comments

@mrdantutunaru
Copy link

mrdantutunaru commented Feb 20, 2019

Hello and thank you for this beautiful library.
I am new to picocli, so I want to implement a menu with autocomplete possibility.
My code is the following :

@Command(name = "licenserES", version = "Help demo v1.0.0", header = "%nAutomatic License Helper%n",
        description = "Prints usage help and version help when requested.%n",
        subcommands = {GenerateKey.class, Sign.class, Verify.class})
public class LicenseCommandLine implements Callable<Void> {

    @Option(names = {"--help", "-h"}, usageHelp = true, description = "Print usage help and exit.")
    boolean help;
    @Option(names = {"--generateKey", "-g"}, description = "Generates a key.")
    boolean gk;
    @Option(names = {"--sign", "-s"}, description = "Signs the license.")
    boolean sl;
    @Option(names = {"--verify", "-v"}, description = "Verifies the license.")
    boolean v;
    @Option(names = {"--exit", "-e"}, description = "Exit the app.")
    boolean exit;

    public static void main(String[] args) {
        args = new String[]{"--help" };
        LicenseCommandLine licenseCommandLine = new LicenseCommandLine();
        new CommandLine(licenseCommandLine).parseWithHandler(new RunAll(), args);
    }

    @Override
    public Void call() {
        System.out.println("Menu LicenseCLI");
        return null;
    }
}

When I run this class from IDE, it works fine, it shows me the menu:

Automatic License Helper

Usage: licenserES [-eghsv] [COMMAND]
Prints usage help and version help when requested.

  -e, --exit          Exit the app.
  -g, --generateKey   Generates a key.
  -h, --help          Print usage help and exit.
  -s, --sign          Signs the license.
  -v, --verify        Verifies the license.
Commands:
  --generateKey  Generate the Key.
  --sign         Sign the license.
  --verify       Verify license passed as an argument

After I generated my licenserES_completion, and the source of this script. AutoComplete works fine if a write licenserES ---> it will show me these options, but if I type licenserES --help ...it keeps saying licenserES command not found. Any sugestions on this? Or maybe you have a good example.
Thanks in advance !

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

When you say

if I type licenserES --help ... it keeps saying licenserES command not found

do you mean when you type this in the bash shell?

Have you tried putting a ./ in front of the command so that the shell knows to look for the command in the current directory? So, in the bash shell, type:

$ ./licenserES --help

@mrdantutunaru
Copy link
Author

Thank you for the quick response, yes I tried, same result. Yes, I mean bash shell.

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

Did you create a shell script for the licenserES command? The picocli.AutoComplete utility can generate such a script if you specify the --writeCommandScript option.

You can also create it yourself. Something like this:

#!/usr/bin/env bash

LIBS=path/to/libs
CP="${LIBS}/LicenserES.jar"
java -cp "${CP}" 'com.company.package.LicenseCommandLine' $@

(but replace the above with the correct value for LIBS, the correct jar name and the correct fully qualified class name)

You should make this shell script executable with:

$ chmod 755 licenserES

@mrdantutunaru
Copy link
Author

mrdantutunaru commented Feb 20, 2019

Yes, sir I created. And it works fine, when I type:
`/es/licenser$ licenserES -- [TAB][TAB]

--exit --generateKey --help --sign --verify `

But, when I choose: licenserES --help

It gives me this message : licenserES: command not found

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

I believe this is the bash shell telling you it cannot find the licenserES script in the current directory. Do you have a file named licenserES in the current directory when you run the licenserES --help command?

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

By the way, the licenserES_completion script and the licenserES command script are two separate scripts.

@mrdantutunaru
Copy link
Author

mrdantutunaru commented Feb 20, 2019

No, I don't have that file. Only licenserES_completion. But I did this step :

# 1. Source all completion scripts in your .bash_profile
#
#   cd $YOUR_APP_HOME/bin
#   for f in $(find . -name "*_completion"); do line=". $(pwd)/$f"; grep "$line" ~/.bash_profile || echo "$line" >> ~/.bash_profile; done

Am I supposed to do and this?

The picocli.AutoComplete utility can generate such a script if you specify the --writeCommandScript option.

You can also create it yourself. Something like this:

#!/usr/bin/env bash

LIBS=path/to/libs
CP="${LIBS}/LicenserES.jar"
java -cp "${CP}" 'com.company.package.LicenseCommandLine' $@

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

Yes, you need both scripts. Sounds like you already have the licenserES_completion script so all you need is a licenserES command script. The command script is the script that calls java with your class.

@mrdantutunaru
Copy link
Author

I tried to add '--writeCommandScript' when running picocli.Autocomplete. But gives me this error :
Invalid value for option '--writeCommandScript': 'help' is not a boolean

What value do I need to pass for '--writeCommandScript' ?
Thanks

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

Can you please show the exact command you used to run picocli.Autocomplete, and the full output (including a stack trace if there was one)?

@mrdantutunaru
Copy link
Author

I do like this:

image

Stacktrace: Invalid value for option '--writeCommandScript': 'licenserES' is not a boolean

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

Okay, thanks for the clarification. Please specify -w without any arguments. So, program arguments should be:

-n=licenseES -w com.esempla.licenser.LicenseCommandLine

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

By the way (unrelated): I strongly recommend everyone to upgrade to picocli 3.9.5 (see release notes) to protect against a bug in jansi-1.14 that may crash the JVM on some versions of Linux with some versions of Java.

@mrdantutunaru
Copy link
Author

mrdantutunaru commented Feb 20, 2019

Sir, here are my last updates, I was able to generate 2 files [ licenserES ] and [ licenserES_completion ].
I updated the licenserES with the following:

#!/usr/bin/env bash

LIBS=/home/user/workspace/es/es-licenser/target
CP="${LIBS}/licenser-1.0.jar"
java -cp "${CP}" 'com.esempla.licenser.LicenseCommandLine' $@

and made it executable. Now I type the following : ./licenserES [TAB] [TAB,] I get my options.
But, If I type ./licenserGRM --help

Error: Unable to initialize main class com.esempla.licenser.LicenseCommandLine
Caused by: java.lang.NoClassDefFoundError: picocli/CommandLine$IParseResultHandler2

So it seems like a problem with my Main class?

Thanks.

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

You also need the picocli jar in the classpath.

One way to do this, in your licenserES command script, replace the CP line with this:

CP="${LIBS}/licenser-1.0.jar:${LIBS}/picocli-3.9.5.jar"

(and make sure the picocli jar exists in the /home/user/workspace/es/es-licenser/target directory)

@mrdantutunaru
Copy link
Author

mrdantutunaru commented Feb 20, 2019

Thank you a lot, Sir !
it seems to work now, but now for [licenserES] autocomplete is not working. Is this the expected behavior?
When I do, ./licenserES [TAB][TAB], it doesn't show my options.

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

You may need to source the licenserES_completion again, or update your .bash_profile if the location of the licenserES_completion script changed.

@mrdantutunaru
Copy link
Author

I figured it out. Thank you again for your support !

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

Glad to hear I was able to help. Don't forget to star picocli if you like it! :-)

@remkop remkop closed this as completed Feb 20, 2019
@mrdantutunaru
Copy link
Author

mrdantutunaru commented Feb 20, 2019

Sorry, my last question. What can I do, so I don't have to run like this ./licenserES ?
Is there any way to run script like this : licenserES ?
because if I do like this, i get this answer:

user@pc:~/workspace/es/es-licenser$ licenserES --help 
licenserES: command not found

@mrdantutunaru
Copy link
Author

Glad to hear I was able to help. Don't forget to star picocli if you like it! :-)

Already did :) 👍

@remkop
Copy link
Owner

remkop commented Feb 20, 2019

To be honest, I don’t know why the ./ is necessary. I would expect all scripts in the current directory to be in the PATH...

Is the script executable? (Did you chmod 755 <script> it?)

Perhaps you can find the answer to that question on StackOverflow. We’re surely not the first people to have this issue.

@mrdantutunaru
Copy link
Author

Yes, I did. Okay, thank you one more time !

@mrdantutunaru
Copy link
Author

@remkop Hello, Mr. Pompa. I tried to find myself, but was not able. Is autocomplete working with @parameters ? Or only with @option? Thanks

@remkop
Copy link
Owner

remkop commented Feb 21, 2019

Current support for completion on positional parameters is limited. There are some outstanding tickets to improve this, but I’m unable to work on that now.

@mrdantutunaru
Copy link
Author

@remkop Thanks

@mrdantutunaru
Copy link
Author

mrdantutunaru commented Feb 21, 2019

@remkop sorry :) ..for subcommands options, is AutoComplete possible?

@remkop
Copy link
Owner

remkop commented Feb 21, 2019

Yes, options on subcommands (and nested sub-subcommands) should work.
Please raise a separate ticket with steps to reproduce if there is any issue.

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