Bug 162007

Summary: ksh-93 doesn't support + command line options
Product: [Fedora] Fedora Reporter: Ian Mortimer <i.mortimer>
Component: kshAssignee: Karsten Hopp <karsten>
Status: CLOSED NOTABUG QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 4   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2005-06-30 13:30:11 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Ian Mortimer 2005-06-29 01:45:46 UTC
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050513 Fedora/1.0.4-1.3.1 Firefox/1.0.4

Description of problem:
The man page for ksh says that + command line options are supported by the builtin
getopts:

getopts [  -a name ] optstring vname [ arg ... ]
              ...
              parameters are used.  An option argument begins with a + or a -.
              An  option not beginning with + or - or the argument -- ends the
              options.
                          ...  The  option letter will be
              prepended with a + when arg begins with a +.

However + options on the command line are not being recognized as options.

Version-Release number of selected component (if applicable):
ksh-20050202-1

How reproducible:
Always

Steps to Reproduce:
1. write a simple ksh script with a getopts option loop
2. run it with a + command line option
3.
  

Actual Results:  The + option is not recognized as an option but is treated as an ordinary argument.

Expected Results:  The + option should have been recognized as an option and prepended with a + as the man page describes.

Additional info:

This simple script shows the problem:

while getopts :n opt
do
        case $opt in
                n) print "-n selected";;
                +n) print "+n selected";;
        esac
done
shift $(($OPTIND-1))
print "arguments: $*"

When run with pdksh or other ksh implementations with a +n option it produces
the output "+n selected" as expected.  When run with the ksh shipped with FC4
(ksh-93) it produces "arguments: +n" from the final print statement.

The POSIX standard doesn't allow + arguments (it seems) but removing it from ksh
breaks backward compatibility and cross-platform compatibility since it's supported in pdksh and commercial ksh implementations.

Comment 1 Karsten Hopp 2005-06-30 13:30:11 UTC
ksh's man page doesn't cover everything, please use getopts own man page:
ksh> getopts --??

You'll find this section:
If the leading character of optstring is +, then arguments beginning with +
will also be considered options.

A leading : character or a : following a leading + in optstring affects the
way errors are handled. 

If you rewrite your testcase as

while getopts +:n opt
do
        case $opt in
                n) print "-n selected";;
                +n) print "+n selected";;
        esac
done
shift $(($OPTIND-1))
print "arguments: $*"

everything works as expected.

Comment 2 Ian Mortimer 2005-06-30 22:29:37 UTC
Thanks for the quick solution.

That works and is portable (although not mentioned in the man pages for pdksh
and other ksh implementations).

However it still breaks backward and cross-platform compatibility since the
syntax without the leading + works with other ksh implementations (including pdksh).