Now I don't have the titleOptions object created yet so the code would currently fail, but this is the general idea. We want to create the titleOptions object once and reference it in all the locations we use it, for add, read and remove command. I can take titleOptions, and add it for read as well as for add command, as shown here:
.command('add', 'Add a new note', {
title: titleOptions,
body: {
describe: 'Body of note',
demand: true,
alias: 'b'
}
})
.command('list', 'List all notes')
.command('read', 'Read a note', {
title: titleOptions
})
.command('remove', 'Remove a note', {
title: titleOptions
})
Now, just previous the constant argv, I can create a constant called titleOptions, and I can set it equal to that object that we defined for add and read command earlier, which is describe, demand, and alias, as shown here:
const titleOptions = {
describe: 'Title of note',
demand: true,
alias: 't'
};
We now have the titleOptions in place, and this will work as expected. We have the exact same functionality we did before, but we now have the titleOptions in a separate object, which follows the DRY principle we discussed in the Reading note section.
Now, we could also do the same thing for body. It might seem like overkill since we're only using it in only one location, but if we're sticking to the pattern of breaking them out into variables, I'll do it in the case of the body as well. Just following the titleOptions constant, I can create the constant bodyOptions, setting it equal to the options object we defined in the body, for add command in the previous subsection:
const bodyOptions = {
describe: 'Body of note',
demand: true,
alias: 'b'
};
With this in place, we are now done. We have add, read, and remove, all with their arguments set up referencing the titleObject and bodyObject variables defined.