Skip to content

Commit

Permalink
Support combining short options into one argument
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarbenjamin committed Apr 25, 2012
1 parent dd7aba5 commit ae1cbce
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
15 changes: 10 additions & 5 deletions opster.py
Expand Up @@ -548,8 +548,8 @@ def getopts(args, options, firstarg=False):
arg = args.pop(0)
if arg == '--':
break
elif arg.startswith('-'):
opts.append(pop_option(arg, args, onames, oshorts))
elif arg.startswith('-') and arg != '-':
opts.extend(pop_option(arg, args, onames, oshorts))
elif firstarg:
return arg
else:
Expand All @@ -573,9 +573,14 @@ def pop_option(arg, args, onames, oshorts):
o = onames[name]
else:
short, par = arg[1], arg[2:] or None
if short not in oshorts:
while short in oshorts:
o = oshorts[short]
if o.has_parameter or par is None:
break
yield o, None
short, par = par[0], par[1:] or None
else:
raise UnknownOption('-' + short)
o = oshorts[short]

# pop the next arg if needed
if o.has_parameter and par is None:
Expand All @@ -587,7 +592,7 @@ def pop_option(arg, args, onames, oshorts):
raise ParameterError('options: %r has no argument' % o.name)

# Return Option instance and parameter value
return o, par
yield o, par


# --------
Expand Down
23 changes: 23 additions & 0 deletions tests/opster.t
Expand Up @@ -228,6 +228,29 @@ Now let's test our definitions::
'port': 8000,
'test': 'test'}

As long as only the last option has a parameter We can combine short options
into one argument::

$ run test_opts.py -dDa=b so-what?
{'daemonize': True,
'definitions': {'a': 'b'},
'dirname': 'so-what?',
'listen': 'localhost',
'pid_file': '',
'port': 8000,
'test': 'test'}

The parameter can be in a separate argument::

$ run test_opts.py -dD a=b so-what?
{'daemonize': True,
'definitions': {'a': 'b'},
'dirname': 'so-what?',
'listen': 'localhost',
'pid_file': '',
'port': 8000,
'test': 'test'}

$ run test_opts.py -D can-i-haz fail?
error: wrong definition: 'can-i-haz' (should be in format KEY=VALUE)
Expand Down

0 comments on commit ae1cbce

Please sign in to comment.