python argparse tutorial (beginner - intermediate) anthony explains #044

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another video in this one I want to cover art parts which is a Python standard library module and it's useful for parsing command line arguments and writing you know your own man line tools I covered this a little bit in an earlier episode and I'll actually link that below but we're gonna go into more details about our parts today so let's jump into that okay so I've got a lot to cover today so I left myself a little outline this is the only prep that I did for this episode so hopefully open this goes well enough but I want to cover all of these various things about arc parse and hopefully give you some examples that you can use in writing your own command line tools but just to get started I'm gonna write kind of the boilerplate for a little command line app if you want more of an explanation of what this is about the the video that I'll link in the description goes over this so we're gonna be importing arrgh parse our parse will also use P prints because we're gonna be doing some pretty printing from typing import sequence and we'll have some typing here and we're gonna invent these because we're gonna eventually you know show examples of these side-by-side optional sequence return an integer cool so we're gonna add our argument purser here I requires two argument parser so this is kind of where you'll start all of your arc parfaits code you'll instantiate an argument parser and this instance of this class will be to being the argument parsing for us and we'll be modifying that class with some calls that'll set up all the arguments that we want and finally we'll retrieve our arms parser to parse pars and this little trick with arc v8 that gets explained in the other video so I'm not going to be explaining here but it's a way to allow it allowed to be easier to test we're just going to print out the arguments here just to show what happens so this is kind of like the very most barebone example of art person make it pet bait compatible and you'll see if the REM this one three eat eat pie you'll see that you know we parsed who first no arguments but the nice thing that art parts gives us out of box is it gives us a help for three we don't actually have to do anything to make help work it'll automatically populate this but we don't have any arguments or options or anything yet so it's just you know the most basic helping it now one nice thing that it does give you is you'll get the program name in the output by default and the help option is automatically documented and so you don't have to do anything special there anything there's the there's the most basics so let's let's jump into our first topic which is positional arguments and art cars and our quest loosely follows the conventions of like GNU POSIX options and so a lot of this is convention driven so let's look at a positional arguments a positional argument while all arguments in art burns well almost all arguments and arg parts are added using the add argument call we'll do parser and argument and for positional arguments they are ones that do not start with a dash so if you do like filename as an example this is a positional argument and by default positional arguments are required so you'll notice there's no - dashing and if we run this again with help and if I could spell arguments oops there we go we run this again with help you'll see that now we have a positional argument here and if we run this with no options we'll actually get an error this is a cool thing that our purse gives you out of the box is you'll get error messages for things that are problematic that's saying here that this argument is required because positional arguments by default are required you can actually change that within args but we're not gonna go for NR because here because it's a little bit complicated but if we you know past filename now you'll see that in our parsed arguments here we got any file name foo that's kind of our very most basic stuff here now I'm actually going to move this topic bits will do will do positional and help at the same time for any argument that you want to display a help message here so you'll notice that when we got positional arguments here this didn't actually say anything didn't explain what this file name is now a file name is a pretty decent name so you probably don't have to explain it but if you needed to you could add a help here in the call to add argument so you might do something like configuration the file name or I don't know whatever whatever you want to do but now that that help text here will show up in this message here there's also some stuff you can substitute in here like I believe what is it percent prog yes yeah so this will substitute the program name if you had a default here which you actually don't have defaults for positional arguments but will will show one with optional arguments in a second but there's there's several things that you can substitute there so like prog there's defaults and I think that's it there might be more anyway so let's talk about optional we'll also talk about short and long and aliases and defaults all at the same time and again you're gonna call it in the same way as before and arguments now the convention for options or optional things is to prefix them with a - you either have short options which is a single - or long options let's actually do a little comment this out because we don't we don't want that so let's say that we want like - C for config file and - - config now you can specify as many different options that you want here and you actually don't need a short option you could just leave it as a long option and that'll work fine but a long option is required for our course for everything but let's keep it like this and let's say that we also wanted you know maybe there was some backwards compatibility that we needed here so we need to define an alias for - stretch config and maybe that's you know JSON file or something like that I don't know somebody's for config it's it's a bad example but so yeah we covered short options which are just single single - I'm gonna see you can actually have short options with multiple letters like like so but I would not suggest it because it kind of goes against the the ideas of you know POSIX or GNU options so I would always suggest using single letters for your short options short options being only one - and then your long option should be a descriptive name the long option actually ends up being the name that you'll see in this args object at the end I know and I forgot to explain this down here I'm doing a little tricky thing to retrieve all of these as a dictionary just so that I can pretty print it but normally you would access them by using the dot operator on this args object but anyway here's now our our optional option here and if we go back over here and we run this again you'll see that we now get - see config - brush config convey and JSON file config so the help automatically shows you all of the various options here we run this with no options you'll see that config by default is none so that's the the default there but if we do you know config what you'll see that we can pass along an argument there we did not cover defaults so let's do defaults now maybe our default is you know config dot JSON or something like that it's actually a tad this house it's a little bit easier to see I usually like to keep the options on one line and then the arguments on another but it really doesn't matter you can pick whatever you wants and you know black will have its opinion about that and other four matters will have their opinion about it but yeah you can set a default here and the thing that I was talking about earlier with the help message um specify the config file defaults percent defaults [Music] and so you can you can use this substitution parameter to show the default value there so you can see we do Dexter's help again it'll say specify the quick Val default is config dot JSON so this is how you specify a default option for an optional option no I didn't actually mention it here but you can make optional options required by setting required equals true so now if I were to try and call this without - just ok it'll tell me that the following arguments are required this one here but if we you know pass it in we'll get that it kind of makes no sense to have required equals true and default I don't think these can ever work together like this default will never be applied if you have required but it is a thing okay so that cover is optional options and positional options and then the rest are just kind of extensions on those so the first thing that we'll extend is a custom type so you can pass the type keyword argument to add argument and that'll allow you to pick a particular type for that option so you might do something like parse or not an argument maybe it's like number of days or something like maybe this is a logging tool and you can use type equals int this will check the this will make sure that when you pass a days in it will convert it to an integer automatically for you will actually show what that does if I don't have this so we can see the difference there created days you know 10 you'll see that we get days as a string here and if we do days you know asdf it's not actually validating that this is an integer at all but if we make this type equals and now you'll see that we get an invalid int value asdf right have we specified 10 oh that's not 10 there we go you'll see that it converted it from a string up here into an integer down here so that's how you can add types to things you can also do custom types so let's say once I should just copy this that commented out here I really need to make a thing that comments out blocks in my editor I haven't done that yet but I will I will get that eventually okay let's say we wanted a positive integer and so we're gonna make our own custom type for that and for a Type B it's really just a call well it takes a string and returns whatever type you want so we'll define positive int with s being Astor returning an int and we'll start by doing try value equals ends s except I think it's value error and there's a special type that you can raise in your custom types that will that will signal to the art price framework that you said oh this is problematic and I believe that type is our first argument value error although I think this should be that argument type error I don't know there's two of them one of the other expected integer gods this something like that but now that we've got an integer here we can say we can say what if value is less than or equal to zero raise our first argument value error so here like the type was wrong here the value is wrong expected positive integer got value let's just make it V so that it fits in one lines it's a little bit easier I don't know I probably call this value anyway but this will allow you to kind of validate that and so now if we run this again you'll see Deus equals ten does that if we pass it as a string it'll say you know expected integer got asdf and if we pass what zero really import our cars there are cars so not argument value error maybe there's only one I guess it's only argument type error okay well that makes it simple then ignore the value R then it's always argument type error error cool so now you can say now you'll see it says expect a positive integer got zero that's custom types let's talk about count this is useful if you want to use an argument that might increment or decrement something a good example for that is like a verbose option that you might have in a configuration so you might do parser add arguments both or like dash V usually you put the positional argument first or the short argument first and there's a there's an action so our course has types and actions we've kind of talked about types here and one of the actions is count and if we run this again I'll give it a pass some days you'll see that verbose starts at none this means that it was not passed at all you can also specify like a default of 0 here I usually find that default equals 0 makes the most sense for contacts ends although then you can't tell whether it wasn't passed or not just so that this was always an integer but now if we do you know - - for Bo's here spell verbose you'll see that we have you know verbose of 1 and if we pass a bunch of these you'll see it added one for this and one for each of these short options this is actually a thing that happens with with POSIX options you can change short options together that's how you might do an account option let's actually do boolean options next because they're a little bit simpler to explain or at least I have an example from them and the way you can do boolean options is you again have a custom action so let's say we were implementing like force and there's a store true option or action here and this will by default be false so if we run this again you'll see that worse is false but if we did - worse now you'll see that it sets force to true this is just like a a convenient action there's also store false if you want the opposite and their store Const if you want to store constants and then you would do you know store Const and and cons something anyway and you know to respect your default or whatever there but that's force let's talk about upend think of an example for a pen I don't know let's just make up the one I start out and argument let's say that you know maybe our program can have multiple log files or something and you can do action equals append here and I usually set the default to the empty list here you can set it to or you can leave it out and it'll be none if it doesn't get appended that way you can differentiate but this allows you to collect to have this argument be repeated and you know collect any number of times called so if we do this by default you'll see log as the empty list if we did log you know F 1 dot log now you'll see that we have one item in this list and we can specify this argument many times see now we have F 1 de long and F 2 long because I specified this argument twice that's how the pen works let's talk about choices choices is a way to is a way to constrict the values of a particular option one example that I used and so my code is for a color option if you want to disable or enable command line coloring you can do choices equals you know I think mine is Otto always and ever or something like that if we do the help again you'll see that color automatically gets this little notation that says it has to be one of these three if we passed maybe past color equals wet you'll see that it gives us a nice error message automatically invalid choice wet choose from Otto always or never but if we you know say always here well okay always pass through and that covers most of the like very very basic things I want to cover one last thing which I find pretty useful I actually have to comment all this out though to to do this last little thing which is sub commands and some commands are a way to implement a command line that's kind of like you know kind of like how it works you know what gets status so I'm not gonna get repository of whatever we're you have like get status and like it check out and like all of these sub commands to your main command our course has a way to do that with sub commands and let's see it I believe it's um what is it I look at some code right at this main top I and I believe it's an sub Purser's yes okay so you do some parsers equals or sir dot and some parsers and the destination is actually optional but I usually find this is really useful if you want to query which command you're using so I usually do test equals commands and then you can add your individual commands by doing add parser and this will make in like a nested art purse parser thing so you can do you like subpar spheres dot and parser let's say we're just doing like status will make it similar to get and this will be the status our sir and maybe the status parser has a NS parser not an argument maybe this has a force I don't know Dora true just like a silly example there maybe we also have a check out parser and parser check out parser not add argument maybe this has verbose and you can add individual parsers like that I usually find that it is necessary to do this our sirs dot required equals true this will make it so that a command is required otherwise you get weird errors were like command is sometimes none this is you know of course if your if your command has optional stuff commands them you wouldn't do this and if you're in Python 3.7 or above you can do that here I actually added this feature talking price so that's me but it's not available in Python 3 6 I don't if I recall correctly anyway now that we've done that see - we do TWP hide extra help you'll see that we get these individual commands here and if we were to add help here let's say like hel equals show those days and this one l equals lots of stuff because check out is it's really overloading you'll see that you can actually get descriptions for these sub commands and if we run one of those sub commands and ask for its help you can see that we get the individual arguments of the underlying commands check out yeah so you can see that and you'll see if I run like you know Python 3 T dot PI check out will get just the verbose option because that's the only one that ran whereas if we run like status we'll get a different set of options here so you can kind of think of it as like separate nested argument parsers anyway hopefully this was helpful kind of a kind of a whirlwind of a bunch of information so found this helpful you know leave a comment below or if you want to see other stuff that I could explain also leave a comment to reach out to me on the various platforms but thank you for watching and I'll see you guys in the next one
Info
Channel: anthonywritescode
Views: 20,780
Rating: undefined out of 5
Keywords:
Id: -Sgw-6a1HjU
Channel Id: undefined
Length: 21min 10sec (1270 seconds)
Published: Mon Jun 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.