Skip to content

Commit a728370

Browse files
committed
added tests + renamed default to isDefault for clarity
1 parent c0c204f commit a728370

4 files changed

Lines changed: 54 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Any arguments that are not taken by options or commands, are automatically passe
165165
| `aliases` | `string[]` | | | `["n"]` | Alternate names for the option, available for use in addition to `name` |
166166
| `description` | `string` | | | `"Description of the command"` | Description for the command, only displayed with `--help` or `printHelp()` |
167167
| `parse` | `function(value, options) => any` | | `String` | `(value, options) => parseInt(value)` | Function that parses this option. The supplied arguments are the string value from the arg, and other options passed via the CLI and parsed by massarg before this one. Not all options will be available. |
168-
| `default` | `boolean` | | `false` | | When `true`, any args placed without name will be applied to this option. When more than one arg is supplied this way, only the last given will be used (unless the option is an array type). |
168+
| `isDefault` | `boolean` | | `false` | | When `true`, any args placed without name will be applied to this option. When more than one arg is supplied this way, only the last given will be used (unless the option is an array type). |
169169
| `boolean` | `boolean` | | `false` | | When set to `true`, this option will be treated as a boolean: will accept no value as `true`, or other truthy values as `true`, and the rest as `false` |
170170
| `array` | `boolean` | | `false` | | When set to true, you will be able to take multiple values when using the same option more than once. They will all be parsed properly and put into an array. |
171171
| `required` | `boolean` | | `false` | | When an option is required, parsing will throw a `RequiredError` if it was not given a proper value. If it is attached to a specific (or several) commands, it will only throw if the relevant command was used. |

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
265265
}
266266

267267
if (!option && !command) {
268-
const defOpts = this._options.filter((o) => o.default)
268+
const defOpts = this._options.filter((o) => o.isDefault)
269269
if (defOpts.length) {
270270
for (const option of defOpts) {
271271
this._addOptionToData(option, option.parse!(arg, this.data))

src/options.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface OptionDef<Options, Value> {
1919
* When `true`, any args placed without name will be applied to this option. When more than one arg is supplied
2020
* this way, only the last given will be used (unless the option is an array type).
2121
* When more than one option has this turned on, they will all be given these values. Use carefully. */
22-
default?: boolean
22+
isDefault?: boolean
2323

2424
/**
2525
* In addition to primary name, you may also define aliases. Aliases when used in CLI should only be prefixed with

tests/options.test.ts

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,58 @@ describe("Options", () => {
8888
).toThrow("Missing value for: number")
8989
})
9090

91-
test("should parse default", () => {
92-
const options = massarg()
93-
.option({
94-
name: "number",
95-
default: true,
96-
parse: (v) => parseInt(v),
97-
})
98-
.option({
99-
name: "bool",
100-
default: true,
101-
boolean: true,
102-
})
103-
.parseArgs(["1"])
91+
describe("isDefault", () => {
92+
test("should parse default with one default", () => {
93+
const options = massarg()
94+
.option({
95+
name: "number",
96+
isDefault: true,
97+
parse: (v) => parseInt(v),
98+
})
99+
.option({
100+
name: "bool",
101+
isDefault: false,
102+
boolean: true,
103+
})
104+
.parseArgs(["1"])
105+
106+
expect(options).toHaveProperty("number", 1)
107+
expect(options).not.toHaveProperty("bool", true)
108+
})
104109

105-
expect(options).toHaveProperty("number", 1)
106-
expect(options).toHaveProperty("bool", true)
110+
test("should parse default with multiple defaults", () => {
111+
const options = massarg()
112+
.option({
113+
name: "number",
114+
isDefault: true,
115+
parse: (v) => parseInt(v),
116+
})
117+
.option({
118+
name: "bool",
119+
isDefault: true,
120+
boolean: true,
121+
})
122+
.parseArgs(["1"])
123+
124+
expect(options).toHaveProperty("number", 1)
125+
expect(options).toHaveProperty("bool", true)
126+
})
127+
128+
test("should parse default with command", () => {
129+
const options = massarg()
130+
.command({
131+
name: "cmd",
132+
run: () => void 0,
133+
})
134+
.option({
135+
name: "number",
136+
isDefault: true,
137+
parse: (v) => parseInt(v),
138+
})
139+
.parseArgs(["cmd", "1"])
140+
141+
expect(options).toHaveProperty("number", 1)
142+
})
107143
})
108144

109145
describe("required", () => {

0 commit comments

Comments
 (0)