Anatomy of a Command Line Interface
00:08 Because a command line interface is built to always take in some kind of input from a user, the programmer has to do a little bit more work to deal with things like parsing and validation. When you read a command line interface, you first have to verify the argument structure to see whether it fits the standards that you as the programmer have designed for that command line interface.
00:30 This is what you normally do with parsing: determine the contents, the order, deal with things like mutual exclusivity, and so on. Then, you should normally be checking for some kind of type validation. In Python, this is a bit nebulous because Python is not a super strongly typed programming language already, but you do have to check whether all of the arguments have the correct type at least to some basic level, right?
All of the arguments that are supposed to be numbers have to be parseable numbers, you know? If you had an argument that was supposed to be a number and someone just put in
"cat", it wouldn’t be clear at all what that was supposed to mean as a number.
01:03 You have to do some form of type validation. And then, finally, you have to do behavioral validation, and this is something that you should be doing in really any well-written Python program, anyway. This is stuff like checking whether filenames are actually valid files, or whether things that are supposed to be a member of an enumerative set are actually members of that set as opposed to being some random input. And then, of course, after all of this comes the real logic of your actual program that you’re supposed to run with the command line interface. Over the next few lessons, I’ll show you several different ways that you can implement parsing for your command line interface, and then I’ll also show you a little bit about type validation.
The behavioral validation is something that I’ll cover more incidentally as I go through the various code files: how to handle things like
FileNotFoundError, or set membership problems, or something like that.
Become a Member to join the conversation.