So here, I have a partially completed version of a
seq() implementation using the
getopt module. So of course, I have to
import getopt here, but you’ll notice that this
USAGE message, the
VERSION message, and then this actual
seq() code here are very similar to previous implementations that you’ve seen.
seq() just takes in a list of integer operands and then a separator with a default of a newline, and then it decides what to do based on how many arguments there are. Now, the meat of this code will be in the
parse() function, which I’ll be writing out in a second. But if you take a look at
main(), you’ll see that
main() gets the arguments from
sys.argv, raises a
SystemExit if there are no arguments—because
seq doesn’t function with no arguments—and then it gets the separator and the operands from this
parse() function, which is where all of the logic of
getopt will need to go.
Then, it just prints out the
seq() of those operands and that separator. So, a lot of this code is very familiar, but this
parse() function will look a little bit different than previous implementations you’ve seen.
that are passed in from the command line. So, what you can do to do that is say
getopt.getopt(), and then you pass in these string
args, and then you need to pass in a string, which contains all of the short options in just one big concatenated string.
And this colon at the end indicates that the option preceeding it takes in an option-argument. So this is
--version, which has no option-argument,
--help, which has no option-argument, and then
--separator, which does in fact have an actual option-argument that it takes.
Then, you need to add in a list of strings here, which are the long forms of those short-form operands that I just showed you. So
"separator", and then you put an equals here for the one that requires an option-argument.
So, this will actually get you the options and the arguments in a pretty convenient form, and it will allow you to perform all of your own logic on the options and the arguments. So this is what I mean when I say that
getopt is a little bit more of a manual configuration option than something like
argparse, because what you’re going to have to do is use what
getopt gives you to actually define the logic of your program. So for example, what I can do here is I can say
for opt, opt_arg in options—
and I actually need to include the double hyphens in front of
"version", as well—so, if the
opt is in those, then I’m used to print out
VERSION, and then I’ll just
0 exit code to show that something worked.
then I’ll do the same thing, so
sys.exit(0). So really, you are performing all of this logic here, as the programmer. And then finally,
if opt in
"p", of course,
"--separator"—then what you actually need to do there is say that the
separator equals the
And then one other thing that would be nice here is to define
separator up top as just a newline, so that that’s the default. Now, I also have these
arguments, which are the operands, and so I can say
if not arguments or len(arguments) > 3,
then I need to print out—or, I can simply raise
SystemExit with the
USAGE message. Otherwise, though, I can just return—in the correct order based on what
main() has said—I should return
separator, and then these actual arguments.
There’s no way in
getopt to quickly and easily define something like a flag, right? Where if the option is present, you just print out the version, right? That’s not something that’s supported by
getopt, but it should work just fine, so let’s see it work.
and that works pretty darn well, too. So, this is a great option.
getopt really works quite well. The thing to remember is just that when you use
getopt, it’s going to be a little bit more of your logic and your code describing what is done in the command line interface, rather than having something that kind of does it for you. So if you prefer a little bit more manual customization and if you want to really make sure that you’re clear on all of the logic that’s happening in the code, this is a great option, and it’s also quite similar to the C library that does the same thing, so if that’s something that’s attractive to you then definitely go for this. Next up, I’ll cover some external libraries that aren’t included in the Python standard library but are still really useful to work with with command line interfaces.
Become a Member to join the conversation.