Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to safe storage of options by default, and change action parameters #1409

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
64537e8
First cut at storing options safely by default
shadowspawn Dec 3, 2020
9ab4ad2
Pass command as options for legacy behaviour
shadowspawn Dec 3, 2020
d3ce415
Update examples for new patterns
shadowspawn Dec 4, 2020
5031893
Rework README on using storeOptionsAsProperties
shadowspawn Dec 4, 2020
b5f165f
Tweak wording
shadowspawn Dec 4, 2020
eefb4f9
Updat example for options-common
shadowspawn Dec 4, 2020
b0241d9
Update options-defaults
shadowspawn Dec 4, 2020
b7e5857
Update options-negatable example
shadowspawn Dec 4, 2020
ef34354
Update options-boolean-or-value example
shadowspawn Dec 4, 2020
de5d2b6
Update options-custom-processing example
shadowspawn Dec 4, 2020
ac58879
Merge branch 'release/7.x' into feature/safe-options-by-default
shadowspawn Dec 5, 2020
de31e1c
Turn on error for excess arguments, and update tests accordingly
shadowspawn Dec 5, 2020
0adf3c1
Add first cut at migration tips
shadowspawn Dec 5, 2020
0b0454a
Second cut at migration tips
shadowspawn Dec 5, 2020
03389c7
Migration wording tweaks
shadowspawn Dec 5, 2020
46f8a37
Reworking README for new patterns
shadowspawn Dec 5, 2020
4013a2d
Goodbye to option properties on Command
shadowspawn Dec 5, 2020
f6aa9c9
Add migration tip for excess arguments
shadowspawn Dec 6, 2020
6f4238b
Minor improvements to migrations tips
shadowspawn Dec 6, 2020
31990ee
Update examples for arguments
shadowspawn Dec 6, 2020
768974a
Update example for action handler
shadowspawn Dec 6, 2020
de93d9c
Add allowUnknownOption and allowExcessArguments to README
shadowspawn Dec 6, 2020
4de66a8
Update pizza example
shadowspawn Dec 6, 2020
d81d524
Merge branch 'release/7.x' into feature/safe-options-by-default
shadowspawn Dec 6, 2020
6727ccb
Reuse pizza as single command example. Tidy up pizza and deploy.
shadowspawn Dec 7, 2020
8132427
Word change
shadowspawn Dec 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 95 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,101 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
<!-- markdownlint-disable MD024 -->
<!-- markdownlint-disable MD004 -->

## [Unreleased] (date goes here)

### Migration Tips

The biggest change is the parsed option values. Previously the options were stored by default as properties on the command object, and now the options are stored separately.

If you wish to restore the old behaviour and get running quickly you can call `.storeOptionsAsProperties()`.
To allow you to move to the new code patterns incrementally, the action handler will be passed the command _twice_,
to match the new "options" and "command" parameters (see below).

**program options**

Use the `.opts()` method to access the options. This is available on any command but is used most with the program.

```js
program.option('-d, --debug');
program.parse();
// Old code before Commander 7
if (program.debug) console.log(`Program name is ${program.name()}`);
```

```js
// New code
const options = program.opts();
if (options.debug) console.log(`Program name is ${program.name()}`);
```

**action handler**

The action handler gets passed a parameter for each command-argument you declared. Previously by default the next parameter was the command object with the options as properties. Now the next two parameters are instead the options and the command. If you
only accessed the options there may be no code changes required.

```js
program
.command('compress <filename>')
.option('-t, --trace')
// Old code before Commander 7
.action((filename, cmd)) => {
if (cmd.trace) console.log(`Command name is ${cmd.name()}`);
});
```

```js
// New code
.action((filename, options, command)) => {
if (options.trace) console.log(`Command name is ${command.name()}`);
});
```

If you already set `.storeOptionsAsProperties(false)` you may still need to adjust your code.

```js
program
.command('compress <filename>')
.storeOptionsAsProperties(false)
.option('-t, --trace')
// Old code before Commander 7
.action((filename, command)) => {
if (command.opts().trace) console.log(`Command name is ${command.name()}`);
});
```

```js
// New code
.action((filename, options, command)) => {
if (command.opts().trace) console.log(`Command name is ${command.name()}`);
});
```

**excess command-arguments**

There is now an error if there are too many command-arguments on the command line (only checked if there is an action handler).
If the extra arguments are supported by your command then you can either declare the expected arguments, or allow excess arguments.

```js
// Old code before Commander 7
program
.action(() => {});
program.parse(['a', 'b', 'c'], { from: 'user' }); // now causes an error
```

```js
// New code, declare arguments
program
.arguments('[args...]')
.action(() => {});
```

```js
// New code, allow excess arguments
program
.allowExcessArguments()
.action(() => {});
```

## [7.0.0-1] (2020-11-21)

### Added
Expand Down