Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Add Yargs default and middleware logic to display helpful message during invalid arg/command calls #262

Closed
wants to merge 7 commits into from
6 changes: 6 additions & 0 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ const clean = {
handler: exitOnError(main.clean)
};

const defaultCommand = {
command: '$0',
handler: exitOnError(main.defaultCommand)
};

let config = require('../get-config')();
yargs // eslint-disable-line
.middleware(require('../utils/check-version'))
Expand Down Expand Up @@ -191,6 +196,7 @@ yargs // eslint-disable-line
.command(login)
.command(require('../commands/repl'))
.command(require('../commands/generate-key'))
.command(defaultCommand)
.config(config)
.alias({
'accountId': ['account_id'],
Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,19 @@ exports.stake = async function(options) {
const result = await account.stake(options.stakingKey, utils.format.parseNearAmount(options.amount));
console.log(inspectResponse(result));
};

exports.defaultCommand = async function(options) {
let unclearPiece = `'${options._.join(', ')}'`;
switch (options._.length) {
case 0:
unclearPiece = 'argument(s)';
break;
case 1:
unclearPiece = `'${options._}'`;
break;
default:
// use initialized value
break;
}
console.warn(`Sorry, I do not understand ${unclearPiece}\nPlease run 'near' to see the list of available commands.`);
};