-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Create helper function to parse global options #1955
Conversation
i propose to do something like this let getIPFS = null
cli
.commandDir('commands')
.middleware(argv => {
getIPFS = argv.getIpfs = utils.singleton(cb => utils.getIPFS(argv, cb))
return argv
})
.help()
.strict()
.completion()
let exitCode = 0
try {
const { data } = await new YargsPromise(cli).parse(args)
if (data) print(data)
} catch (err) {
debug(err)
// the argument can have a different shape depending on where the error came from
if (err.message || (err.error && err.error.message)) {
print(err.message || err.error.message)
} else {
print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output')
}
exitCode = 1
} finally {
// If an IPFS instance was used in the handler then clean it up here
if (getIPFS.instance) {
try {
const cleanup = getIPFS.rest[0]
await cleanup()
} catch (err) {
debug(err)
exitCode = 1
}
}
}
if (exitCode) {
process.exit(exitCode)
}
} this way we use only one yargs instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test please?
I'd like so but I'm not sure yet about how to handle it. Is there any test utility/method that exposes the instantiated IPFS object produced by cmds? |
to do this we need to change the way we test the cli check the example below |
i improved the example to further simplify this code, can you have a look? can you also make one more test with a real cmd like thank you so much for your contribution 👍 |
No description provided.