Skip to content

Commit

Permalink
Update example using option as boolean flag and also having it accept…
Browse files Browse the repository at this point in the history
… operands
  • Loading branch information
heyjiawei committed Aug 28, 2020
1 parent c686ee3 commit 3e29566
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions optional-options-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,39 @@ There are potential challenges using options with optional values. They seem qui

## Parsing ambiguity

There is parsing ambiguity when using option as boolean flag and also having it accept operands(sometimes called a positional argument or command argument, referring to `Command.arguments()`) and subcommands.
There is parsing ambiguity when using option as boolean flag and also having it accept operands (sometimes called a positional argument or command argument, referring to `Command.arguments()`) and subcommands.

```
program.command('example')
.option("-o, --option [optionalValue]")
.command('brew')
.action(() => {
console.log("example brew");
})
program.parse(process.argv);
program
.arguments("[technique]")
.option("-i, --ingredient [ingredient]")
.action((args, cmdObj) => {
console.log(args);
console.log(cmdObj.opts());
});
if (program.option) console.log(`option: ${program.option}`);
program.parse();
```

```
$ example -o
option: true
$ example -o thisValueIsPassedToOption
option: thisValueIsPassedToOption
$ example -o brew
option: brew
$ example -o nextArg
option: nextArg
$ cook scrambled
scrambled
{ ingredient: undefined }
$ cook -i
undefined
{ ingredient: true }
$ cook -i egg
undefined
{ ingredient: egg }
$ cook -i scrambled
undefined
{ ingredient: scrambled }
```

For example, you may intend `brew` to be passed as a subcommand. Instead, it will be read as the passed in value for `-o`. Likewise, you may intend `nextArg` to be passed as an argument but it too, will be read as the passed in value for `-o`
For example, you may intend `scrambled` to be passed as a non-option argument. Instead, it will be read as the passed in value for ingredient.

### Possible workarounds

Expand Down

0 comments on commit 3e29566

Please sign in to comment.