-
Notifications
You must be signed in to change notification settings - Fork 51
feat(help): add help command #49
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
Conversation
import shell from 'shelljs'; | ||
|
||
const commandList = Object.keys(shell) | ||
.filter(cmd => typeof shell[cmd] === 'function' && cmd !== 'ShellString'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, this would be a part of shelljs itself.
Also, it would be nice if each command had a .help
property which contained documentation. Maybe we could generate the documentation from that.
Or... (WARNING: Crazy Ahead):
Currently the docs are like this:
//@ Blah Blah Blah
function _blah() {
do_something();
}
What if we made them like this:
function _blah() {
//@ Blah Blah Blah
do_something();
}
AFAIK, it would work entirely the same, documentup doesn't actually care about the function, but then we could parse it like this:
var lines = shell[cmd].toString().split('\n').slice(1, -1); // Remove the first and last lines.
var docLines = [];
for (var i = 0; i < lines.length; ++i) {
if (lines[i].strip().slice(0, 3) === '//@') {
docLines.push(lines[i].strip().slice(3));
} else {
break;
}
}
shell[cmd].help = docLines.join('\n');
@ariporad Can you add unit tests for this as well? Also, the output looks off on my Linux box. I only see one character per line, which isn't ideal. Here's a screenshot of running |
I really think we should consider The output of help is also borked right now: |
@levithomason: The problem with Also, I fixed the output. |
const commandList = Object.keys(shell) | ||
.filter(cmd => typeof shell[cmd] === 'function' && cmd !== 'ShellString'); | ||
|
||
const help = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm totally open to help with the actual help message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use a template string instead, they are multi-line:
const help = `
shx: A wrapper for shelljs UNIX commands.
Usage: shx <command> [options]
Example:
$ shx ls .
foo.txt
bar.txt
baz.js
$ shx rm -rf *.txt
$ shx ls .
baz.js
Commands:
${commandList.map(cmd => ` - ${cmd}`).join('\n')}
`;
I added a Usage: part in there, feel free to rework this.
I get the same output. Also, for starters, we could just loop through the shelljs commands and add the relevant parts to the yargs config. As shelljs exposed more info, we could beef that up. |
I don't know if we necessarily need yargs, since we're just trying to output a static string. Doesn't matter much to me though. |
Output definitely needs to be changed though, since it implies that |
Output working, sorry had to rebuild! |
Should be fixed. Thanks! Ari — |
@@ -0,0 +1,29 @@ | |||
import shell from 'shelljs'; | |||
|
|||
const badCommands = ['ShellString', 'cd', 'pushd', 'popd', 'dirs']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a CMD_BLACKLIST
already defined in shx.js
. Suggest pulling that and the ERROR_CODES
into a config.js
or something so it can imported into both shx.js
and help.js
.
Then you can filter based on the same list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Thanks! Ari
On Thu, May 5, 2016 at 5:11 PM, Levi Thomason notifications@github.com wrote:
In src/help.js [https://github.com//pull/49#discussion_r62274779] :
@@ -0,0 +1,29 @@
+import shell from 'shelljs';
+
+const badCommands = ['ShellString', 'cd', 'pushd', 'popd', 'dirs'];
There is a CMD_BLACKLIST already defined in shx.js . Suggest pulling that and the ERROR_CODES into a config.js or something so it can imported into both places.
Then you can filter based on the same list.
—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub [https://github.com//pull/49/files/c476547d608b00becee7cd1584e944c2d134c034#r62274779]
Looking good, tested node 4/5/6. Just needs a trailing comma cleaned up in the commands list:
|
Done.x |
nit picky so feel free to ignore, but there are two line breaks at the end of the commands list. otherwise, LGTM.
|
EDIT submitted before done Would be nice to print the help after each error in shx as well: if (!fnName) {
console.error('Error: Missing ShellJS command name');
console.error(help());
return EXIT_CODES.SHX_ERROR;
}
// validate command
if (typeof shell[fnName] !== 'function') {
console.error(`Error: Invalid ShellJS command: ${fnName}.`);
console.error(help());
return EXIT_CODES.SHX_ERROR;
} else if (CMD_BLACKLIST.indexOf(fnName) > -1) {
console.error(`Warning: shx ${fnName} is not supported`);
console.error(help());
return EXIT_CODES.SHX_ERROR;
} Then, we get helpful errors instead of just errors. |
I think it's most useful to have the full help only after I like this because it keeps output succinct for simple mistakes (like |
@nfischer that is definitely superior. I'd sign off on that. |
@shelljs/contributors any further work on fixing the broken build/writing tests for this? I may be able to take up some work on this tonight or tomorrow. |
Been slammed here, will try to take a look tonight. No guarantees. If @ariporad is down, I say whoever can push it over the finish line should go for it. |
I'll try to work on this later tonight. |
Also shows help on `shx nonExistantCommand`
Ok, tests added, and it should print help on |
Per @nfischer's comment, i think it would be best to not show the full help when missing a command or an invalid command is used, but instead, just show the error plus something like "For more info: shx help" Other than that, this looks great! |
Current coverage is 90.32%@@ master #49 diff @@
==========================================
Files 2 4 +2
Lines 19 31 +12
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 16 28 +12
Misses 3 3
Partials 0 0
|
Pulled and tested, LGTM. Going to merge this. |
Awesome, thanks for updating the tests 👍 |
No description provided.