Polyglot Programming: TypeScript, Go, & Rust

Process CLI Args in TypeScript & Node



Polyglot Programming: TypeScript, Go, & Rust

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.


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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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?

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?

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?

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.

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.

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?

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.

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.

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.

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.

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.

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?

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.

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.

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.

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.

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?

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.

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.

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.

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?

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.

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.

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.

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.

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