I currently have yargs working in an app that can run with no arguments, or EITHER or BOTH of the following:
a '-o' option with exactly one argument
a '-f' option with no arguments
These work, but nothing stops the user entering extra options or arguments which get ignored, e.g.:
app hello
app -f hello
app -o hello hello
I would rather fail and remind them of the usage. I think a .check() function is the right way, but I can not find many examples online. Could anyone suggest a check() function, if this is the right way forward?
Okay, after some experimenting, it seems the following works:
.check(function (argv) {
if (argv.f && argv.f !== true) {
return false;
}
if (argv._.length) {
return false;
}
return true;
})
Hope that's helpful to someone in the future
Related
I'm migrating a package from a much older version of commander (v2.20.3)
program.command(`install [pkg]`)
.alias(`i`)
.action(installPackageOrLocal)
.option(`-S, --save`, `Save to dependencies`)
.option(`-D, --save-dev`, `Save to devDependencies`)
.option(`--production`, `Will not install modules listed in devDependencies`)
.option(`--test`, `Exit with code 1 if package limits like maxPackagesNumber or maxSizeBites exceeded`);
I'd like for the default (when calling the CLI with no arguments at all) to continue being the display of the help and no erring out, but currently it errs with:
.../npm-reflect/node_modules/.pnpm/commander#8.3.0/node_modules/commander/lib/command.js:142
const [, name, args] = nameAndArgs.match(/([^ ]+) (.)/);
I was able to get the mostly desired behavior by adding:
program.command('help', {isDefault: true})
.action(() => {
program.help();
})
.command(`install [pkg]`)
// ...
...but this seems to be polluting things in the help by listing a new "help" command. How can I avoid the parser complaining when no arguments are present yet without adding a new command?
The default behaviour in latest Commander is to display the help if you have subcommands and do not specify a subcommand. Which sounds like what you want! Not sure how you are getting an error, you might want to open a Commander issue for help.
In general, if you want some custom behaviour for no arguments then it may be simple and easy to check yourself before calling parse(). e.g.
if (process.argv.length < 3)
program.help(); // exits
program.parse(process.argv);
I'm trying to learn testing in general and Jest in particular.
I'm going through strange cases just to have an idea of how to handle them.
I have this super basic example. A function that takes a string and if the string is hello it alerts it.
function alertHello(input='') {
if (input === 'hello') alert(input)
}
I want to test the alert.
I checked some StackOverflow posts where the suggestion is to mock it
jest.spyOn(window, 'alert').mockImplementation(() => {});
But I cannot still understand how to implement it.
Any help and advice will be appreciated.
Is there an ESLint rule to detect if the code can be detected when it's not using guard clauses?
This is related to this post- https://elliotekj.com/2016/12/02/guard-clauses-in-javascript/
for example, this should HAVE a warning:
function doSomething(obj) {
if(obj) {
console.log('I did something');
}
return null;
}
This should have NO warning
function doSomething(obj) {
if(!obj) {
return null;
}
console.log('I did something');
}
I would prefer the latter. Please don't comment on the 'opinionated' aspect of this question. I just want to know if there is an ESLint specific rule or something similar that I can work with.
Or is ESLint even the right tool to detect/correct this kind of code style enforcement?
Thank you.
PS. I did do some research and did not found any rules that related to this my specific need on ESLint Rules. Now, I'm just a newcomer JS developer and would like to know if I miss anything that I should have checked before I go any deeper into writing my own rule.
There is no ESLint rule that will catch that (up to v5.8.0). You provided the source yourself :)
I'm all in for guard clauses but I don't think this is a feature coming anytime soon because it's very context dependent and difficult to correctly identify it on all cases.
A sidenote from looking at your snippet, if you want to follow ESLint best practices, you should be returning a undefined/void instead of null in cases you don't expect a value to be returned (check https://eslint.org/docs/rules/consistent-return#require-return-statements-to-either-always-or-never-specify-values-consistent-return)
I am trying to setup a CasperJS script that will do some testing on a personal site.
I need to check for a selector, and if it cannot be found on the page, click the next page link in the pagination and check again.
I am struggling to wrap my head around this problem and how to solve it. I know I need some sort of loop, and I even tried a while() loop, but I don't understand CasperJS enough to get it working.
Basic idea of what I want, in psuedocode:
open page http://www.example.com
check if 'li.my-class' exists
if not
click '.next-page'
then check again for 'li.my-class'
(repeat this process)
else
'li.my-class' exists, go do something else
I've tried reading about waitFor, waitforSelector etc. The documentation doesn't help me much as it's quite basic in terms of examples.
I recommend you to use recursion, especially a recursive IIFE. Here is the implementation:
var casper = require('casper').create();
casper.start('http://www.example.com');
(function go() {
casper.wait(1000, function () {
if (!this.exists('li.my-class')) {
this.click('.next-page');
go();
} else {
// Do something...
}
});
})();
casper.run();
I have webpage that uses various libraries and some personal javascript code. I've tested it on my machine and it works.
When I pushed it to production a user informed my that the page was broken. After looking at their console log I see that my page is now throwing a syntaxerror on a generic function that seems to be fine. I can't reproduce it on my machine but it happens on theirs.
I'm assuming its either a different os issue or a x-browser version issue (I'm using Win-8, Chrome they are using Mac, Chrome). I could see how a function would break and it would spit out an error saying it couldn't execute function of undefined. What would be my next step in trying to identify what could be the cause?
The syntaxerror is "Unexpected token ("
Is there some php I can inject into the page to get more info about this error?
var objBillManager = {
...
objBillTableManager:{
...
GetsBillsStatus(bPayed){
if(bPayed == "1"){
return "Payed";
} else {
return "Not Payed";
}
},
...
},
...
}
Without seeing more, it's hard to know what's going on. As someone else mentioned, firebug is a pretty good debugger.
One thing I noticed: your GetsBillsStatus was not defined as a function. It should look like this:
GetsBillsStatus: function(bPayed){
if(bPayed == "1") {
return "Payed";
} else {
return "Not Payed";
}
}
Maybe that's the issue?