args icon indicating copy to clipboard operation
args copied to clipboard

Handling top level command

Open KidkArolis opened this issue 5 years ago • 5 comments

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 avatar Jun 19 '19 13:06 KidkArolis

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

KidkArolis avatar Jun 19 '19 13:06 KidkArolis

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 avatar Jun 20 '19 11:06 KidkArolis

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?

ntwcklng avatar Jun 29 '19 10:06 ntwcklng

No. I will post a reproducible example shortly.

On Sat, 29 Jun 2019 at 12:21, Marvin Mieth [email protected] wrote:

Hey @KidkArolis https://github.com/KidkArolis you can show the Help on your main command (with no arguments) with this function: .showHelp(). It is listed in the Docs: [image: Bildschirmfoto 2019-06-29 um 12 20 01] https://user-images.githubusercontent.com/8714775/60382775-40720000-9a68-11e9-8403-f3c5e09a3350.png

Does this fix your problem?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/leo/args/issues/141?email_source=notifications&email_token=AACPGWAD4LT6RR7WQA3M7YTP44ZRLA5CNFSM4HZJ4NO2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODY3WG6Y#issuecomment-506946427, or mute the thread https://github.com/notifications/unsubscribe-auth/AACPGWG2DUNWZRVPRVLTP53P44ZRLANCNFSM4HZJ4NOQ .

KidkArolis avatar Jun 29 '19 16:06 KidkArolis

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..

KidkArolis avatar Jun 30 '19 16:06 KidkArolis