Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course:
The "Process CLI Args in TypeScript & Node" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through the setup for the TypeScript version of the CLI project. The goal of this segment is to get the command line arguments and turn them into an object that can be manipulated.

Get Unlimited Access Now

Transcript from the "Process CLI Args in TypeScript & Node" Lesson

>> All right, so we're breaking up the problem. So first let's start here, the CLI options, let's solve this for TypeScript. We're gonna always start in TypeScript, even though some of these things are gonna be a little different I think this is probably going to be the most different part of it and I may forget one option which I really am hoping I do not forget one option cuz that's always the worst.

[00:00:19] But this is just using a library, right? So we're just gonna have to do it. The reason why I chose this library was this is one of the first ones I found that also had a pretty simple interface. There wasn't a lot to it. You could kind of just look at it and go, okay, I see effectively what you got to do.

[00:00:34] And that's good enough for me. All right, let's see. So let's set up a project, this is effectively how I might set up my project. I think I'm gonna drop the word projector from each one of them. You can choose how to set it up. The places where we did all those practice problems is where I'm gonna be just doing it all cuz I already have it set up.

[00:00:52] I already have cargo knitted. I already have Gogo knitted I already have TypeScript installed with TS node and Typescript and types node so everything seems pretty good. I also use Tmux by the way so I mean if you're wondering how switching so fast before it's Tmux just in case you're wondering.

[00:01:07] I also use Vim by the way it's pretty fantastic, I don't know if that but Vim's fantastic. I also use Dvorak by the way. I just by the way do three times in the row by the way it's just because I use a lot of super cool stuff.

[00:01:19] I also use a Kinesis 360 Advantage prototype, okay, right here proto type, anywho so let's get started, so we need to install all these things. So I'm just gonna grab this because I already have my directory sort of started up, I don't need to do the old yarn knit.

[00:01:34] I don't need to do the TLS node, I don't need to do the git init. But we do need to get that Git ignore going. So let's do that right now. So I'm gonna jump back here and I'm gonna go projects, TypeScript, awesome. And let's just, I'm gonna delete out all these things.

[00:01:50] We just don't need those anymore. I'm gonna add a gitignore, and I'm gonna put in my node modules. Just set up your project so it's not totally screwed up right now. And then of course we need to add in that yarn command, you can copy and paste it for yourself, but it's yard add.

[00:02:06] Command line args and of course types command line args. I know there's dependency or developer dependencies versus not, we're just not gonna care for this, right? You can set up your project nice and pristine. This is more about learning a language, not crying and configuration. All right there we go, awesome.

[00:02:21] We have command line args, we should be able to get started now. So I'm gonna jump back to my favorite editor, so let's just get started. So we're gonna open up our source directory and we're gonna create the opts.ts, right? So, the only responsibility of this one is simply to get the command line arguments and turn it into an object that we can manipulate.

[00:02:42] I like to do things kind of in simple steps. I don't know, easiest for me, I'm too simple to be able to understand a bunch of things I want. So I just do it very, very simple. So first, let's think about our type, right? Export type, we'll just call this Opts, right?

[00:02:54] Why not? We can keep it simple. And we need that nice little equal sign, because it's how works. And so if you think about it, we can call projector with nothing. Therefore we should probably have a set of args that are some sort of string array but they may be nothing, right?

[00:03:10] We may not provide anything, so we do want to kind of constrain ourself in a sort of way. Next we're gonna want a present working directory, right? A present working directory of course is either the directory you execute, project or from or a directory you give it, right?

[00:03:25] So you can imagine we could have a command line flag that is just pwd, so you can say hey, go execute, go give me the variables that are located in this other area. That way you could imagine you could use this in CI pretty easy. You don't have to be in the directory you want to execute, you just simply need to point it towards the directory.

[00:03:42] And we'll do the same thing with config. We can also imagine that there's a config, that is your JSON config that contains all your values. So either we can point to some sort of default location that everyone's used to using or we can point to a specific one that says our unusual one.

[00:04:00] So for purposes of just testing ourselves, we're gonna do this but you can also imagine that there's some sort of CLI out there or CI operation out there that wanna do it in a specific way, this would give them the opportunity to just point somewhere else. Awesome. So let's export a nice default function and just do getOps and of course this thing is gonna retun an ops, right?

[00:04:23] Feels good, everyone's on board, all right so we need to use that whole command line arguments. So I'm gonna import import CLI from command line args, awesome. There we go, we have it, we can just jump into the code itself and just look at it. You can see right away that it takes.

