diff --git a/bin/tldr b/bin/tldr index f0f5c74..d85507c 100755 --- a/bin/tldr +++ b/bin/tldr @@ -7,6 +7,8 @@ const config = require('../lib/config'); const platform = require('../lib/platform'); const { TldrError } = require('../lib/errors'); +const { Option } = program; + program .version(pkg.version, '-v, --version', 'Display version') .helpOption('-h, --help', 'Show this help message') @@ -20,15 +22,18 @@ program .option('-1, --single-column', 'List single command per line (use with options -l or -a)') .option('-r, --random', 'Show a random command') .option('-e, --random-example', 'Show a random example') - .option('-f, --render [file]', 'Render a specific markdown [file]') + .option('-f, --render ', 'Render a specific markdown [file]') .option('-m, --markdown', 'Output in markdown format') - .option('-o, --os [type]', 'Override the operating system [linux, osx, sunos, windows]') + .addOption( + new Option('-o, --os ', 'Override the operating system [linux, osx, sunos, windows]') + .choices(['linux', 'osx', 'sunos', 'windows']) + ) .option('--linux', 'Override the operating system with Linux') .option('--osx', 'Override the operating system with OSX') .option('--sunos', 'Override the operating system with SunOS') .option('--windows', 'Override the operating system with Windows') - .option('-t, --theme [theme]', 'Color theme (simple, base16, ocean)') - .option('-s, --search [keywords]', 'Search pages using keywords') + .option('-t, --theme ', 'Color theme (simple, base16, ocean)') + .option('-s, --search ', 'Search pages using keywords') // // CACHE MANAGEMENT // @@ -60,60 +65,59 @@ program.on('--help', () => { console.log(help); }); -program.parse(process.argv); +const options = program.parse(process.argv).opts(); -if (program.linux) { - program.os = 'linux'; +if (options.linux) { + options.os = 'linux'; } -if (program.osx) { - program.os = 'osx'; +if (options.osx) { + options.os = 'osx'; } -if (program.sunos) { - program.os = 'sunos'; +if (options.sunos) { + options.os = 'sunos'; } -if (program.windows) { - program.os = 'windows'; +if (options.windows) { + options.os = 'windows'; } let cfg = config.get(); -if (program.os) { - if (platform.isSupported(program.os)) { - cfg.platform = program.os; +if (options.os) { + if (platform.isSupported(options.os)) { + cfg.platform = options.os; } } -if (program.theme) { - cfg.theme = program.theme; +if (options.theme) { + cfg.theme = options.theme; } const tldr = new Tldr(cfg); let p = null; -if (program.list) { - p = tldr.list(program.singleColumn); -} else if (program.listAll) { - p = tldr.listAll(program.singleColumn); -} else if (program.random) { - p = tldr.random(program); -} else if (program.randomExample) { +if (options.list) { + p = tldr.list(options.singleColumn); +} else if (options.listAll) { + p = tldr.listAll(options.singleColumn); +} else if (options.random) { + p = tldr.random(options); +} else if (options.randomExample) { p = tldr.randomExample(); -} else if (program.clearCache) { +} else if (options.clearCache) { p = tldr.clearCache(); -} else if (program.update) { +} else if (options.update) { p = tldr.updateCache() .then(() => { return tldr.updateIndex(); }); -} else if (program.render) { - p = tldr.render(program.render); -} else if (program.search) { - program.args.unshift(program.search); - p = tldr.search(program.args); +} else if (options.render) { + p = tldr.render(options.render); +} else if (options.search) { + p = tldr.search([ options.search, ...program.args ]); } else if (program.args.length >= 1) { - p = tldr.get(program.args, program); + p = tldr.get(program.args, options); } if (p === null) { diff --git a/lib/errors.js b/lib/errors.js index 882c4e5..fc1c511 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -35,20 +35,10 @@ If you want to contribute it, feel free to send a pull request to: ${repo} } } -class MissingRenderPathError extends TldrError { - constructor() { - super('Option --render needs an argument.'); - this.name = 'MissingRenderPathError'; - // eslint-disable-next-line no-magic-numbers - this.code = 4; - } -} - module.exports = { TldrError, EmptyCacheError, - MissingPageError, - MissingRenderPathError + MissingPageError }; function trim(strings, ...values) { diff --git a/lib/tldr.js b/lib/tldr.js index 823e0fc..3c0ffcb 100644 --- a/lib/tldr.js +++ b/lib/tldr.js @@ -4,7 +4,7 @@ const sample = require('lodash/sample'); const fs = require('fs-extra'); const ms = require('ms'); const ora = require('ora'); -const { EmptyCacheError, MissingPageError, MissingRenderPathError } = require('./errors'); +const { EmptyCacheError, MissingPageError } = require('./errors'); const Cache = require('./cache'); const search = require('./search'); const platform = require('./platform'); @@ -73,9 +73,6 @@ class Tldr { } render(file) { - if (typeof file !== 'string') { - throw new MissingRenderPathError(); - } return fs.readFile(file, 'utf8') .then((content) => { // Getting the shortindex first to populate the shortindex var diff --git a/package-lock.json b/package-lock.json index 4d736c1..7dd9d20 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1499,9 +1499,9 @@ "dev": true }, "commander": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-6.1.0.tgz", - "integrity": "sha512-wl7PNrYWd2y5mp1OK/LhTlv8Ff4kQJQRXXAvF+uU/TPNiVJUxZLRYGj/B0y/lPGAVcSbJqH2Za/cvHmrPMC8mA==" + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.0.0.tgz", + "integrity": "sha512-Xvf85aAtu6v22+E5hfVoLHqyul/jyxh91zvqk/ioJTQuJR7Z78n7H558vMPKanPSRgIEeZemT92I2g9Y8LPbSQ==" }, "commondir": { "version": "1.0.1", diff --git a/package.json b/package.json index df87350..dcdeb9e 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "dependencies": { "axios": "^0.21.1", "chalk": "^4.1.0", - "commander": "^6.1.0", + "commander": "^8.0.0", "fs-extra": "^9.0.1", "glob": "^7.1.6", "lodash": "^4.17.20",