#1858·yargs

Can't set default value for nested options.

Author: UchihaYukiCreated Feb 1, 2021Updated Aug 2, 2026
Labelsfeature request

The following code doesn't work:

javascript
const argv = yargs(process.argv.slice(2))
    .options({
        listen: {
            port: {
                default: 80,
            },
            hostname: {
                default: "0.0.0.0",
            },
        }
    }).argv;

argv.listen.port and argv.listen.hostname is undefined. If I use the following syntax:

javascript
const argv = yargs(process.argv.slice(2))
    .options({
        "listen.port": {
            default: 80,
        },
        "listen.hostname": {
            default: "0.0.0.0",
        },
    }).argv;

Althought it works in non-strict, but in yargs.strict(), it will say:

Unknown argument: listen

I have to write it like the following to make it work:

javascript
const argv = yargs(process.argv.slice(2))
    .options({
        listen: {},
        "listen.port": {
            default: 80,
        },
        "listen.hostname": {
            default: "0.0.0.0",
        },
    })
    .strict().argv;