hi there serdar yagilalp here for infoworld in this episode of smartpython we're going to take
a look at how to use command line arguments passed to a python script there's more than
one way to make use of command line arguments in python and so you can choose a method that
suits the level of complexity that you need for your script the most basic way to find out
what command line arguments have been passed is sys.org this is just a list of all the arguments
passed to the script as strings with the name of the script itself as the first item in the list
now if we run this painfully simple example with no arguments supplied at all on the command line
we get one item in the list and that's the script name as a relative path but if we run it again
and pass some arbitrary arguments into it we'll see those show up as other elements in the list
anything separated by spaces on the command line is treated as a separate argument
although anything placed in quotes will be treated as a single argument by itself
now it isn't hard to make use of these in a python program
let's say we have a program that reads files it takes in the name of a file by
way of a command line argument and optionally a starting point from within that file a number
it'll then print out the contents of the file from that starting point or by default from the
beginning of the file so the first argument that we would want to pass to the script would be the
file name that'll show up as the second item in sys.org v for the file position which is optional
we will look for a flag the argument dash s or dash dash start and make sure that that's
immediately followed by another argument which is the position to start at some integer so to
make all this work the first thing we need to do is make sure we have at least one argument
passed in which we'll assume as a file name if we don't get at least one argument we'll bail out
but if we do have that file name argument then we can look through the other arguments for the
start flag so we inspect each element in sys.rgv and if one of the matches are flag arguments we
look at the next element in syst.rgv after that to get the actual value for the start position
now let's run this with just a file name passed to it when we do that we get back the whole file and
0 has been indicated as our start position but if we run it again and supply a start position
we skipped ahead and we can use either dash s or dash dash start to supply starting point
now many of you are probably thinking well this doesn't trap things like an invalid value
for the start position argument or any other number of nitpicks you could make about this
admittedly simplified code and that's all true the thing is that using sys.org v is
a quick and dirty and handy way to grab command line arguments and do something with them but you
are pretty much on your own doing something useful with the results you have to build a lot manually
around it and that can be tricky fortunately python supplies a module in the standard
library to handle a lot of exactly that kind of heavy lifting it's called arg parse and it lets
you create handlers for command line arguments with a lot of other features thrown in for free
so in this version of the same program we're going to use arg parse to take in
the same arguments and process them for us and we start by importing arc parse and creating an
argument parser object with an optional description of what this program is meant to do
you'll see how that gets used in a little bit next we add in the arguments that are to be handled
by the parser the first is file name and we can provide with that a help string that describes it
again you'll see how this works in a moment then we supply an argument handler for the dashes or
dash dash start argument which we will require to be an integer and which will default to 0.
something else worth pointing out here filename is the first argument
positionally it shows up first it's mandatory but start in all of its incarnations is optional
in theory it can appear anywhere in the command line after file name if we had other arguments
and the way arg parse knows this is because the name begins with either one or two dashes that's
the hint to arg parts as to whether this is a positional or an optional argument positional
arguments are just a name optional named arguments take dashes in front of the name
so once we finish defining all our arguments we then use the parse args method to get an
object that has all of the arguments in it we can then access those arguments as object properties
args.file name being the file name and args.start being the start position for the file the rest of
the program works exactly the same as before we're just supplying args and its properties as the
things to use to read in the file and to move to a certain position now let's run this program with
no command line arguments and see what happens we now get with no additional work on our part
a nicely formatted usage prompt that tells us what arguments to provide and which ones are required
that last one comes by the way of an error but again we don't have to define that it's automatic
you'll notice that one of the options that we can pass by the way is
h or dash h we didn't specify that in our arguments list that's another freebie
so if we run this program again with dash h as an argument we get a help description for
the program based on the description we gave the parser and the help prompts that we provided for
each argument and again all this comes with far less work on our part than with messing around
with sys.rgv directly and constructing our own methods for creating all of these things
so now if we run this again passing a file name it will behave as expected from before
and we can run it one more time with the start position passed with the dash s flag
to move to a specific position or again we can use the dash
dash start flag to accomplish the same thing with a little bit more verbosity
so to sum up if you just want to access command line arguments passed to your script and work with
them manually and you don't need any special handling to do it you can use sys.org v and
just read those values directly and wrap your own command line argument handler around them
but if you have a python program that potentially takes in a lot of arguments you can use arg parse
to do that for you and to add contextual help and prompts completely for free on top of it
that's it for this video if you liked it please leave a comment below
don't forget to subscribe to the tech talk channel on youtube and for more smart python
and more dev with serdar be sure to follow me on facebook youtube and infoworld.com