I am trying to create a CLI utility with Yargs but the types of argv in my command handlers are unknown. I installed @types/yargs as well but still cannot resolve the types. Any help would be greatly appreciated. I tried extending Yarg's ArgumentsCamelCase, but it does not allow me to use the type in the function declaration.
#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
yargs(hideBin(process.argv));
(async () => {
await yargs
.scriptName('')
.alias('v', 'version')
.alias('h', 'help')
.option('global', {
alias: 'g',
describe: 'perform globally',
type: 'boolean',
})
.command({
command: 'register [options]',
describe: 'register',
builder: {
global: {
alias: 'g',
type: 'boolean',
description: 'register globally',
default: false,
},
},
handler: async (argv) => {
const { global } = argv //global is unknown type;
// ...
},
})
.command(
'delete <id> [options]',
'delete command with id',
(yargs) => {
yargs.option('id', {
describe: 'The id',
type: 'string',
});
yargs.option('global', {
alias: 'g',
type: 'boolean',
description: 'delete globally',
default: false,
});
},
(argv) => {
const {id, global} = argv; //both unknown
// ...
}
)
.strict()
.parseSync();
})();