Skip to content

Commit 99914b6

Browse files
committed
fix: remove the first arg for sub-command
1 parent b51adbe commit 99914b6

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/Command.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ export default class Command {
199199
})
200200
.join('\n\n')
201201
)
202+
203+
process.exit(0)
202204
}
203205

204206
outputVersion(bin: string) {
@@ -208,8 +210,8 @@ export default class Command {
208210
process.arch
209211
} node-${process.version}`
210212
)
213+
process.exit(0)
211214
}
212-
return this
213215
}
214216

215217
/**

src/index.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class CAC extends EventEmitter {
115115
this.rawArgs = argv
116116
this.bin = argv[1] ? path.basename(argv[1]) : 'cli'
117117

118+
// Search sub-commands
118119
for (const command of this.commands) {
119120
const minimistOptions = getMinimistOptions([
120121
...this.globalCommand.options,
@@ -124,21 +125,22 @@ class CAC extends EventEmitter {
124125
argv.slice(2),
125126
minimistOptions
126127
)
127-
if (command.isMatched(args[0])) {
128+
const commandName = args[0]
129+
if (command.isMatched(commandName)) {
128130
this.matchedCommand = command
129-
this.args = args
131+
this.args = args.slice(1)
130132
this.options = options
131-
this.emit(`command:${args[0]}`, command)
133+
this.emit(`command:${commandName}`, command)
132134
this.runCommandAction(command, this.globalCommand, {
133-
args,
135+
args: this.args,
134136
options,
135137
originalOptions
136138
})
137-
return { args, options }
139+
return { args: this.args, options }
138140
}
139141
}
140142

141-
// Try the default command
143+
// Search the default command
142144
for (const command of this.commands) {
143145
if (command.name === '') {
144146
const minimistOptions = getMinimistOptions([
@@ -233,16 +235,11 @@ class CAC extends EventEmitter {
233235

234236
command.checkUnknownOptions(originalOptions, globalCommand)
235237

236-
// The first one is command name
237-
if (!command.isDefaultCommand) {
238-
args = args.slice(1)
239-
}
240-
241238
const minimalArgsCount = command.args.filter(arg => arg.required).length
242239

243-
if (args.length < minimalArgsCount) {
240+
if (command.args.length < minimalArgsCount) {
244241
console.error(
245-
`error: missing required args for command "${command.rawName}"`
242+
`error: missing required args for command \`${command.rawName}\``
246243
)
247244
process.exit(1)
248245
}

0 commit comments

Comments
 (0)