Argparse Basics - How I run my scripts via the Command Line

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you like me you've got scripts that you run for yourself or other people that need to have some kind of input or maybe part of it change to get a different result then you definitely want to look at using arg pass so i'll pass is a way of adding positional or optional arguments to your code when you run it in the command line a lot of linux programs run like this so for example i was to run curl which is a basically a way of getting data off the internet from the command line we can see that it says try curl dash help or car manual for more information and if we were to do that let's say curl dash h for help we can see that it gives us a whole load of apps of options that we can use before our url so in this case these dash dash options here there are the optional arguments and the url here is the positional one which is mandatory to make the program run because after all you can't get information you can't download the data from the url if you don't supply the url so what we're going to do is we're going to take this code that i've got here which is a simple request to an api for sports teams that returns i'm just passing the json data here it returns the stadium that that team plays out so if i run this we'll see right down here it says emirates stadium and if i change it and run it again we can see it says a different one and field which is working which shows that our code is actually working now one thing that we could do is we could say user input we could say team is equal to input and let's just say enter enter team save that so when we run it now it comes down here and you can see it says enter the team and when we type in here it returns the data that's okay but it's not the best way to do this what we want to use is our path so we want to be able to specify our own arguments for when we run this code in the command line so we want to import arg pass and then the two main things that we need to do is we need to basically instantiate our pass and then we need to add some arguments in this case we've only got one which is the team so that's going to make it nice and easy for us so i'm going to say parser is equal to arg pass dot argument passer there we go just like that now that would work but here you can actually add in a description which i'm going to do so we can see where that tile comes up and i'm going to say finds the stadiums great description for what our program does because that's pretty much what it does the next thing we want to do is add an argument so i'm going to say parser because that's what we just created here dot add arguments you can see it pop up there now i'm going to say team because that's what we're calling it then i'm going to add a metavar and i'm going to call it team as well i'm going to say type is equal to string then where i run through these in just a second and then help is equal to enter your team so what these are is basically it's just creating that argument that goes on the end when we run it in that command line so we're calling it team we're saying it has to be a string and the help is basically when we run that help command that dash h it will show that this is what the information that you need to put in so it's just a little reminder so if you come back to this code in a little while or whatever you you can remember how to use it if you don't put anything like this in you'll just be waiting for an argument and you won't know what it is the next thing we want to do is say args is equal to passer dot i think it's pass args there we go so what this is doing is it's just basically creating them all for us and we're saving it in this args variable so then we can do team is equal to args.team so i'm just going to close that up so these are our lines of code that we need to make this passer work we can see that here's the one where we're creating our passer we're adding our one argument and this team is related to here this this name here this is a positional argument which means it's required to run this code because without the team here you can't find the stadium of where they play it kind of makes sense you could add extra optional ones although they don't fit in here and those are done by adding dashes at the start here which would make it optional like i showed you with curl but this is a very simple example so we're just going to keep it to this right now what we want to do now is if we save this and run it we should get a error because pass is not defined because i meant to type parser and again now if i run it we should see i'll just clear that up a bit so it's nice and clear we can see here usage working file and then we can see team just like i showed you with our curl example and it says error the following arguments are required team so if i was to say python3 for me working file which is a really bad name and then give the team name and run it we can see that it takes that argument here it stores it in this team variable which then putting on to the end of our url and it's giving us the response here we can run it again let's try a few other examples that works um i know with this api that the spaces are i think they're done like this there we go and one more you can see that's all working so this is a really cool nice and easy and neat way to just add those extra arguments to your code and you can then just run it nice and easy from the command line you don't have to worry about either opening it up and running it in your code editor just to change it one of the variable name one of the the variables that you're putting into it or having to deal with input which is a bit dodgy and it's not really what this not really what it's designed for so this is a much better way so hopefully you guys have found this useful it's been a bit i know a bit kind of like a rough and ready but quick down a dirty guide to arc past there's so much more to it than this but i really just wanted to show you how i use it in its most basic form for some of my web scraping scripts or some of my uh api uh data gathering scripts that i use for my work so hopefully you found it useful if you've got some value out of it um if you have i'd be great if you hit that thumbs up button drop me a comment and consider subscribing i've got loads of web scraping content on my channel more to come more app building more stuff loads of things gonna happen this year so it'd be really good to have you for that so thank you very much for watching guys and i will see you in the next one goodbye
Info
Channel: John Watson Rooney
Views: 7,563
Rating: undefined out of 5
Keywords: argparse tutorial python 3, argparse tutorial, argparse python, argparse basics, argparse explained, argparse examples python 3, argparse how to, argparse in python example, argparse python tutorial, argparse quickstart, using argparse, what is argparse in python, argparse for dummies, how argparse work python
Id: FbEJN8FsJ9U
Channel Id: undefined
Length: 6min 45sec (405 seconds)
Published: Sun Feb 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.