3

I am using the picocli java command line library to implement a command line application. However I would like to know whether picocli offers a feature that can help handle the situation where the command line does not receive any arguments or options something default case.

Thank you

Arsene
  • 1,037
  • 4
  • 20
  • 47

1 Answers1

1

I'm not sure if this answers your question, but it is certainly possible to create a picocli-based command that has no options or positional parameters:

@Command(name = "demo", description = "no options or positional parameters")
public class Demo implements Runnable {

    @Override
    void run() {
        System.out.println("Hello");
    }

    public static void main(String[] args) {
        CommandLine.run(new Demo(), args);
    }
}

Or is your question about how options can be given default values? If that is the case, can you take a look at the Default Values section of the user manual and let us know what is unclear?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • I may not formulating my question well. However what I am asking is that whether picocli has a feature that allow to check whether args is empty or we have to do ourselves. – Arsene Apr 05 '19 at 18:14
  • Are you interested in finding whether the option was specified at all, or in finding whether the option was specified without option parameter? – Remko Popma Apr 05 '19 at 18:20
  • What I am saying is how to handle args == null – Arsene Apr 06 '19 at 19:10
  • Picocli won’t check, so that will likely result in a NullPointerException. The application should not pass a null args array to picocli. – Remko Popma Apr 07 '19 at 05:17
  • I have a similar question - my expectation is that given a CLI app `thingy`, regardless of subcommands, arguments etc, if invoked as just `thingy`, it should return usage info... this is what happens with, eg, git, sdk, gu, gradle, mvn, etc etc. I can't see how to do this. – bbcmicro Jul 11 '20 at 19:40
  • 1
    @bbcmicro since picocli 4.3, an easy way to accomplish this is to make the `Thingy` class _not_ implement `Runnable` or `Callable`. See [required subcommands](https://picocli.info/#_required_subcommands). Does that meet your requirements? – Remko Popma Jul 11 '20 at 20:24
  • Thanks - I will look into this. Basically I want my command to implement 1) Usage when invoked as `thingy` 2) Help and version when invoked with `-V, --version`, and `-h, --help`, 3) Have three subcommands which when invoked as `thingy subcommand` show subcommand usage and 4) When the subcommands are invoked with arguments/parameters, to take action. This is my first time using picocli, so I'm feeling my way through! – bbcmicro Jul 11 '20 at 20:42
  • 1
    @bbcmicro If the subcommands also need to show usage when invoked without arguments, one idea is to use the [@Spec annotation](https://picocli.info/#spec-annotation) in the subcommands, and call `spec.commandLine().getParseResult()` to get the `ParseResult`, and call `parseResult.expandedArgs()` to get the list of user-specified arguments. Then print usage if the list is empty. You could put this in a [Mixin](https://github.com/remkop/picocli/blob/master/docs/index.adoc#mixins) to reduce code duplication in the subcommands. (Feel free to raise a picocli GitHub ticket to discuss more details.) – Remko Popma Jul 11 '20 at 20:54
  • Thank you very much indeed... I'll plug away for a while and see where I get to. – bbcmicro Jul 11 '20 at 20:58