added tests + renamed default to isDefault for clarity

This commit is contained in:
Chen Asraf
2021-07-20 00:02:52 +03:00
parent c0c204f87d
commit a728370e80
4 changed files with 54 additions and 18 deletions

View File

@@ -165,7 +165,7 @@ Any arguments that are not taken by options or commands, are automatically passe
| `aliases` | `string[]` | | | `["n"]` | Alternate names for the option, available for use in addition to `name` |
| `description` | `string` | | | `"Description of the command"` | Description for the command, only displayed with `--help` or `printHelp()` |
| `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. |
| `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). |
| `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). |
| `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` |
| `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. |
| `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. |

View File

@@ -265,7 +265,7 @@ export class Massarg<Options extends OptionsBase = OptionsBase> {
}
if (!option && !command) {
const defOpts = this._options.filter((o) => o.default)
const defOpts = this._options.filter((o) => o.isDefault)
if (defOpts.length) {
for (const option of defOpts) {
this._addOptionToData(option, option.parse!(arg, this.data))

2
src/options.d.ts vendored
View File

@@ -19,7 +19,7 @@ export interface OptionDef<Options, Value> {
* 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).
* When more than one option has this turned on, they will all be given these values. Use carefully. */
default?: boolean
isDefault?: boolean
/**
* In addition to primary name, you may also define aliases. Aliases when used in CLI should only be prefixed with

View File

@@ -88,22 +88,58 @@ describe("Options", () => {
).toThrow("Missing value for: number")
})
test("should parse default", () => {
const options = massarg()
.option({
name: "number",
default: true,
parse: (v) => parseInt(v),
})
.option({
name: "bool",
default: true,
boolean: true,
})
.parseArgs(["1"])
describe("isDefault", () => {
test("should parse default with one default", () => {
const options = massarg()
.option({
name: "number",
isDefault: true,
parse: (v) => parseInt(v),
})
.option({
name: "bool",
isDefault: false,
boolean: true,
})
.parseArgs(["1"])
expect(options).toHaveProperty("number", 1)
expect(options).toHaveProperty("bool", true)
expect(options).toHaveProperty("number", 1)
expect(options).not.toHaveProperty("bool", true)
})
test("should parse default with multiple defaults", () => {
const options = massarg()
.option({
name: "number",
isDefault: true,
parse: (v) => parseInt(v),
})
.option({
name: "bool",
isDefault: true,
boolean: true,
})
.parseArgs(["1"])
expect(options).toHaveProperty("number", 1)
expect(options).toHaveProperty("bool", true)
})
test("should parse default with command", () => {
const options = massarg()
.command({
name: "cmd",
run: () => void 0,
})
.option({
name: "number",
isDefault: true,
parse: (v) => parseInt(v),
})
.parseArgs(["cmd", "1"])
expect(options).toHaveProperty("number", 1)
})
})
describe("required", () => {