Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course

The "Course CLI Project" 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 provides an overview of the course CLI project, Projector, and how the problem will be approached. Projector is a simple CLI application that stores, deletes, or presents variables based on the current working directory or provided path.


Transcript from the "Course CLI Project" Lesson

>> The name of the CLI application we're gonna be building is called projector. It's a simple CLI application that stores, deletes, and presents variables based on the current working directory or a path provided. The reason why I built this is I actually use it a whole bunch for building the Netflix application at my job cuz we have a C++ based application for the television And I often need to go, I need to build this first specific device, I need to build this as a debug one, I need to build it as a release one.

And if you know anything about you know CMake flags and all that they get long and there's a ton of them and I can never remember them. So I just need to go for, projector odroid and it gives me all the flags in this specific path for me.

And so that way, even if I have different like say a release build and a debug build I can actually make it path dependent. So, odroid means, release plus odroid. And the other side means debug plus odroid, so I never have to remember those flags as much. So I kind of built this tool as something that solves my own problem.

And I think that is pretty fantastic for us now. And so we just got done describing the idea that we're going to build which of course is gonna be a simple CLI application. That store, deletes, and presents variables based on your current working directory or a path provided.

So pretty straight forward. So, here's a pretty example, I cd /foo/bar and I call projector. It all prints an empty, right? We've never used it before, there's nothing there to store. And so, it just prints empty brackets. So let's add and get a value. I have no idea how those things are green but they are green.

So let's just go with it cuz that seems exciting. So I can go projector add foo bar, then call projector, and it'll have a key foo and a value bar If I call projector foo, it will just print bar, right? So this kinda like you can imagine this is like path based environment variables, kind of NixOS just without NixOS.

So, very, very exciting. All right, so we also have merging data, right? So I can be inside of foo bar I can cd into bass and then call projector. It still has foo bar from my parent directory, but it has nothing in this directory. So then, if I call foo, it still prints out bar, if I add in foo/bar/baz projector add foo twitch, and then call projector, I get food twitch because that is the deepest directory.

Therefore it gets the most precedent for having that value, as you can kind of see how this works. Effectively a series of merging maps starting with the root routes being the least precedent to your furthest directory being the most precedent. If I cd back and then call projector again, you will notice that I get foo bar because that directory foo bar does not actually change whereas foo bar baz did have foo twitch.

.So depending on your location, where you're calling it, it's going to make sure that it's returning the correct set of variables. In foo bar I can call projector ad bar baz, another one. .Then I can cd into baz, a deeper directory, call it, and then we'll get this entire object back, right?

Because it is every value that's at this directory or higher up in my directory chain. Or, I can call a specific value, bar, and I'll get baz out, even though that is from one directory behind me. I know I didn't need to do the motion but it just feels like it describes exactly how it feels.

The dot dots back there somewhere. And so, let's kinda break up the problem. It's a pretty straightforward problem, right? We have CLI options, cuz there can be a bunch of things there, we have the actual config for what we're going to do, right? This is going to be the operations of adding, removing, printing all that.

We are breaking up the problem right now, which of course is the CLI options. So, we're gonna break up this problem to four steps. And that's kind of how we're gonna solve this is also via these four steps. So, we're gonna parsing the CLI options. So, you can imagine that if we call projector with nothing.

Well, I didn't specify a path to go look into. I didn't specify a config to use, I didn't specify any operation. So there's gonna be the options of it, then there's gonna be the config, based on the options what do we need to do? And what are the key Here's the values, what are we doing?

Where's the config stored? How do we get the values? All that, right? We have to get that, then we're gonna have to print, add or remove, then we have to save in two cases, or display results in a third. So, there we go kind of broke down the problem, pretty simple.

And we'll kind of do each one of these columns one at a time. So, CLI ArgParsing, we're gonna use three different libraries here. The first one of course is NodeJS's command line args. I just randomly pulled one from a hat. There is at least 500 of these, right?

So there's just so many different ones. We're gonna use clap for rust, clap is fantastic. If you haven't use clap, it is by far the greatest using one. And of course Golang, we're gonna use argparse. It's just like the rest of go, a little verbose, but also pretty darn easy to use.

And so, we'll see what happens here. I liked it at least. All right, and then, let let's talk about like the algorithm side of things. So, finding the key, of course, is the fun part. We start off here, right? That means we're gonna need to check here, here, here, here, here, here, here, here.

And depending on the type of operation, this operation we've specified nothing. Therefore we the merge all the values we find along the way into a map. If we do it with this, we're gonna keep on walking up the path until we find the first occurrence, the one with the highest precedence.

Adding and deleting, should only be done in the directory you've specified, either the directory you execute a projector from, or if you provided a path to add it to, it should add it or delete it there. Let's seems to make sense, right? I was also wondering, is anyone a little upset at the fact that I used a deleting before the word adding?

Does that kinda feel just frustrating? I think it's just fantastic. I like doing those kind of things. There you go, deleting or adding.

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