 Hey, hey everyone, thanks for being here Thanks for using me, and I guess you guys already see the topic So let's start first of all Who am I? I'm actually a serious undergrad from Jamia, Amelia, Islamia. It's in New Delhi It's in New Delhi and Okay, so it's in New Delhi my university and I'm a final year computer science undergrad from there, and I'll hopefully be passing out this fall I did my I spent my last two years doing Google Summer Code project with the MetaBrain Foundation Basically MetaBrain Foundation is this open data organization which deals it's sort of like Wikipedia But it stores Data regarding music like artists albums and and it's all crowdsourced so anyone can contribute So for example, you have a CD some old Song CD and the data isn't available online, but you have it in physical form So you can enter all that data to the databases and it gets openly available to users throughout It's now coming into the machine learning field They're beginning with this acoustic brains project which stores all the low-level information of music files so you can do cool stuff like You know maybe generating some classifier for genres and artists that kind of stuff So I spent my last two years with this Foundation working on different projects Next This is my first international trip and talk. So, you know, please pardon me if I say something wrong or etc so How this how did this talk came about I was actually I Just love the command line, you know, and the raw part that comes with it. I also happen to love Python so, you know, I was waiting for a Way to get into CLI apps I used to create, you know, small shell scripts before So one day I was looking for a Package with which I could create a CLI apps in Python and I found this magical thing called doc opt I'm not sure if you heard of it or not. I mean, that's the point of the stock if you haven't heard So, you know, so I found doc opt It felt like I was I was in Hogwarts doing all the cool stuff So, yeah, that's when I decided that I would like to give this talk Okay, I'm not sure if we should even be discussing this thing like I Mean, I'm just assuming that we all love the command line and you know, why would we be writing a CLI app? And I'll honestly be pretty surprised if you know, people are not already doing this Yeah, this is like this matters why why actually spend or bother even bother writing a good command line app And what does it that even mean? What does the word good even mean in this context? You know, why not just write a script that you know, you're just gonna use one time You know so-so called The one-off scripts that you know, you know that you won't be using in them in the future So let's get this done quick and dirty. Why not just do that? I mean if if you know that, you know, you're just gonna use it this one But Gandalf never said that there's no such thing as a one-off script I mean if you need it now Chances are you'll probably be needing it tomorrow the other day. I mean next week or God forbid one year down the line when you don't even remember what the script did or Maybe your friend needs it. I mean, this is free and open source, right? Maybe you put this thing on some gist, etc. And somebody else downloads this and they try to run it I mean imagine the interaction I mean They have this awesome script The CH mod Let's try to run this Oops, I mean you have some hard-coded parts in the script Which is obviously not available on the user's machine, so they try to find some help and Oops because you didn't need you didn't do any of the command line thing I mean it was just a one-off script. You added some parts the thing ran you you were done Or maybe they missed Hell Okay So I mean you just saw what happens when you just Put things online that you you were only meant to do once Etc. So I actually also read this great book by David Copeland It's called awesome CLI apps in Ruby. It's actually not about Ruby I mean to just replace all the Ruby things with Python and That's still great advice. I mean obviously not the source code But the advice there is much more generic and high level. So it's not really Ruby specific I'm actually mentioning this because I won't be going into the details I mean if I were actually adding the slides, I just copy paste its content and talk about it But I won't be going into the details because you know, we are on a times limit here Okay, so the actual details part about this How we actually go about writing a CLI app This is probably why you are actually in this room and you know need the source and Okay, this is obviously a fake Zen number 20 doesn't exist but With Python comes responsibilities. I mean if you were writing this in C I'd get why you couldn't do all the you know the nitty gritty of Comal line parsing you why you would have a just a if else letter of sys arg V Using, you know, if sys arg V is this do something if it's that do something else I could understand if it were in cc plus plus, but this is Python, right? I mean stuff here is as easy as importing anti-gravity. So CLI parsing should be too I was actually planning to do I Mean all these I mean go all the nine yards. There's multiple ways you can do this There are many packages available on pi pi that allows you to create a CLI app But we actually are just going to discuss the standard live module called our parse. I mean it's available it replaces the opt parse module and Doc op which is the real reason I'm delivering the stock Okay, so this is the standard art parse code This is what a minimal art parse application looks like if you tap on those Hairy lines called if under and it may name equals under and their main and then call the main function, etc I mean this exactly won't run anything so You import the argument parser You then initiate an object You so add some argument name here is an argument that you can pass from the CLI We call this parse art function Which gives you a namespace object that you can then refer and use the values or This this basically gives a default value of false Asia You have a name you do the hello thing and you have an output. Let's see Okay, so the script is called say hello We run Basics the default value We run it with some command line and you have the output this I mean if you can We didn't Say anything about the dash-dash have healthy this handled by default by our parse So when you do that you get Just So you do the dash-dash help and the help text it appears so our parse generates all this thing Ultimately This is how you would do this in doc opt basically just it is just in the reverse wherein You copy all this you copy this Help message, you know remove some Parameters, I mean you don't need this. I mean it's just text, etc. And You put it in the doc strings, right? I mean the top a module level doc string you put the usage there and This is just an import you do this magical thing you pass on the document doc string to the doc op and It gives you a dictionary. It's basically no special object. It's just a dictionary which has all the Parameters that you specify you can then use them like you would use any normal dictionary, you know accessing by keys So, I Mean you might not appreciate it on a slide But when things start growing, I mean when you are riding, I mean this is this is just a basic hello world, right? When things start growing the our parse code Sort of gets down to You know multiple of these are at arguments line. You have some Options to I mean, I've never even used our parts ever So I don't even remember but people who do it every day don't even remember the Options with this you need to specify the type of this name and a lot of hearing things but on the other side with doc op you just Ride in As in you do a git git hyphen hyphen help whatever appears you just copy paste this into the doc string you pass it on to the Doc op and it gives it automatically parses all the things and gives you a dictionary back which you can use Anyway, you'd like So I found this quite useful because this syntax there I mean if you have been using CLI for a while, you must be familiar with that what those brackets mean what the Posey, I mean though the angular brackets basically stand for a positional arguments the square bracket brackets are for Optional arguments, so you know if even if you don't specify any of this I mean the script just runs. I mean you haven't specified any of the arguments it uses the default values if you specify an argument this goes into the name and gets used if You're already familiar with the syntax then I mean You can just pick off a pickup doc opt-in like a minute or two that's what it takes to install it from pi pi and Even if you aren't familiar with the syntax there's this site called doc op dot org Which you can go to and it lists all the it's basically a standard It's an ANSI standard way of doing the help text thing so It has all the listed arguments and the it explains all the Square brackets and angular brackets and what they mean Okay, so like Which is this is what I've been trying to explain for a while So yeah Why doc op as a Python package? I mean The doc strings they're already a part of by a big part of Python they help us improve the the readability of the code so Where I whereas in the Parts thing you had to write all of this in the source code But in doc op you can just do do it on the top a user can just read up the doc string and get to know Everything that you come on that your package actually does It has just a simple API, you know sort of like request you always remember that is request dot get post, you know, it's You know that thing you don't have to go to the documentation to see if it was add arguments what function Other functions are there etc So is there's just one entry point to it you pass on the doc string and you get a dictionary back. That's it Yeah, I mean, this is arguably correct. I mean I find it easy to create Comal and logic with this, but some of the people don't I mean for them the syntax is quite new as in the optional and parametric things and Yes, I mean I also Happen to like go. So when I moved there doc opt was also available Basically, I'm not sure I've added the link on this slide, but github.com slash doc opt is an organization which has doc op packages for Most of the languages like there's one for Ruby go Etc. And they all basically follow the same API So you just pass them a double coded string with all the things and It they try to give you whatever represents a representation of a dictionary that language has I mean, that's it Being my first talk, I didn't know how much how many slides to add or etc So I was actually hoping that there wouldn't be any time for questions left, but so it goes Yeah, I actually added some of the things in case I might do it early So these are the standardly modules Get up is basically a See low-level see thing people used to use this. It's also useful in shell, but no, I Hope nobody uses this to use it it today off parts was created way back and this sort of like completely replaced by our parts. We've discussed our parts there are many third-party things and Actually, I found all of these while researching the talk. I mean, I wasn't even aware of this These existing before So we've discussed doc opt Clint Clint is by Kenneth writes. I mean, I'm hoping the request guy Created request package. So you might it's actually more Then just argument parsing. It does a hell lot of stuff like, you know pretty printing Things in ASCII and see a scape code So you get this the thing gets displayed on the command line in colors, etc It has all those things click is what I use over what I personally use now I'm sad that I couldn't get to explain that in detail. I'm not sure if we'll get that but click is by I'm in Ronnacher. I mean it follows the entire flask model I'm not sure if you've used flask before but that thing is magical to and so is click it has basically similar looking API's to flask and I'm just does great stuff. So I personally use click now It takes a little bit more time as compared to doc opt. I mean There's some learning curve involved, but there is a lot of Cool new things that you can do with click that are a bit hard in doc opt I don't even remember what they have does plaque was Is it's actually a new approach? To come online parsing wherein you basically just don't create a parser, etc. You just create a function put in some arguments they can be keyword arguments positional arguments, etc and Pass on that function to a plot plaque and it generates a parser for you Never used it before found this while researching the top Begins has a similar model to plaque and Compego too. Yeah, Compego too. Compego was created by this dev Who actually created it just for delivering a top on creating CLI apps in Python So he created this package and then went on to deliver a talk. I haven't seen it being used much Okay, these might be the contents of my future talk if I ever get to deliver one Basically CLI parsing isn't CLI isn't just argument parsing, right? I mean you get the arguments now what you need to do something Right, it's an application. So argument parsing is just the entry point for a user the user Basically interacts with the app by passing on the arguments It's what come next that determines, you know, what the user experience of your application is so In case of user inputs, it could be like interactive Get that patch interactive, right? I mean, I'm not sure if you've used add interactive, but it goes on to display a Prompt where you can select all files diff them add them, etc and There's this concept of stream-based IO Alex standard out standard a etc. So it's Sort of like if you need to deliver an input to an application From which takes in a file name. We could just pass in hyphen to denote that you will be taking the input from standard in So that's basically a stream-based IO and that matters too In case of output here differentiating between standard out and standard error, right? like The output that's meant to be consumed by other applications Which your application could be piped on to is need is meant to be delivered to standard out The standard or is basically, you know Some find it wrongly named. I don't really have enough Opinions on any opinions on that but they believe that it should have been called like standard Log or something because it's not for it's not really meant for errors It's just meant for anything that you want your user to see and not Applicate other applications to consume So differentiating between those two matters Colors are just fancy, you know, they look good So you need to know about antsy escape codes, etc. But there are packages available in python that allow you to Get colored output across the platform like windows always has different ways of doing things than Mac unit, etc. Etc. So there's this cool package called color ama. It basically you pass on a string You set the foreground background color and it is a cross-platform way of handling colors For progress bar, there's the school package called progress bar to I mean It's great. You just pass on any iterator to the progress bar Function and it all automatically displays a progress bar as you trade to the through the iterator and Yeah, writing man pages. There's this Obscure language of writing man pages. I'm not even sure what it's called because I always you happen to use I'm not sure if you've heard of man dog before you could just write in markdown and it it basically converts any of Given list of formats to a huge Array of output formats so you could write it in markdown and then use band off to create a man page PDF, etc. For it. So that's how I think you should be doing your documentation And this is the real thing Don't have any slides We actually had a OS lab When we did this it was it had to be in C. We couldn't do it in python Because you know props don't like python because it's too short a port. I mean you had you need to do hard work In your lab right and see his hard work python isn't hard work So we had to create a rebel within some of the commands work some dent, etc So but yeah, writing isn't python would be a nice Thank you guys