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

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help #1331

Merged
merged 45 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5a8c010
Add .addOption and use as bottleneck. Make all Option properties priv…
shadowspawn Aug 15, 2020
ac590a6
Desription for object is optional
shadowspawn Aug 15, 2020
77c8409
Add support for hidden options
shadowspawn Aug 15, 2020
69d084d
Try setFoo for booleans so less ambiguous what no parameter means
shadowspawn Aug 15, 2020
c1665e1
Renamed method
shadowspawn Aug 15, 2020
6da28cc
Restore Option property names to reduce churn
shadowspawn Aug 16, 2020
fcd73c7
Try more fluent names for non-property methods
shadowspawn Aug 16, 2020
fcc3d80
Avoid renaming existing members of Option
shadowspawn Aug 16, 2020
fb486f7
Add default description for help
shadowspawn Aug 16, 2020
f266bb7
Fix return JSDoc
shadowspawn Aug 16, 2020
d8a2f85
First cut at choices
shadowspawn Aug 16, 2020
9fa7ebf
Throw CommanderError for easier detection
shadowspawn Aug 16, 2020
5103430
Add catch for tidy coercion failure handing
shadowspawn Aug 16, 2020
fe3d543
Add tests for Option.choices
shadowspawn Aug 16, 2020
8e7804c
Rename custom option processing property
shadowspawn Aug 16, 2020
0a513cf
Add run script for TypeScript checkJS
shadowspawn Aug 16, 2020
8ccebe4
Add tests for chaining routines
shadowspawn Aug 17, 2020
408501b
More consistent name for custom option arg processing
shadowspawn Aug 17, 2020
12e81c2
Add choices to help
shadowspawn Aug 18, 2020
e3d997d
.default() now expects parameter
shadowspawn Aug 18, 2020
312321d
Fixed return type
shadowspawn Aug 18, 2020
d541cee
Separate out argumentRejected for possible reuse
shadowspawn Aug 18, 2020
c916a17
Add back support for RegExp which accidentally dropped.
shadowspawn Aug 20, 2020
9f92db7
Add test for obsolete regexp
shadowspawn Aug 20, 2020
b8c5ecc
Add TypeScript definitions for new Option properties
shadowspawn Aug 23, 2020
d7f0998
Switch from obsolete to deprecated, clearer meaning
shadowspawn Aug 24, 2020
3a32314
Fix left-over edit
shadowspawn Sep 4, 2020
91df291
Add comment
shadowspawn Sep 5, 2020
5205053
Simplify the comments
shadowspawn Sep 5, 2020
eb12b74
Add README and example file
shadowspawn Sep 5, 2020
35e854a
Remove example covered elsewhere
shadowspawn Sep 5, 2020
9e143e6
Restore example, leave change for a separate PR
shadowspawn Sep 5, 2020
0c3d916
Fix example output to match changed code
shadowspawn Sep 5, 2020
6966e71
Add language to code blocks
shadowspawn Sep 5, 2020
c79f904
Rename getFullDescription, not using get much
shadowspawn Sep 6, 2020
99da3cb
Merge branch 'release/7.x' into feature/fluent-options
shadowspawn Sep 9, 2020
8401ee6
Add chaining test for addHelpText
shadowspawn Sep 9, 2020
c36e08e
Describe as legacy rather than deprecated in comments, add @deprecate…
shadowspawn Sep 12, 2020
88a84f8
Do not have to have both should and long flags these days
shadowspawn Sep 12, 2020
fa09345
Eliminate duplicate code using internal knowledge
shadowspawn Sep 12, 2020
f96b36d
Rename parseArgWith to argParser
shadowspawn Sep 12, 2020
7a75f3d
Improve JSDoc for help
shadowspawn Sep 12, 2020
5e4ed3d
Make code and declarations consistent to pass tsc checks
shadowspawn Sep 12, 2020
dd5807a
Match up write signature to fix linting error
shadowspawn Sep 12, 2020
34c7289
Restore "deprecated", it is the right word
shadowspawn Sep 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
- [Common option types, boolean and value](#common-option-types-boolean-and-value)
- [Default option value](#default-option-value)
- [Other option types, negatable boolean and flag|value](#other-option-types-negatable-boolean-and-flagvalue)
- [Extra option features](#extra-option-features)
- [Custom option processing](#custom-option-processing)
- [Required option](#required-option)
- [Variadic option](#variadic-option)
Expand Down Expand Up @@ -202,6 +203,33 @@ $ pizza-options --cheese mozzarella
add cheese type mozzarella
```

### Extra option features

You can add most options using the `.option()` method, but there are some additional features available
by constructing an `Option` explicitly for less common cases.

Example file: [options-extra.js](./examples/options-extra.js)

```js
program
.addOption(new Option('-s, --secret').hideHelp())
.addOption(new Option('-t, --timeout <delay>', 'timeout in seconds').default(60, 'one minute'))
.addOption(new Option('-d, --drink <size>', 'drink size').choices(['small', 'medium', 'large']));
```

```bash
$ extra --help
Usage: help [options]

Options:
-t, --timeout <delay> timeout in seconds (default: one minute)
-d, --drink <size> drink cup size (choices: "small", "medium", "large")
-h, --help display help for command

$ extra --drink huge
error: option '-d, --drink <size>' argument of 'huge' not in allowed choices: small, medium, large
```

### Custom option processing

You may specify a function to do custom processing of option values. The callback function receives two parameters, the user specified value and the
Expand Down
20 changes: 20 additions & 0 deletions examples/options-extra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

// This is used as an example in the README for extra option features.

// const commander = require('commander'); // (normal include)
const commander = require('../'); // include commander in git clone of commander repo
const program = new commander.Command();

program
.addOption(new commander.Option('-s, --secret').hideHelp())
.addOption(new commander.Option('-t, --timeout <delay>', 'timeout in seconds').default(60, 'one minute'))
.addOption(new commander.Option('-d, --drink <size>', 'drink cup size').choices(['small', 'medium', 'large']));

program.parse();

console.log('Options: ', program.opts());

// Try the following:
// node options-extra.js --help
// node options-extra.js --drink huge
Loading