you should be using argparse
instead of getopt
which is now deprecated. About explanations, the documentation is really accessible:
and about your specific question, I think you'd have your answer there:
(read about this and that to know why you should be using argparse
; even getopt
documentation states (sic) "Users who are unfamiliar with the C getopt() function or who would like to write less code and get better help and error messages should consider using the argparse module instead.")
AFTER LAST EDIT: when in a documentation you see a part of the prototype surrounded by square brackets, by convention, that means that part is optional, whereas the part that's before is mandatory. When you want to call getopt.getopt()
you shall valuate args
and options`.
Now, it is not getopt.getopt(args, options, [long_options])
because it would mean the last comma would be mandatory too, though if you call getopt.getopt(args, options,)
it is not a valid python expression.
AFTER LAST COMMENT: well, that syntax is a convention used across almost every tool existing on unix platform... I don't know if it has been defined somewhere, but I wouldn't be surprised if it was older than the POSIX specification itself! The only piece of "documentation" I could find is the following wikipedia page, but it lacks references:
I found a course at caltech (lookup section "Optional arguments in usage statements") that tells to use square brackets for optional arguments:
And finally you're not the first asking this here on stack overflow, there are at least two other questions on the same subject:
If you look at manpages on your system, you'll see all of them using that syntax, all parameters in square brackets being optional, e.g.: ls
manpage, cat
manpage, even macos' open
manpage uses that convention!
I hope this time I did answer your question!