[00:04:42] It's just a function that takes in some sort of options definitions. That thing includes the long option name, right? I also by the way, I really do like looking through the source code. I feel like that's often the best documentation out there is what the code actually says it's going to do.

[00:04:57] So name is obviously required, right? It's not so much of an option definition. It's a required definition. We can have type if it's a string, what is it gonna be? Alias? So this would be like the single character if we wanted to add in a say a dash P option for the present working directory, multiple, lazy multiple, default option.

[00:05:17] So default option is any values unaccounted for by the option definition will be set on this one. So we probably need that one as well, right? Cuz we have projector add foo bar, right, so all of those needs to be put into a specific location. This sounds like our goal here.

[00:05:32] So I feel pretty confident at this point. So let's go here and go return CLI, and I believe it said it takes an array of these kind of configs here. So let's just build them one at a time. First thing is name, right? You can see right here, let's call the first one.

[00:05:49] Let's start with args, right? Cuz that's the one you know, that's our first one. All right, args, awesome. This is gonna be that default value. Right, true all right, that's the one we want to make sure that everything just falls into and then of course was there anything else?

[00:06:05] Yeah, type we probably want the type. The type should be something along the lines of a string array is that what I wanted? Or it's a string. We got multiple, true, right? We can try out these different ones to see how each one does, we can go look up on the documentation to see how they do that but each one seems to have its own options.

[00:06:26] I believe this is about the right set of values, we may need to include a type string, I'm not 100% sure quite yet. And so let's just return it as Opts, there we go cuz it doesn't quite define what the object is coming out of it. So of course I'm gonna cheat and type land and just say, trust me, I'm telling you the truth.

[00:06:44] I'm never lying to you. Come on, I'd never lie to you. And so let's create another one of these and let's click this. Name should be config, all right? This is definitely the config you want to use for your projector and we're definitely going to want an alias.

[00:07:00] C, right? That makes sense. We're gonna want to be able to just say dash C as opposed to dash, dash config. On top of that we're probably gonna want the type to be a string, right? I believe I should be doing this as well, we'll find out. And then let's do one more, let's do present working directory.

[00:07:19] So in case you want to be able to use values from somewhere else on your operating system and we can use a p right here. So if I've done everything correct, this should effectively be everything we need for this program. And so let's create a index, index.ts, everyone's favorite file and we should be able to import that little option creator and it should just create the options for us, right?

[00:07:45] So import getOpts from, what is it? GetOpts or opts, there we go. Awesome. And now I should be able to go console.loggetOpts. So long as I've done everything correctly in npx ts-node index, it should just simply print out. Okay, there's nothing there but I did get this args: true so I probably missed up default value versus default option.

[00:08:14] So let's just go back in there, make sure I didn't do that. The initial value, whoopsie daisies, right? I did the wrong thing, it should have been default option. There you go, awesome. Re-executed again and its like, hey there's nothing, you're good to go. I can never remember with npx do, you have to do the double dashes, we can find out foo bar.

[00:08:36] Okay, that's not quite it. So you might not need to do the double dashes, foo bar. Okay. So it's saying unknown value. What are we getting here? All right, so it looks like we got ourselves a small little bit of an error, which is a little bit funny, cuz I figured that was the right way to do it.

[00:08:51] It might be multiple. Is the thing we're missing, hey can you have multiple items? That's probably the thing we're missing, doing these things from heart, you always forget a couple of the options there we go. Foo bar, we got the options out, fantastic. And so if I have nothing, what do we get?

[00:09:07] We get nothing, so that's exactly kind of how we specified it. Can I do-- config? And then this would be a path, right? Awesome. So we see that config is correct. We see that the args are correct. And I bet you present working directory is the exact same thing.

[00:09:28] Perfect. Okay, so I'd say that we've effectively finished this little object. They're mostly undefined or notables. But some of them of course are, they require this. So here we go. I'll leave this up for a quick second. If you wish to follow along, I believe if I am not mistaken, I also have the code on the website.

[00:09:48] So if you just feel like cutting and pasting this, it's just a bunch of configuration. Are you getting a much out of it? That's up to you. Here we go. I'm gonna jump over for a second. There we go. Arguments type string default option. True, multiple, true. See, I eventually was gonna get it.

[00:10:02] And I also called the projector options. Like look at how ambitious I was at one point, man. How far do you fall when you start calling something ops? Just nothing right there.