课程: Python Essential Training

Command-line arguments - Python教程

课程: Python Essential Training

Command-line arguments

- [Instructor] When we run a Python program from the terminal, we type python and then the name of the file we want to run, for example, example.py. But you can also pass in a number of what's called command-line arguments after that. And these command-line arguments can be of two types. A single character proceeded by a dash, for instance, -h, which might stand for help. And if you run it, it usually prints out some documentation about the program along with available commands. Or you can do a longer word proceeded by two dashes, like --help which might do the same thing as -h. And these command-line arguments can also take values. For instance, -i might stand for an input. And then you could have some string of text or let's say a file name like somefile.txt that the program reads in. There are a number of Python modules available to help you read, parse, and use these command-line arguments and values. But my favorite is argparse. Here, I'm going to be writing all of the code inside 11_1_writefile.py, which is obviously not a Jupyter Notebook but a Python script that will be run from the command line. So argparse provides an ArgumentParser class. Let's import that, from argparse, import ArgumentParser. And the ArgumentParser class allows you to create an object that keeps track of all of the arguments your program accepts. So let's make a new ArgumentParser instance called parser, ArgumentParser. And let's create a new command-line argument called output, parser.add_argument --output. And because this argument is a full word, I have two dashes in front of it as is convention. So let's say we want to provide a file name from the command line like this, Python 11_1 --output, that's our new command-line argument, somefile.txt. So how do we get this value somefile.txt? Let's use the parse_args method on our parser instance. Parser.parse_args and let's make a variable called args, set that equal to the output. So this return value args is an object that has all of our arguments as attributes on it. So we can access the file name like this. Print args.output where output is the name of our command-line argument. So now when we run this, we'll see somefile.txt printed out. We can make this argument output required using a keyword argument required equals True. And there's another keyword argument that's helpful here, help. We can provide some help text to the user that lets them know what this command is for. So this might be the destination file for the output of this program. So now, when we run this, and we use the command-line argument h, we see this help text printed out. And you see there's the string that we passed in to that method. And also notice that if we run this without using output, we get an error. The following arguments are required, output. By convention, we want to give the user options for these arguments. So allowing them to either type output or simply -o. So all of the different arguments we want to allow, we can put them in here. And while we're at it, let's create another argument called text, text or -t. It's also required, and let's change the help text, the text to write to the file. And you can probably see where we're going with this. Let's write a couple lines to write this text to a new file. So with open args.output, open it in write mode as f. F.write, args.text, plus, let's add a new line character to the end of this text, just to keep our files clean. And then let's add a print statement at the end, so that the user knows that our program has run successfully. Print, wrote arg.text to file args.output and let's put some double quotes around this to keep this output message nice. All right, and now we can run this program with something like this, -o somefile.txt. And the text is some text to write to the file. So note that I have these quotes around this string and these quotes aren't actually part of the string but any time you have a value with spaces in it, you need to put quotes around the string, so that the terminal doesn't get confused about what's a single value and what's potentially multiple values. So it looks like it ran successfully. Let's go back to our files and see if we can see anything. All right, and a file just popped up, somefile.txt and it looks like we have some text in it. As you can see, we've made some very powerful arguments.

内容