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

Require argument parameters #359

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
70 changes: 37 additions & 33 deletions bin/tldr
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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 <file>', '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 <type>', '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 <theme>', 'Color theme (simple, base16, ocean)')
.option('-s, --search <keywords>', 'Search pages using keywords')
//
// CACHE MANAGEMENT
//
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 1 addition & 11 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions lib/tldr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down