Skip to content

[Bug] Passing the --help option with a subcommand prints the primary command help instead #156

Description

@shortercode

I'm trying to build a CLI app which follows the PROGRAM CATEGORY COMMAND syntax pattern, while testing the help generation I discovered that the help option only ever runs for the primary command. By debugging the library I found this in parse.js

// If default help is allowed, check for it
if (this.config.help) {
  this.checkHelp()
}

which runs before the subcommand interpretation, causing the application the print the primary command help instead of the subcommand help and never call the subcommand.

Example code

app.js

#!/usr/bin/env node
import args from 'args';

args.command('sub', 'Do the subcommand');
args.parse(process.argv);

app-sub.js

#!/usr/bin/env node
import args from 'args';

args.parse(process.argv);

Output

> app --help
  Usage: app [options] [command]
  
  Commands:
    help      Display help
    sub  Do the subcommand
    version   Display version
  
  Options:
    -h, --help     Output usage information
    -v, --version  Output the version number
> app sub --help
   Usage: app [options] [command]
  
  Commands:
    help      Display help
    sub  Do the subcommand
    version   Display version
  
  Options:
    -h, --help     Output usage information
    -v, --version  Output the version number
> app sub help
   Usage: app sub [command]
  
  Commands:
    help      Display help
    version   Display version
  
  Options:
    -h, --help     Output usage information
    -v, --version  Output the version number

The help subcommand appears to work correctly, looking at the contents of checkHelp it appears to immediately run help if the option is set. Whereas the sub command is registered before and interpreted later in the subcommand interpretation.

Removing the if (this.optionWasProvided('help')) check from checkHelp and placing it after the subcommand checks in parse would resolve this issue and make the help option consistent with the help subcommand.

The same issue appears to be true for versions as well, but I haven't tested this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions