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

Handling top level command #141

Open
KidkArolis opened this issue Jun 19, 2019 · 5 comments
Open

Handling top level command #141

KidkArolis opened this issue Jun 19, 2019 · 5 comments

Comments

@KidkArolis
Copy link

Maybe I'm missing something, but how do I handle the top level command?

args
  .command('a', 'Import data', a)
  .command('b', 'Export data', b)

This will run a or b if I run ./cmd a or ./cmd b.

But if I run ./cmd, it's hanging (cause my script opens db connection), how do I
a) print help or
b) execute some code?

@KidkArolis
Copy link
Author

KidkArolis commented Jun 19, 2019

OK, if I remove the db connection, it prints help, I'll shuffle my code around.

@KidkArolis
Copy link
Author

No it doesn't actually.

If I just run bin/import without any other arguments, the program just exists.
Didn't see any docs on how to either print help or execute some code in this case.

Had to resort with wrapping my command callbacks with a wrapper function that sets a boolean executedCommand = true which seems suboptimal.

@KidkArolis KidkArolis reopened this Jun 20, 2019
@ntwcklng
Copy link
Collaborator

Hey @KidkArolis you can show the Help on your main command (with no arguments) with this function: .showHelp(). It is listed in the Docs:
Bildschirmfoto 2019-06-29 um 12 20 01

Does this fix your problem?

@KidkArolis
Copy link
Author

KidkArolis commented Jun 29, 2019 via email

@KidkArolis
Copy link
Author

Here's what I mean, taking the example from README (slightly modified):

#!/usr/bin/env node

const args = require('args')

args
  .option('port', 'The port on which the app will be running', 3000)
  .option('reload', 'Enable/disable livereloading')
  .command('serve', 'Serve your static site', serve, ['s'])

const flags = args.parse(process.argv)

function serve () {
  console.log('serving...')
}

This works fine:

$ ./hello serve
serving...
$ 

But how do I make sure that the following executions print help:

$ ./hello badcommand
$
$ ./hello
$

I'm thinking.. perhaps loop over args.details.commands to match sub against each command..

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