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

Make command-argument array public (in readonly mode) #1970

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions docs/deprecated.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ They are currently still available for backwards compatibility, but should not b
- [InvalidOptionArgumentError](#invalidoptionargumenterror)
- [Short option flag longer than a single character](#short-option-flag-longer-than-a-single-character)
- [Import from `commander/esm.mjs`](#import-from-commanderesmmjs)
- [.\_args](#_args)

## RegExp .option() parameter

Expand Down Expand Up @@ -207,3 +208,17 @@ import { Command } from 'commander';
```

README updated in Commander v9. Deprecated from Commander v9.

## ._args
shadowspawn marked this conversation as resolved.
Show resolved Hide resolved

The registered command argument instances have been made accessible via `.registeredArguments`.

```js
const program = new Command().arguments('arg1 <arg2> [arg3]');
const argumentNamesForUsage = program.registeredArguments
.map(arg => arg.required ? `< ${arg.name()} >` : `[ ${arg.name()} ]`)
.join(' ');
program.usage('program ' + argumentNamesForUsage);
```

The older name of the property was `_args`. It was never documented but is still available so that old code relying on it keeps working. Do not use it.
1 change: 1 addition & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Command extends EventEmitter {
this._allowExcessArguments = true;
/** @type {Argument[]} */
this.registeredArguments = [];
this._args = this.registeredArguments; // deprecated
/** @type {string[]} */
this.args = []; // cli args with options removed
this.rawArgs = [];
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ export class Command {
readonly commands: readonly Command[];
readonly options: readonly Option[];
readonly registeredArguments: readonly Argument[];
/** @deprecated */
readonly _args: readonly Argument[]; // added here for strikethrough highlighting in editors
shadowspawn marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
readonly _args: readonly Argument[]; // added here for strikethrough highlighting in editors
readonly _args: readonly Argument[];

parent: Command | null;

constructor(name?: string);
Expand Down