Skip to content
This repository has been archived by the owner on Jan 25, 2021. It is now read-only.

Latest commit

 

History

History
56 lines (43 loc) · 1.02 KB

setting-a-default-value-for-an-arg.md

File metadata and controls

56 lines (43 loc) · 1.02 KB

Default

Sometimes you may want an argument to be optional. In that case, you can use default.

Let's say you have a command that rolls a dice, it's args would probably looking something like this:

args: [
    {
        key: 'value',
        prompt: 'What is the maximum number you wish to appear?',
        type: 'integer'
    }
]

Let's say you wanted it to default to 6 if not set, like a real dice.

args: [
    {
        key: 'value',
        prompt: 'What is the maximum number you wish to appear?',
        type: 'integer',
        default: 6
    }
]

And then it will be 6 if no argument is set.

You can take advantage of this to create completely optional arguments, for example:

args: [
    {
        key: 'value',
        prompt: 'What is the maximum number you wish to appear?',
        type: 'integer',
        default: ''
    }
]

This sets the default to '', which is falsey. Then, in your run, you can handle that.

if (!value) {
// do this
} else {
// do something else
}