Skip to content

Wrong argument handling when specifying 'ignore_unknown_options' in combination with options #602

Closed

Description

@cbandera

I am having trouble with argument handling when specifying the 'ignore_unknown_options' in combination with options.

In this specific case, I need to use the 'ignore_unknown_options' flag in order to pass a unknown amount of arguments AND options to an underlying process. (And prevent click from interpreting words beginning with a dash '-' as options).

At the same time, whenever you have an option specified with a letter, click will still steal this single letter from the argument.

Consider this minimal example:

import click

@click.command(context_settings=dict(ignore_unknown_options=True, ))
@click.option('-y', '--default_yes')
@click.argument('compiler_args', nargs=-1)
def cli(  default_yes, compiler_args):
        print(compiler_args)

When called with
cli -DCMAKE_CXX_FLAGS=-Wreturn-type
it will print
(u'-DCMAKE_CXX_FLAGS=-Wreturn-t',)
and cut the rest off.

Activity

  1. davidism commented on Feb 27, 2022

    @davidism
    Member

    Use -- to turn off subsequent option parsing. All tokens after the -- are treated as arguments.

    cli -- -DCMAKE_CXX_FLAGS=-Wreturn-type
    

    The specific behavior reported is a weird ambiguity with how a few different features of the parser. Since short options can be combined, and unknown options were ignored, the short option parsing scans each character in the token to see if it's individually a valid option, and collects all unknown options, recombining them at the end as an extra argument. Since -y is a valid option, and none of the other characters are, -y gets collected and removed from the rest.

  2. locked as resolved and limited conversation to collaborators on Mar 14, 2022
  3. added
    parsingParsing, parameters, commands, chaining, context
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    parsingParsing, parameters, commands, chaining, context

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions