Skip to content

Commit

Permalink
feat: add support for showVersion, similar to showHelp (#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
OsmanAltun committed Mar 14, 2021
1 parent 8b95f57 commit 1a1e2d5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
27 changes: 27 additions & 0 deletions docs/api.md
Expand Up @@ -1522,6 +1522,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/yargs')(process.argv.slice(2));
yargs.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 @@ -674,9 +674,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 @@ -754,9 +756,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 @@ -1346,6 +1346,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 @@ -1759,7 +1765,7 @@ function Yargs(
if (exitProcess) setBlocking(true);

skipValidation = true;
usage.showVersion();
usage.showVersion('log');
self.exit(0);
}
});
Expand Down Expand Up @@ -2175,6 +2181,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
35 changes: 35 additions & 0 deletions test/usage.cjs
Expand Up @@ -3198,6 +3198,41 @@ describe('usage tests', () => {
});
});

describe('showVersion', () => {
// see #143.
it('should show version regardless of whether argv has been called', () => {
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

0 comments on commit 1a1e2d5

Please sign in to comment.