vorpal icon indicating copy to clipboard operation
vorpal copied to clipboard

Print help programmatically

Open scarabdesign opened this issue 7 years ago • 14 comments

I'm wondering if there is a way (or if you can add a way) to print the help text with a command, instead of waiting for the user to fuck up the input or type help. I'd like the user to get a list of command (the help text) when the app first launches without any input first.

Perhaps something like vorpal.help() to print the main help or vorpal.help("mycommand") to print the help for a command. In the code you can detect the input type and if it's a function it would work as currently designed and create custom help text. If input is empty string, print main help, if it's a string, print help for string if matching command exists, if not, print main help or error out

scarabdesign avatar Apr 10 '17 23:04 scarabdesign

Same request here

PatoSalazarNascent avatar Apr 11 '17 14:04 PatoSalazarNascent

Hi there. You could manually execute help command with vorpal.exec('help') and vorpal.exec('help commandName'). Hope that helps.

postatum avatar Apr 13 '17 06:04 postatum

it does..thanks!

PatoSalazarNascent avatar Apr 13 '17 14:04 PatoSalazarNascent

Yes! Thank you, that will work great.

scarabdesign avatar Apr 13 '17 15:04 scarabdesign

So I am still having an issue. if I call vorpal.exec('help') inside the validate function, I still can't see the output.Nothing fails, just the help does not display. What would be the problem with this approach??... Any help would be appreciated

 .validate(function (args) {
    if (args.classtype == classtype.service
        || args.classtype == classtype.controller
          || args.classtype == classtype.manager) 
    {
      return true
    }
    // add this message to errors object
    shelljs.echo(`the type ${args.classtype} is not supported by the cli `);
    vorpal.exec('help');
    return false;
  })

PatoSalazarNascent avatar Apr 13 '17 16:04 PatoSalazarNascent

Hmm, it worked for me. Perhaps it needs to get piped somehow to the shelljs lib you are using?

Just to verify, if you put vorpal into a test script that does nothing but print the help, does that work?

scarabdesign avatar Apr 13 '17 17:04 scarabdesign

@PatoSalazarNascent maybe vorpal is a different object in your scope or maybe output is consumed by something. I've tried running your code and it worked.

var vorpal = require('vorpal')()
var shelljs = require('shelljs')

var classtype = {
  service: 'foo',
  controller: 'bar',
  manager: 'baz'
}

vorpal
  .command('make <classtype>')
  .description('make classtype')
  .validate(function (args) {
    if (args.classtype == classtype.service
        || args.classtype == classtype.controller
          || args.classtype == classtype.manager)
    {
      return true
    }
    // add this message to errors object
    shelljs.echo(`the type ${args.classtype} is not supported by the cli `);
    vorpal.exec('help');
    return false;
  })
  .action(function(args){
    console.log(args)
  });

vorpal.show()
// vorpal.exec('make hello')
$ node index.js 

local@pc~$ make asdasd
the type asdasd is not supported by the cli 

  Commands:

    help [command...]  Provides help for a given command.
    exit               Exits application.
    make <classtype>   make classtype

postatum avatar Apr 13 '17 17:04 postatum

question... I have two different commands like this. Is this correct or I don't need another reference to vorpal to create a different commands. Maybe this could be affecting the execution of help??


vorpal
  .command('make <classtype>')
  .description('make classtype')
  .validate(function (args) {
    if (args.classtype == classtype.service
        || args.classtype == classtype.controller
          || args.classtype == classtype.manager)
    {
      return true
    }
    // add this message to errors object
    shelljs.echo(`the type ${args.classtype} is not supported by the cli `);
    vorpal.exec('help');
    return false;
  })
  .action(function(args){
    console.log(args)
  })

vorpal
.command(otherMake <command>)
.etc(...)

PatoSalazarNascent avatar Apr 13 '17 19:04 PatoSalazarNascent

question... I have two different commands like this. Is this correct or I don't need another reference to vorpal to create a different commands. Maybe this could be affecting the execution of help??

I believe multiple commands can be defined with the same instance of vorpal. I've tried doing so and help still works the way I described.

I would recommend isolating your command and trying to make it log help. Then add things step by step to find a thing that may be interfering with help output.

postatum avatar Apr 14 '17 05:04 postatum

@PatoSalazarNascent I have found that each command needs to reference the one vorpal instance created with require("vorpal")() but they need to be declared separately. So, this works:

vorpal
  .command(...)
  .action(...);
vorpal
  .command(...)
  .action(...);

But, I wasn't able to stack them, so it did not work like this:

vorpal
  .command(...)
  .action(...)
  .command(...)
  .action(...);

scarabdesign avatar Apr 14 '17 15:04 scarabdesign

@scarabdesign right. That's what I meant

postatum avatar Apr 14 '17 16:04 postatum

awesome I'll keep exploring where my mistake is and I'll post my findings here when I finally figure it out thanks for your help

PatoSalazarNascent avatar Apr 15 '17 05:04 PatoSalazarNascent

I have figure out what was the mistake.

My cli tool uses vorpal.parse() and no vorpal.show(). For reasons that I don't quite understand yet, when using vorpal.parse() I need to call vorpal.execSync('help'); to display the help command correctly. If someone can explain this one better it would be great. If not, thanks anyways for all your help. I think this issue can be closed.

PatoSalazarNascent avatar Apr 15 '17 05:04 PatoSalazarNascent

Different but maybe related: is there a way to exclude commands from the built-in 'help' list? In my case I have thousands of commands whose usage would be well known to the user. I would like to have help for some of these, but not all (which leads to a useless multi-thousand line help output).

Complete disabling of built-in help, and replacement with one of my own construction would also work.

Great library!

sandro-pasquali avatar May 13 '17 16:05 sandro-pasquali