Digging Into Node.js

Argument Handling

Kyle Simpson

Kyle Simpson

You Don't Know JS
Digging Into Node.js

Check out a free preview of the full Digging Into Node.js course

The "Argument Handling" Lesson is part of the full, Digging Into Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Kyle writes code to handle the arguments in the generated object from minimist, output a help message if the help argument is included, make sure sufficient arguments are added, and print an error if input requirements are unmet.

Preview
Close

Transcript from the "Argument Handling" Lesson

[00:00:00]
>> Kyle Simpson: All right, so we wanna do something useful with these inputs, we wanna start deciding what to do. So let's just write some if-logic for a moment. Let's just say, if (args.help), then we know we wanna print the help.
>> Kyle Simpson: That means, if there's a --help anywhere in there, even if there's other stuff, it sort of overrides everything.

[00:00:30]
It says, I definitely wanna see the help. So if args.help has been specified, we wanna print the help. Then let's have an else if clause that, else if (args.file), in other words, that's been specified, and it's a non-empty string, then we're gonna go on and do something with that file.

[00:00:47]
So maybe we'll just print out whatever the contents of args.file are.
>> Kyle Simpson: And then lastly, we wanna say, if we didn't pass in help, and we didn't pass in a file parameter? We wanna treat that as an error condition, because you didn't do what was required, you didn't do the minimum-require.

[00:01:07]
So we're gonna do an else condition, and I'm gonna define a little helper for myself, called error. So down here at the bottom, I'm going to write, function error.
>> Kyle Simpson: It's gonna take a message, and what I'm going to do is console.error the message that we're printing. But I also wanna do one other little thing, which is typically, if there's an error, in some cases you want to just show the error.

[00:01:39]
But in other cases, if it's a mis-usage, you wanna go ahead and print the help for them to see. So I'm gonna include, I'm gonna have a parameter here called includeHelp, and we'll default that to false.
>> Kyle Simpson: And so we'll print the error message, and then if includeHelp we'll put a separating empty line, and then we'll call printHelp.

[00:02:15]
>> Kyle Simpson: So up in our else clause, then, we'll use this helper, we'll say,
>> Kyle Simpson: Incorrect usage.
>> Kyle Simpson: And we want to pass true, to tell it to go ahead and print the help.
>> Kyle Simpson: Let's try it without any input at all.
>> Kyle Simpson: And you'll see now, we've got the incorrect usage, and then it printed the help.

[00:02:50]
We could try it with a --help, and we only get the help, or we can try it with a --file that has no value, and it says incorrect usage. Or if we actually give it a value, like hello, then it prints that input.
>> Kyle Simpson: So now, we are successfully receiving inputs to our Node program, and using them to make decisions.

[00:03:17]
Let's also not forget that we should update our help, so that it is useful to somebody. This is, again, so easy to forget to do, and then later on, we're pulling our hair out. So let's make sure we update this with something useful.
>> Kyle Simpson: Be as creative as you'd like with your help output.

[00:03:58]
A quick note on minimist. If you were to look up and minimist and see, you would find that literally tens of thousands of other packages in existence rely upon minimist. It has no dependencies itself, and it is depended upon by almost every other major package out there. It's a really good, solid way of handling, which is why I picked it for the purposes of this exercise, good, solid way of handling it.

[00:04:24]
It is very low level, it's very bare-bones, it doesn't have a ton. If you go look up the documentation for it, it doesn't have a lot of details to it. So if you want something a bit more sophisticated, there are a few that I might recommend. In particular, I would recommend one called Yargs, Y-args.

[00:04:42]
It uses minimist, and it wraps on top of minimist. It uses this special syntax where you define what your options are, and what their types are. And one of the benefits of Yargs, one of the many benefits, is that it will automatically generate your help output from your args configuration.

[00:05:01]
So if you don't wanna manually keep your help updated, Yargs is another one. Just a quick little note on Yargs. When you go and look at the documentation for that, you will find out that the author of that, and I know him, he's a great guy. But the author of that package really went all in on the pirate theme.

[00:05:19]
Yargs, the entire documentation, everything, all the examples, they're all in pirate-speak. So that might be good just for the entertainment value, even if you don't actually wanna use Yargs. But anyway, I like to use minimist, I probably use that 95%of the time. Every once in a while, I use one of those sophisticated packages.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now