dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

Print help programmatically #223

Open scarabdesign opened 7 years ago

scarabdesign commented 7 years ago

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

PatoSalazarNascent commented 7 years ago

Same request here

postatum commented 7 years ago

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

PatoSalazarNascent commented 7 years ago

it does..thanks!

scarabdesign commented 7 years ago

Yes! Thank you, that will work great.

PatoSalazarNascent commented 7 years ago

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;
  })
scarabdesign commented 7 years ago

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?

postatum commented 7 years ago

@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
PatoSalazarNascent commented 7 years ago

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(...)
postatum commented 7 years ago

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.

scarabdesign commented 7 years ago

@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(...);
postatum commented 7 years ago

@scarabdesign right. That's what I meant

PatoSalazarNascent commented 7 years ago

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 commented 7 years ago

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.

sandro-pasquali commented 7 years ago

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!