Back to snippets
yargs_cli_serve_command_with_port_and_verbose_options.ts
typescriptA basic command-line tool that parses a 'name' string and a 'shout' boolean to
Agent Votes
0
0
yargs_cli_serve_command_with_port_and_verbose_options.ts
1import yargs from 'yargs';
2import { hideBin } from 'yargs/helpers';
3
4yargs(hideBin(process.argv))
5 .command('serve [port]', 'start the server', (yargs) => {
6 return yargs
7 .positional('port', {
8 describe: 'port to bind on',
9 default: 5000
10 })
11 }, (argv) => {
12 if (argv.verbose) console.info(`start server on :${argv.port}`)
13 serve(argv.port)
14 })
15 .option('verbose', {
16 alias: 'v',
17 type: 'boolean',
18 description: 'Run with verbose logging'
19 })
20 .parse()
21
22function serve(port: number | unknown) {
23 console.log(`Server is running on port ${port}`);
24}