#100DaysOfWeb in Python Transcripts
Chapter: Days 77-80: Twitter and Slack bots
Lecture: Easy command line interface with argparse
0:01 All right, we're almost done with this exercise. The last step is to use argparse to receive username and project
0:10 from the command line to call this script for any user and project ID. So let's setup a parser. Which is the argparse module
0:27 very useful for any command line application. I import it here at the top and argparse as an argument parser adequately named.
0:40 And here I can give the program a help text which we defined in the constants. So how to tweet your hundred days progress. When we -h of help
0:55 we see this text at the top. And then we have a parser object and we can add one or more arguments. So the first one is -u
1:09 or that's nice you can give it the short form and the long double dash format so I can call my script later with -u or --username.
1:25 I give it a type, with 'str', and it's required. And I give it a help text.
1:49 And I give it a second argument. -p, or long format, --project. That's actually an 'int', it's also required.
2:11 And that will be your hundred days project ID. And again, those match the username and the ID of the grit, which can be variable.
2:26 And then only thing that's left is call args args. Let's try that out.
2:53 So I've not specified username and project which are required, so I get this error message. I still need the -p I can do 'p' or project
3:13 and that gives me project and username args. I can give it a -h and you get this nice help message. Notice the help text we stored in a constant
3:24 and this nice table of options, or arguments. Next we need to log these arguments into the URL. So at the top we defined challenge URL
3:38 and that will be not an fstring but a classic string format where we're going to replace username and project with the variables we get from argparse.
3:48 And the way to do that is URL, this challenge URL and I can use format with named arguments which I always find more readable.
4:03 Might be possible with fstrings but I grew accustomed to this form. So that looked pretty good. So now we should have the URL. Perfect.
4:21 And this is the URL, I mean if I was Pyglite with a grit of 999, I could call the script like this. If it was my other user, or my main GitHub user
4:38 I could call like this, and I always get the right URL. Perfect. So then we're kind of done the only thing I have to undo this URL is equals none.
4:51 And I had it somewhere else you know this is not necessary anymore. The get day tweet will always get the URL not an optional. So if we call it again
5:06 I kind of call it again like this with arguments. So I'll have to call it like this now. And this of course gets me the warning again
5:23 because it's still the first of October. If I run it tomorrow, it will tweet day four. Congratulations you made it to the end it was quite some code
5:33 but I think we have a very useful utility to assist you or somebody else with the hundred days of code to automate the Twitter part.
5:42 That wraps up the videos of day one and tomorrow you get to exercise yourself.