0

I'm writing some code for a command line program and I'm using the getopt() function. Can someone explain the options / long_options syntax?

getopt.getopt(args, options[, long_options])

My question is this:

Why does is the list fragmented between arguments? why is it options[ not options?

Nirma
  • 5,640
  • 4
  • 35
  • 47
  • Please try and make your titles searchable. If you're querying something specific put it in the title "this function" doesn't help anyone find this question in the future. – Ben Jun 12 '13 at 18:31
  • are you asking why the square bracket includes the comma?! – zmo Jun 13 '13 at 15:37
  • No, why are the brackets being used that way? Why is this legal syntax to call this getopt(args, options[, long_options]) not options,[long_options]. – Nirma Jun 13 '13 at 17:32
  • Basically what is the significance of the square brackets here. This function looks as if the parameter names are args, options[, and long_options] – Nirma Jun 13 '13 at 17:33

3 Answers3

2

The function has 2 required arguments (args and options) and one option that is not required (long_options). The exact meaning of args, options and long_options can all be found in the documentation

Basically, if you want the commandline to be parsed as:

myprogram --foo=bar

Then you need to have a long_options list which looks something like ['--foo='], but if you want to parse it as:

myprogram -f bar

then you would have options set to 'f:'. Of course, you can mix and match as much as you want.

For what its worth, I would never recommend anyone use getopt in favor of optparse or (even better) argparse. These later two modules make working with getopt feel like trying to use a hammer to build yourself a new computer ...

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I understad that part but what is the significance of the way the square brackets are being used? Just the syntax. – Nirma Jun 13 '13 at 17:34
  • @Nirma -- The square brackets mean that the option is not required. – mgilson Jun 13 '13 at 17:48
  • @Nirma -- The best I can do is [this](http://sphinx-doc.org/domains.html#signatures). sphinx is the tool used to generate the python docs. – mgilson Jun 13 '13 at 20:07
0

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!

Community
  • 1
  • 1
zmo
  • 24,463
  • 4
  • 54
  • 90
  • This does not answer my original question at all, Im asking about the syntax of that particular prototype and why its called options[ not options. – Nirma Jun 13 '13 at 17:28
  • If you know where there is some python documentation explaining this syntax please link to it. – Nirma Jun 13 '13 at 17:28
  • in that particular syntax, you're misreading it: the second argument is called `options` followed by `[, long_options])` the square brackets are only there to tell you that `long_options` is optional. What it means is that it can be either `getopt(args, options)` OR `getopt(args, options, long_options)`, depending on your needs! – zmo Jun 14 '13 at 01:00
0

The arguments args and options are mandatory arguments, and long_options is optional argument to the function i.e. it may or may not be provided. You will provide it in your command line utility if you intend to support long options like --format along with -f .

Example of Linux ls utility, showing both options and long options, which start with --

LS(1)                                       User Commands                                       LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information about the FILEs (the current directory by default).  Sort entries alphabeti‐
       cally if none of -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author   <--- LONG OPTIONS OF COMMAND LINE
              with -l, print the author of each file
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175