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

#187 Change boolean option parsing #188

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
70 changes: 70 additions & 0 deletions src/command/__tests__/command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,76 @@ describe("Command", () => {
)
})

describe("with boolean option before arguments", () => {
it("command should be able to handle variadic arguments with boolean option alias", async () => {
const action = jest.fn().mockReturnValue("OK!")
prog
.command("order", "Order something")
.argument("<types...>", "Pizza types")
.option("-v, --verbose", "Detailed log")
.action(action)
const args = ["order", "-v", "pepperoni", "regina"]
await prog.run(args)
expect(action).toHaveBeenCalledWith(
expect.objectContaining({
args: { types: ["pepperoni", "regina"] },
options: { v: true, verbose: true },
}),
)
})

it("command should be able to handle variadic arguments with boolean option", async () => {
const action = jest.fn().mockReturnValue("OK!")
prog
.command("order", "Order something")
.argument("<types...>", "Pizza types")
.option("-v, --verbose", "Detailed log")
.action(action)
const args = ["order", "--verbose", "pepperoni", "regina"]
await prog.run(args)
expect(action).toHaveBeenCalledWith(
expect.objectContaining({
args: { types: ["pepperoni", "regina"] },
options: { v: true, verbose: true },
}),
)
})

it("command should be able to handle variadic arguments with boolean option and explicit value", async () => {
const action = jest.fn().mockReturnValue("OK!")
prog
.command("order", "Order something")
.argument("<types...>", "Pizza types")
.option("-v, --verbose", "Detailed log")
.action(action)
const args = ["order", "--verbose=true", "pepperoni", "regina"]
await prog.run(args)
expect(action).toHaveBeenCalledWith(
expect.objectContaining({
args: { types: ["pepperoni", "regina"] },
options: { v: true, verbose: true },
}),
)
})

it("command should be able to handle variadic arguments with negative boolean option", async () => {
const action = jest.fn().mockReturnValue("OK!")
prog
.command("order", "Order something")
.argument("<types...>", "Pizza types")
.option("-v, --verbose", "Detailed log")
.action(action)
const args = ["order", "--no-verbose", "pepperoni", "regina"]
await prog.run(args)
expect(action).toHaveBeenCalledWith(
expect.objectContaining({
args: { types: ["pepperoni", "regina"] },
options: { v: false, verbose: false },
}),
)
})
})

it("command should check arguments range (variable)", async () => {
const action = jest.fn().mockReturnValue("hey!")
const cmd = prog
Expand Down
72 changes: 59 additions & 13 deletions src/parser/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,66 @@ describe("Parser", () => {
expect(result.args).toEqual(["my-arg1", "my-arg2"])
})

it("should handle 'boolean' option", () => {
const line =
"--my-opt true --my-bool 1 --my-false=0 --another=yes --not-now=no -y=yes --not-forced=yes"
const result = parseLine(line, {
boolean: ["myOpt", "myBool", "myFalse", "another", "notNow", "y"],
describe("boolean options", () => {
it("should handle 'boolean' option", () => {
const line =
"--my-opt --my-bool --my-false=0 --another=yes --not-now=no -y=yes --not-forced=yes --no-negative"
const result = parseLine(line, {
boolean: ["myOpt", "myBool", "myFalse", "another", "notNow", "y", "negative"],
})
expect(result.options).toEqual({
myOpt: true,
myBool: true,
myFalse: false,
another: true,
notNow: false,
y: true,
notForced: "yes",
negative: false,
})
})
expect(result.options).toEqual({
myOpt: true,
myBool: true,
myFalse: false,
another: true,
notNow: false,
y: true,
notForced: "yes",

it("should handle flag followed by arguments", async () => {
const line = "-v arg1 arg2"
const result = parseLine(line, {
boolean: ["v"],
})
expect(result.options).toEqual({
v: true,
})
})

it("should handle long option followed by arguments", async () => {
const line = "--verbose arg1 arg2"
const result = parseLine(line, {
boolean: ["verbose"],
})
expect(result.options).toEqual({
verbose: true,
})
})

it("should handle negative option followed by arguments", async () => {
const line = "--no-verbose arg1 arg2"
const result = parseLine(line, {
boolean: ["verbose"],
})
expect(result.options).toEqual({
verbose: false,
})
})

it("should handle negative option correcly in rawOptions", async () => {
const line = "--no-verbose"
const result = parseLine(line, {
boolean: ["verbose"],
})
expect(result.options).toEqual({
verbose: false,
})
expect(result.rawOptions).toEqual({
"--no-verbose": true,
})
})
})

Expand Down
20 changes: 12 additions & 8 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,13 @@ class OptionParser {

handleOptWithoutValue(name: string, tree: Tree): void {
const next = tree.next()
const nextIsOptOrUndef = isOptionStr(next) || isDdash(next) || next === undefined
const cleanName = formatOptName(name)
const shouldTakeNextAsVal = this.shouldTakeNextAsValue(cleanName, next)
this.compute(
name,
cast(name, nextIsOptOrUndef ? true : (next as string), this.config),
cast(name, shouldTakeNextAsVal ? (next as string) : true, this.config),
)
if (!nextIsOptOrUndef) {
if (shouldTakeNextAsVal) {
tree.forward()
}
}
Expand All @@ -265,17 +266,19 @@ class OptionParser {
val = true
const next = tree.next()
const last = names[names.length - 1]
const alias = this.config.alias[last]
const shouldTakeNextAsVal =
next && !isOptionStr(next) && !isDdash(next) && !this.isBoolean(last, alias)
if (shouldTakeNextAsVal) {
if (this.shouldTakeNextAsValue(last, next)) {
tree.forward()
val = next as string
}
}
this.computeMulti(names, val)
}

shouldTakeNextAsValue(cleaned: string, next: string | undefined): boolean {
const nextIsOptOrUndef = isOptionStr(next) || isDdash(next) || next === undefined
return !nextIsOptOrUndef && !this.isBoolean(cleaned, this.config.alias[cleaned])
}

visit(tree: Tree): boolean {
// only handle options
/* istanbul ignore if */
Expand Down Expand Up @@ -313,7 +316,8 @@ class OptionParser {
: [prop]
).concat(val)
} else {
this.rawOptions[name] = this.options[cleanName] = no ? !val : val
this.options[cleanName] = no ? !val : val
this.rawOptions[name] = val
}
if (alias) {
this.options[alias] = this.options[cleanName]
Expand Down