Skip to content

Allow for prohibiting the no-blank way of specifying short option with option argument #742

@andy-maier

Description

@andy-maier

The command line parsing implemented by Click allows for specifying a short option followed directly by its option argument without an intervening blank. Click also supports grouping short options behind a single hyphen. These two features together (although they are backed by the POSIX-1.2008 "Command Syntax Guidelines") result in a confusing number of ways to specify short options, that can easily be misunderstood.

Request: I'd like to have a way to define a click option such that it requires at least one whitespace between the option name and its option argument. The default should be the current behavior, so that existing commands behave the same as they do today.

I make this request on the grounds that the "no-blank" rule in the POSIX-1.2008 "Command Syntax Guidelines" was added "to ensure continued operation of historical applications". I think this positioning calls for a way to make this behavior configurable in a parser like Click.

Here is an example script cmd.py, and further down some ways to specify its command line, in order to illustrate why I think it is confusing:

import click

@click.group(invoke_without_command=True)
@click.option('-h', '--host', type=str, default="localhost", metavar="HOST",
              help="Hostname or IP address. Default: localhost.")
@click.option('-v', '--verbose', is_flag=True, default=False,
              help='Verbose mode.')
@click.pass_context
def cli(ctx, host, verbose):
    print("Debug: host=%r, verbose=%r" % (host, verbose))

if __name__ == '__main__':
    cli()
$ cmd.py -h myhost
Debug: host=u'myhost', verbose=False

$cmd.py -hmyhost
Debug: host=u'myhost', verbose=False

$cmd.py -v -h myhost
Debug: host=u'myhost', verbose=True

$cmd.py -vh myhost
Debug: host=u'myhost', verbose=True

$cmd.py -vhmyhost
Debug: host=u'myhost', verbose=True

$cmd.py -hvmyhost
Debug: host=u'vmyhost', verbose=False

The last example is probably not very intuitive to most people, and thus causes confusion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions