Argparse for CLI - Intermediate Python Programming p.3
Skills:
Prompt Craft80%Prompt Systems Engineering70%Advanced Prompting60%Prompting Basics50%Tool Use & Function Calling50%
Key Takeaways
This video tutorial series covers the use of Argparse for creating Command Line Interfaces (CLI) in Python, including argument parsing and validation, with a focus on best practices such as following the PEP8 style guide. It demonstrates how to use Argparse to add CLI options to a program, handle multiple arguments with default values, and print output to the console using sys.stdout.
Full Transcript
what's going on everybody and welcome to part three of our python fundamentals that art Basics tutorial Series in this video we're going to be talking about ARG pars for making CIS or command line interfaces so first of all why might you actually want to make a command line interface so to some degree it can make like interacting with your own program a little easier you know you can just call it from the command line and you can change variables just-- variable name equals boom it's changed you don't have to open and edit the file and all that so it can be kind of like it can be quicker to tweak things and rerun it really quickly also I think command line interfaces are exceptionally useful for anytime you're going to set up something like a Cron job or something like that that you want to maybe tweak that uh some sort of variable but also run the script via the system so maybe a Chron job or maybe even you're you're running an os. system call in your script or something you know and you want to change a variable or do something uh having your program be and have CLI options is useful so let's go ahead and cover them so for now we've got this really basic function it just calculates X and Y and does some sort of operation so the if the operation is add it adds them and so on obvious actually never mind we'll talk about it later but you know these methods and stuff if you were going to actually make a a function like this you wouldn't you wouldn't write this but anyway uh so what we're going to do is is convert this into a command line interface using art Parts uh also you don't have to write up this entire function some of you are probably like oh my gosh now you can go to the text based version of this tutorial and uh copy this code I'll either link to the tutorial in the description or copy and paste the code in the description or both or who knows the video is not out yet so I don't know we'll see it'll be a surprise so so let's convert this to command line interface so the first thing that we're going to do is we're going to import arars and we're going to import CIS so then we're going to do Define Main and then main is where we're going to actually build the arguments themselves as you might guess from the name ARG parse it's an argument parser surprise so what we're going to do is we're going to define the parser and the parser is equal to AR parse do uh argument parser one quick note on um pep 8 when I see the following AR parsa argument parser I am I can assume argument parser is a class and parser equals is an object of that argument parser class how do I know because of the studly case again that's just that's one reason why you want to sometimes adhere to Pepe because it's clear to me immediately when I know like that's the name of that okay it's a parser object it's not some sort of function that's returning some sort of value that we're calling parser moving along uh parser do add argument add argument again because we know parser is an object we know add argument method if that doesn't make much sense to you right now it will soon because we're going to be talking about objectoriented programming soon moving along uh the first argument We'll add is just d-x that's going to take the place of this x parameter so that's the name of it the type we're going to say is a float uh then we're going to say default equals 1.0 and then we're going to come down and we're going to add um some help and the help is just uh like a some sort of string that's going to explain what this is so we're just going to say what is the first number and I'm just going to copy this and paste and paste and the second parameter or argument will be why it's not the first it's the second number we'll stay stick with the default one uh and then the final one is Operation the type is not float it's a string and then we're going to say um what operation to um let's just put parentheses add sub or div okay um I'll show you how help can be utilized uh soon so those are the arguments now we're going to say the args is equal to parser parse the args and then um we're just going to cy. S standard output. write the string version of count args okay so this is all necessary to do with ARG pars and then the system out the reason why we're doing this is um so the output will actually come to the to the console itself um in many cases you could just use print uh but there's going to be times when actually you need to do a system out uh because otherwise you're just not going to see it anyway so then we're going to Output it now you can see that actually rather than passing XY operation we're passing we're just passing args that's it so as you might guess we actually need to change this now is args then you could either do oper operation um equals args do operation and then do that for X and Y I wouldn't recommend getting the habit of doing that because basically you're you're like you're D you're you're multiplying that variable whatever the contents of that variable art boom this got doubled um and also I didn't really fully explain in the last tutorial but with like string formatting and concatenation when you use the plus sign to add things together you're making multiple copies of that that string whatever it is that's why it doesn't scale very well especially if you were doing like a large concatenation process uh that would not be um favorable anyway um we're not going to use this you could use this uh it might make the function more readable I don't think it does but you might and if you disagree feel free it's not that big of a deal so instead I'm actually just going to put ARS dot in front of everything boom boom okay so that's all we need to do there now pretty much I think we're done actually so we're going to save that and I'm going to pull up a um command window that's not very big let's see if I can edit this really quick I think I do it in properties huge all right so now we're going to actually run this in most cases you can probably just type python that'll work for you I've got multiple versions of python so I'm going to specify I want uh python 35 or 3.5 and then python uh and then I'm running 3. capital A really I'm pretty sure that's what it's called Art Parts okay cool we'll go with that I don't know why 3. space capital A did not work but that's fine so let's just run it once without doing anything up C oh we're still calling oh oh my gosh Come on here okay so actually at the very end we forgot something pretty important Also let's do this pep8 specifies one white space between functions two between classes so we'll honor that while I'm thinking about it if name equals main always a good idea this just basically ensures that if this is the actual script that's being run we run this function otherwise we don't run it so if we were to import this for whatever reason it wouldn't just run okay save that uh or I think we're I think we're ready to rumble now let's try again uh this time we get none that's not what I wanted I want the output of the RS default is yes RS why did you give me none oh the default for the operation uh we need to make that something we'll we'll do add for now seriously did I not okay we got rid of the comma we'll get there we'll get there we're on our way boom successful like eighth times a charm good stuff so 2.0 that's the default awesome but what we can start to do instead is say DSX is = 5 d-y = 2 and then the operation Operation equals um we'll say mole for multiply and we get 10.0 instead pretty cool we can also do dashh just a single Dash and what we get is basically the help output so it just tells us basically you know what we're trying to run what our various options are so we know that d-x corresponds to X and Y and then operation and then we've got optional arguments here for H and help this would just show basically the help message um and then we've got the the parameters that we added or the arguments that we added okay so that's about all I want to show with ar Parts it's just a nice clean way to parse arguments as opposed to using like CIS for getting your arguments because you can do that you can get arguments from the command line using actually just CIS um but it's one it's going to be required that you do that and you do all of the arguments that you're expecting uh well I guess you you could make a function and get around it but the the CIS module is going to want that of you it's going to want something for all those arguments but I guess we we've made defaults so I suppose it's the same thing but anyways ARG parse is just a much more friendly way to to parse arguments from the command line than to use CIS the only reason we've got CIS here is just to Output from um from the function so that's all for AR parse if you have any questions comments concerns whatever feel free to leave them below otherwise in the next tutorial we're going to be talking about list comprehension and generators so stay tuned for that thanks for watching
Original Description
Welcome to part 3 of the intermediate Python programming tutorial series. In this part, we're going to talk about the standard library called argparse. Argparse is a parser for command-line options, arguments, and sub-commands.
Argparse tutorial: https://pythonprogramming.net/argparse-cli-intermediate-python-tutorial/
In the basics series, we actually just used the sys library, which can also work, but isn't quite as extensive.
Having even just a very basic command-line interface (CLI) for your program can make everyone's life easier for modifying parameters, including programmers, but also non-programmers. A CLI for your program can also make it easier to automate running and modifying variables within your program, for when you want to run your program with a cronjob or maybe an os.system call.
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Prompt Craft
View skill →
🎓
Tutor Explanation
DeepCamp AI