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

Add missing support for command-arguments to commands added with .addCommand() #1941

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
17 changes: 11 additions & 6 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ class Command extends EventEmitter {

constructor(name) {
super();
const nameMatchResult = name?.match(/([^ ]+) *(.*)/);
/** @type {string} */
this._name = nameMatchResult?.[1] ?? '';
/** @type {Argument[]} */
this._args = [];
if (nameMatchResult?.[2]) {
this.arguments(nameMatchResult[2]);
}

/** @type {Command[]} */
this.commands = [];
/** @type {Option[]} */
this.options = [];
this.parent = null;
this._allowUnknownOption = false;
this._allowExcessArguments = true;
/** @type {Argument[]} */
this._args = [];
/** @type {string[]} */
this.args = []; // cli args with options removed
this.rawArgs = [];
this.processedArgs = []; // like .args but after custom processing and collecting variadic
this._scriptPath = null;
this._name = name || '';
this._optionValues = {};
this._optionValueSources = {}; // default, env, cli etc
this._storeOptionsAsProperties = false;
Expand Down Expand Up @@ -142,17 +148,16 @@ class Command extends EventEmitter {
desc = null;
}
opts = opts || {};
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);

const cmd = this.createCommand(name);
const cmd = this.createCommand(nameAndArgs);
if (desc) {
cmd.description(desc);
cmd._executableHandler = true;
}
if (opts.isDefault) this._defaultCommandName = cmd._name;
cmd._hidden = !!(opts.noHelp || opts.hidden); // noHelp is deprecated old name for hidden
cmd._executableFile = opts.executableFile || null; // Custom name for executable file, set missing to null to match constructor
if (args) cmd.arguments(args);

this.commands.push(cmd);
cmd.parent = this;
cmd.copyInheritedSettings(this);
Expand Down