pallets / pallets/click

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

Open
#742 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

parsing
Dominant language
Python
Stars
17.7k
Forks
2.3k
Avg merge
1d 30m
Merged PRs (30d)
18

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the example cmd.py and run the listed command forms to reproduce how grouped short options consume arguments. Trace Click's short-option parsing entry point and determine how an option can require whitespace before its argument while preserving the current default behavior; done means the new configuration works without changing existing forms and is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.