Getopt and optparse vs argparse

  1. Usually, if option -f has a required parameter, -f -b means option -f with argument -b. But in argparse this is an error, missed argument for option -f. You should write -f-b (without space) to pass argument -b for option -f. But this does not work if the value starts with =, because -f=-b mens option -f with argument -b. You should write -f==-b or -f =-b to pass argument =-b for option -f. The latter does not work if the value starts with -, and the former is unexpected, because it differs from the behavior of other Unix/Linux programs.

    Also, the workaround dous not work at all for multi-argument options.

    This is at the core of argparse – they assume that the user missing an argument for option is much more probable that the user passing an argument that starts with -. argparse does not work without such assumption.

  2. Usually, if option -f has an optional parameter, -f b means option -f without argument followed by a positional argument b, but in argparse it means -f with argument b.

  3. In argparse you can interleave options and positional arguments, but not always. For example, in cp-like program you can run cp file -f target, cp -f file1 file2 target, cp file1 file2 target -f, but not cp file1 file2 -f target. The latter parses the second positional argument as target and the last argument as unrecognized.

  4. There are no any tests for this, but in argparse you can add positional arguments after subparsers. I am sure that this is used in user code because there are many reports for the case when it does not work. For example, variable-argement positional parameter never works. Also, subparsers should consume variable number of arguments.

These are just some examples which for sure depends on the argparse design. You can find more on the tracker. Some issues were reclassified from bug reports to feature requests because even if the current behavior is meanless or frankly wrong, it cannot be changed in bugfix releases.

8 Likes