Parsing Deno Command-Line Arguments

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back today i'm going to teach you how to create applications that take advantage of command line arguments in dino and how these arguments are parsed and processed for example when you write do you know run something like main ts and you write again some parameters of the form file etc these are the uh command line arguments that are passed into the runtime of dino and i'm going to show you how to use your own command line arguments to create interesting applications so let's get started i'm going to use the information that is in the if you go to the dino land web page and you go to the standard library then there is the flags module the flags module is the one that is responsible for parsing the arguments of the command line so i'm going to show you how to use that and we are going to create an application that will solve the quadratic formula from the command line so i thought we'd make something useful either even if it is so i mean probably useless for lots of you but it could have some use for some some some other people for students for example so let's let's get going so i have created a directory called the dino args and i have created a subfolder in that called va.vs code where we are going to store the settings for our project and those settings require that you install the dino official plugin which you can get from the you know extensions market this is the one that is signed by dinoland so if you have that then you need to enable dino you need to use the unstable operator or property in case you want to use the linting functionality and you want to tell the intellisense for dino to use the one from dinoland and also you need to provide the typescript property to use the dino typescript capabilities okay other than that there is nothing else to prepare we can start writing coding the code directly so i'm going to create a file called maints and in that file what we need to do is to import the parse function from the module from the flags module so https it goes from goes to dino dot land slash flag cd of course standard library then flags module then mod ts okay so let's test that so const arguments equals and let's check if the arguments property for dino is there so let's open a terminal and in that terminal let's say dino run main ts and let's check okay there is nothing so let's provide something let's say f um s quark oops oh yeah sure of course um console log um args so let's see yep so um here you can see that we have received some command line arguments um here is the thing if you if you write if you enter command line arguments without any hyphens without any um uh double hyphens like like we did this like this way then these command line arguments will be stored in an in a underscore property array property of the object of command line arguments so this this thing here is a object and that object has the form of underscore which is an array and properties prop 1 value prop 2 value etc okay so if you do something like that x 1 for example then that will be stored here as x column one okay it will be a property if you don't do that if you do if you do this if you write only x without anything then that will go here it will be stored here okay you can also uh create double hyphened properties let's say file name okay and you need to assign assign it a value let's say name of something something this is a string this will be stored here in as file name and call on the f name that's the value okay so um having said that we can we can now start programming the small application that we have in mind so we see that this works and let's go with declaring our variables so i'm going to pass as you know if you if you take a look at the quadratic formula the quadratic formula has the form of oops let me check okay i'm not sure if it is possible to see okay so here you have the quadratic formula has the form of a multiplied by x squared plus b multiplied by x plus c equals and we know that this in in the set of real numbers this has either one solution no solutions or two solutions so we want to enter the parameters a b and c to the program and from that we will try to figure out whether there is an x that is fulfilling this equation or not so um for that i'm going to declare uh the parameter a and i'm expecting if you if you remember we defined arcs to be uh the parsed property uh the past result of dino arcs so we are expecting that uh a will be a property of args so it will be a because a will be written as a dash a and some number for example four okay so again const b equals args b const c equals r c and let's check whether we have received those properly so i'm going to uh check whether we have that value or not so b is um b and c is c okay so let's try this um i'm going to say a equals one b equals two and c equals three and as you see we have received those as properties to the args object and we are displaying them after that so this works so now we need to define our i don't know solutions if you want so x x1 x2 and these are numbers okay so we need to declare the discriminator so discriminator that equals to b square minus 4 multiplied by a multiplied by c okay so what are we also want to do something like this so in case you want to store the um the results of your application in a file we're going to declare whether to use a file and that should receive something either true or false okay and in case you want that then we are going to declare another parameter called file name and that should be uh receiving the name of the file you want to store the results to so something like that so in that case we are going to assume if the file there is a property called file and we should check if it is defined or not if it is undefined then we don't want to store the values inside the file if it is defined then we are going to grab the file name for that and we will going to open a file create a file and store the results there so for that purpose we are going to declare a text encoder and if you've watched my previous tutorial on file handling this encoder will be encoding text or anything else any other type of data so it can be stored into a file but for our purposes it will be encoding text data and also we are going to declare a file handler this will be used in a bit so um we are going to check if the args file is there if it is there then encoder is equal to new text encoder so it will be a new instance and in that case the file the file handler will be await dino open oops open the arcs of the fname property of the args and i'm going to assume that it is there i'm not going to handle every single error here so i'm going to assume that it is there if if file is there so and we are going to pass some options one of the options is write write is true so we are going to write into that file and we are going to create that file if it is not created already so other than that we are going to continue now if um if not a which means the parameter a is not defined if it is defined and if the value isn't uh if the value is actually zero so you see we will have zero x square plus b x okay plus c equals zero and in that case that that will be that will mean that uh x there is only one solution which is x and that is uh minus c uh divided by b okay so but also we need to check whether b itself is there or not so if b is not present or b equals to zero okay then that means uh there is no solution for that equation so console log yeah because if b is zero okay uh b equals zero then uh we will get the equation uh the this equation c equals zero and that is [Music] true only if c itself is 0. okay so if you imp for example if you enter something else then this equation is invalid okay so if b equals and c is not oops equals zero console log um invalid equation okay so let's check so other than that um what else we have in that case uh oops um in that case x is equal as we said to minus c on b okay so okay um yeah we need to output that so log um one real solution found x equals x okay and yeah in that case uh we are going to check if the file handler is activated and if the if the encoder is also activated then we need to store the information inside the file so const data i'm going to say what uh encoder encode and now i'm going to put this information inside the file okay so that will be do you know right you need to pass the file handler where you want to store your data and the data itself okay um other than that okay um in case a is not invalid or a is not zero then we will have to solve the general case and this one is the one that will check for the discriminator if it is greater or uh not than the zero so if it is uh equals to zero then we have also one solution and that one solution is uh minus b divided by two a okay provided of course uh a isn't uh yeah a isn't zero which is true in our case so again this also means that we need to input i mean output the finding so one real solution found which is this okay and the same from above i will copy this paste it here so if we have the file handler activated and the encoder then yeah that's it so we will pass that data to the file and we will write it down there other than that we will check um else if the discriminator is less than zero uh then i'm just going to say console log um no real solutions are found okay and [Music] um i think yeah so else this is the case where the discriminator is greater than zero and uh in that case we will be having two real solutions so i'm going to say x 1 equals 2 minus b minus math well we need to use the square root tool from the math library so sqrt and of course you remember that the solutions are defined this way it's minus b plus minus the square root of the discriminator divided by 2a so it's minus b and in this case square root of um sorry b multiplied by b minus 4 multiplied by a multiplied by c and all that is divided by 2 a okay and the other one is similar except for the plus sign here okay and we can say console log to real solutions found x1 equals x1 and x2 equal equals to x2 yeah sorry okay so x1 x2 we have that and of course if we have the encoder and the file handlers enabled then we can save the information inside the file so cost data equals encoder encode and we are going to put that info there okay so you know right all and we are going to say write all to f and with the data so um i think that's it so we can try that now and see what's going to happen so let's say um [Music] i don't know let's make five and the file is true and the name is output text okay let's see what will happen oh and of course because uh yeah we are going to write then i'm going to allow right into file because other otherwise that won't run so okay so you see we have two real solutions here and we have the output file and that output file has the answers of course you can if you watched my csv tutorial you can input that information into csv with without any you know useless text data but that's how you can open text files and you know um let's try it again so this time i'm not going to use any files i'm going to say one i'm going to say zero here so we have one solution here and x is one on five which is twenty percent minus twenty 20 yeah let's try something else let's say one and let's give this five no real solutions found because yeah because the discriminator would be less than zero and let's try something else let's say i don't know let's say this is zero and this is one no real solutions found yeah that's true because uh we will be having minus four by five which is twenty by one which is minus twenty so um yeah so let's try it again let's say four let's give this one and let's say file name file equals true and flame equals something text okay right so we have two solutions here again and we have another file called something text and it has this value okay so as you can see this is very easy to implement it's very versatile you can you can write robust command line applications using this and you can probably create some shell script procedures using this very easily and that concludes this tutorial please share like and subscribe and leave any comment if you have any questions see you the next time
Info
Channel: Eduard Karesli
Views: 34
Rating: 0 out of 5
Keywords: Deno programming, Node.js, programming, web development, JavaScript, TypeScript, Command-line
Id: 4GgFpsbyBOM
Channel Id: undefined
Length: 26min 34sec (1594 seconds)
Published: Thu Nov 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.