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

Match help information with .parse({ from }) #2276

Open
hypeJunction opened this issue Nov 1, 2024 · 4 comments
Open

Match help information with .parse({ from }) #2276

hypeJunction opened this issue Nov 1, 2024 · 4 comments

Comments

@hypeJunction
Copy link

Displayed help information does not align with what the user input actually is, in case from property is set during parsing. Help still shows the main command from node perspective.

@shadowspawn
Copy link
Collaborator

I am not sure what the problem is. Can you give an example of what you are trying to do and what you see?

@hypeJunction
Copy link
Author

I will post an actual example when I am back at my computer. The problem is that help prefixes the command with the program name, even though it parses just the command. Parsing user input actually fails if you add the name of the program before it.

@hypeJunction
Copy link
Author

Here is the Screencast I made earlier. As you see what help says is not what you have to type to execute a command.
382253310-ca079a5c-2f15-4d66-ae47-b3e7bb0fd270.webm

@shadowspawn
Copy link
Collaborator

Ah, I see. The help does assume it should show the root command (program) in the usage.

A quick hack would be to set the program name to a space. There will be some extra spaces in the displayed usage, but really easy!

program.name(' ');

A full solution is to change the method building the usage. (You could leave out the ancestorCmdNames related code if you don't have nested subcommands.)

// Do now include the "program name" since it does
// not make sense in our usage.
program.configureHelp({
  commandUsage: (cmd) => {
    if (!cmd.parent) return cmd.usage();

    let cmdName = cmd.name();
    if (cmd._aliases[0]) {
      cmdName = cmdName + '|' + cmd._aliases[0];
    }
    let ancestorCmdNames = '';
    for (
      let ancestorCmd = cmd.parent;
      ancestorCmd && ancestorCmd.parent;
      ancestorCmd = ancestorCmd.parent
    ) {
      ancestorCmdNames = ancestorCmd.name() + ' ' + ancestorCmdNames;
    }
    return ancestorCmdNames + cmdName + ' ' + cmd.usage();

  }}
);

const fooCommand = program.command('foo');
const barCommand = fooCommand.command('bar');
$ node repl.mjs --help | head -n 1
Usage: [options] [command]
$ node repl.mjs foo --help | head -n 1
Usage: foo [options] [command]
$ node repl.mjs foo bar --help | head -n 1
Usage: foo bar [options]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants