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

feat: add support for showVersion, similar to showHelp #1831

Merged
merged 8 commits into from Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
27 changes: 27 additions & 0 deletions docs/api.md
Expand Up @@ -1504,6 +1504,33 @@ yargs.showHelp(s => myStream.write(s)); //prints to myStream

Later on, `argv` can be retrieved with `yargs.argv`.

.showVersion([consoleLevel | printCallback])
---------------------------

Print the version data.

If no argument is provided, version data is printed using `console.error`.

```js
var yargs = require("yargs")
OsmanAltun marked this conversation as resolved.
Show resolved Hide resolved
.version('1.0.0');
yargs.showVersion(); //prints to stderr using console.error()
```

If a string is specified, version data is printed using the [`console`](https://nodejs.org/api/console.html) function `consoleLevel`.

```js
yargs.showVersion("log"); //prints to stdout using console.log()
```

If a function is specified, it is called with a single argument - the version data as a string.

```js
yargs.showVersion(s => myStream.write(s)); //prints to myStream
```

Later on, `argv` can be retrieved with `yargs.argv`.

.showHelpOnFail(enable, [message])
----------------------------------

Expand Down
10 changes: 6 additions & 4 deletions lib/usage.ts
Expand Up @@ -669,9 +669,11 @@ export function usage(yargs: YargsInstance, y18n: Y18N, shim: PlatformShim) {
version = ver;
};

self.showVersion = () => {
self.showVersion = level => {
const logger = yargs._getLoggerInstance();
logger.log(version);
if (!level) level = 'error';
const emit = typeof level === 'function' ? level : logger[level];
emit(version);
};

self.reset = function reset(localLookup) {
Expand Down Expand Up @@ -746,9 +748,9 @@ export interface UsageInstance {
getUsageDisabled(): boolean;
help(): string;
reset(localLookup: Dictionary<boolean>): UsageInstance;
showHelp(level: 'error' | 'log' | ((message: string) => void)): void;
showHelp(level?: 'error' | 'log' | ((message: string) => void)): void;
showHelpOnFail(enabled?: boolean | string, message?: string): UsageInstance;
showVersion(): void;
showVersion(level?: 'error' | 'log' | ((message: string) => void)): void;
stringifiedValues(values?: any[], separator?: string): string;
unfreeze(): void;
usage(msg: string | null, description?: string | false): UsageInstance;
Expand Down
11 changes: 10 additions & 1 deletion lib/yargs-factory.ts
Expand Up @@ -1254,6 +1254,12 @@ function Yargs(
return self;
};

self.showVersion = function (level) {
argsert('[string|function]', [level], arguments.length);
usage.showVersion(level);
return self;
};

let versionOpt: string | null = null;
self.version = function version(
opt?: string | false,
Expand Down Expand Up @@ -1667,7 +1673,7 @@ function Yargs(
if (exitProcess) setBlocking(true);

skipValidation = true;
usage.showVersion();
usage.showVersion('log');
self.exit(0);
}
});
Expand Down Expand Up @@ -2072,6 +2078,9 @@ export interface YargsInstance {
scriptName(scriptName: string): YargsInstance;
showCompletionScript($0?: string, cmd?: string): YargsInstance;
showHelp(level: 'error' | 'log' | ((message: string) => void)): YargsInstance;
showVersion(
level: 'error' | 'log' | ((message: string) => void)
): YargsInstance;
showHelpOnFail: {
(message?: string): YargsInstance;
(enabled: boolean, message: string): YargsInstance;
Expand Down
55 changes: 55 additions & 0 deletions test/usage.cjs
Expand Up @@ -3198,6 +3198,61 @@ describe('usage tests', () => {
});
});

describe('showVersion', () => {
// see #143.
it('should show version regardless of whether argv has been called', () => {
OsmanAltun marked this conversation as resolved.
Show resolved Hide resolved
const r = checkUsage(() => {
const y = yargs()
.version('1.0.0')
.wrap(null);

y.showVersion();
});

r.errors
.join('\n')
.split(/\n+/)
.should.deep.equal([
'1.0.0'
]);
});

it('should call the correct console.log method when specified', () => {
const r = checkUsage(() => {
const y = yargs()
.version('1.0.0')
.wrap(null);

y.showVersion('log');
});

r.errors.length.should.eql(0);
r.logs
.join('\n')
.split(/\n+/)
.should.deep.equal([
'1.0.0'
]);
});

it('should call the callback to print when specified', done => {
const y = yargs()
.version('1.0.0')
.wrap(null);

y.showVersion(printCallback);

function printCallback(msg) {
msg
.split(/\n+/)
.should.deep.equal([
'1.0.0'
]);
return done();
}
});
});

describe('$0', () => {
function mockProcessArgv(argv, cb) {
const argvOld = process.argv;
Expand Down