From 40f885e7c4cced90c9b4bb2472783740858a0cc2 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 23 Feb 2021 12:54:35 +0200 Subject: [PATCH] feature(cloudcmd) add ability to suggest option, when wrong provided --- bin/cloudcmd.mjs | 53 +++++++++++++++++++++++++++++++++++------------- package.json | 5 +++-- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/bin/cloudcmd.mjs b/bin/cloudcmd.mjs index 74824584..5971faba 100755 --- a/bin/cloudcmd.mjs +++ b/bin/cloudcmd.mjs @@ -6,6 +6,7 @@ import {createRequire} from 'module'; import {promisify} from 'util'; import tryToCatch from 'try-to-catch'; import {createSimport} from 'simport'; +import parse from 'yargs-parser'; import exit from '../server/exit.js'; import { @@ -34,8 +35,21 @@ const require = createRequire(import.meta.url); const Info = require('../package.json'); -const {argv} = process; -const args = require('minimist')(argv.slice(2), { +const maybeRoot = (a) => { + if (a === '.') + return process.cwd(); + + return a; +}; + +const yargsOptions = { + configuration: { + 'strip-aliased': true, + 'strip-dashed': true, + }, + coerce: { + root: maybeRoot, + }, string: [ 'name', 'port', @@ -81,6 +95,7 @@ const args = require('minimist')(argv.slice(2), { 'import', 'import-listen', 'log', + 'zip', 'dropbox', ], default: { @@ -127,19 +142,19 @@ const args = require('minimist')(argv.slice(2), { 'dropbox-token': config('dropboxToken'), }, alias: { - v: 'version', - h: 'help', - p: 'password', - o: 'online', - u: 'username', - s: 'save', - a: 'auth', - c: 'config', + version: 'v', + help: 'h', + password: 'p', + online: 'o', + username: 'u', + save: 's', + auth: 'a', + config: 'c', }, - unknown: (cmd) => { - exit('\'%s\' is not a cloudcmd option. See \'cloudcmd --help\'.', cmd); - }, -}); +}; + +const {argv} = process; +const args = parse(argv.slice(2), yargsOptions); if (args.version) version(); @@ -149,6 +164,16 @@ else main(); async function main() { + const validateArgs = await simport('@putout/cli-validate-args'); + + const error = await validateArgs(args, [ + ...yargsOptions.boolean, + ...yargsOptions.string, + ]); + + if (error) + return exit(error); + if (args.repl) repl(); diff --git a/package.json b/package.json index 0b4c58b3..6d709ad7 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,7 @@ "@cloudcmd/fileop": "^5.0.0", "@cloudcmd/move-files": "^4.0.0", "@cloudcmd/read-files-sync": "^2.0.0", + "@putout/cli-validate-args": "^1.0.1", "@putout/plugin-cloudcmd": "^1.2.0", "apart": "^2.0.0", "chalk": "^4.0.0", @@ -111,7 +112,6 @@ "just-snake-case": "^1.1.0", "markdown-it": "^12.0.0", "mellow": "^3.0.0", - "minimist": "^1.2.0", "nomine": "^4.0.0", "object.omit": "^3.0.0", "once": "^1.4.0", @@ -137,7 +137,8 @@ "tryrequire": "^3.0.0", "win32": "^5.1.11", "wraptile": "^3.0.0", - "writejson": "^3.0.0" + "writejson": "^3.0.0", + "yargs-parser": "^20.2.5" }, "devDependencies": { "@babel/code-frame": "^7.5.5",