Conflicts and Implications
This example illustrates how .conflicts() and .implies() can be used to enforce mutually exclusive options and mutually required options, respectively.
1 FILESTry in Playground
All Example Files
FILE EXPLORER
conflicts-and-implications.ts
1import cli from 'cli-forge';
2
3cli('conflicts-and-implications', {
4 builder: (args) =>
5 args
6 .option('source', {
7 describe: 'Source database',
8 type: 'string',
9 })
10 .option('target', {
11 describe: 'Target database',
12 type: 'string',
13 })
14 .option('dry-run', {
15 describe: 'Simulate the migration without making changes',
16 type: 'boolean',
17 })
18 .option('force', {
19 describe: 'Force the migration even if there are warnings',
20 type: 'boolean',
21 })
22 .option('backup', {
23 describe: 'Where should the backup be stored',
24 type: 'string',
25 })
26 // Conflicts creates mutually exclusive arguments. Validation will throw an error if both options are provided.
27 // In this case, it makes sense that the user wouldn't want to both simulate and force a migration.
28 .conflicts('dry-run', 'force')
29
30 // Implies creates mutually required arguments. Validation will throw an error if the first argument is provided without the second.
31 // Practically in this case, this means that if the user provides the --force option, they must also provide the --backup option.
32 .implies('force', 'backup'),
33 handler: (_args) => {
34 // ...
35 },
36}).forge();
37