Skip to content

--help detection ignores a command's own options #1370

Description

@czpilar

AbstractCommand decides whether the user asked for help by looking only at the option names typed on the command line, never at what the command declares. Two defects follow from that.

@Command(name = "connect", description = "Connect to a host", help = "Help of 'connect'")
public void connect(@Option(shortName = 'h', longName = "host") String host, @Option String port) {
	System.out.println("connecting to host=" + host + " port=" + port);
}

1. A declared -h is hijacked as help

isHelp() treats any option with shortName == 'h' as a help request. The parser resolves the value correctly and execute() then discards it, so the command never runs - and the exit status is OK, so it fails silently:

shell:>connect -h myhost
Help of 'connect'                     <-- expected: connecting to host=myhost

shell:>connect --host myhost
connecting to host=myhost port=null   <-- the same option, spelled long, works
Image

(The third line in the screenshot above is explained in the last section.)

2. Help is only honoured when it is the sole option

The dispatch condition is options.size() == 1 && isHelp(...), so anything else on the line suppresses it:

shell:>connect --help=true
Help of 'connect'

shell:>connect --help=true --port 22
connecting to host=null port=22       <-- expected: Help of 'connect'
Image

These cannot be fixed separately

options.size() == 1 is what currently masks defect 1 (the third line of the first screenshot):

shell:>connect -h myhost --port 22
connecting to host=myhost port=22     <-- correct today, only because size() == 2

Relax the dispatch on its own and that line prints help instead, widening defect 1 from one option to any number. A change fixing only defect 2 is therefore a regression.

Reproduced on main.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status/need-triageTeam needs to triage and take a first look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions