question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ignore/pass over the double dash (--) marker

See original GitHub issue

I’m working on a command line application which includes something like ssh wrapper. Basically what I’m doing is I’m resolving hostname myself, but then I want to pass all other parameters to the underlying ssh command. However, I’ve noticed that Click is consuming the first double dash (--) marker.

I was hoping Click has somewhere a variable containing all unprocessed arguments, but apparently, that’s not the case, or at least I couldn’t find it. So, in the end, I ended up adding one more click.Context option called ignore_end_of_the_opts_marker that tells Click to pass over the double dash, so if you combine it with ignore_unknown_options, you can pick all unprocessed arguments.

Eg.:

@click.command(context_settings=dict(
    ignore_unknown_options=True,
    ignore_end_of_the_opts_marker=True
    ))
@click.argument('args', nargs=-1, type=click.UNPROCESSED)
@click.pass_context
def cli(ctx, args):
    print('All the arguments:', args)

I’ve noticed some people are also writing wrapper-like tools, so maybe more people would be interested in this option.

So far, I’m not sure if it’s worth a Pull Request, let me know if it is, I’ve tried to document everything consistently with the rest of the code, but I can imagine there could be a better name for this option. Or maybe you have a better idea of how to solve this issue.

Anyway, if someone is interested, here is my fork: https://github.com/Higgcz/click

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
POD666commented, Oct 25, 2021

I end up with the following snippet:

class HandleDoubleDashArg(click.Group):
    def parse_args(self, ctx, args):
        if "--" in args:
            # concat all options that follow double dash into tuple
            double_dash_idx = args.index("--")
            args = args[:double_dash_idx] + [tuple(args[double_dash_idx + 1 :])]
            # ['--arg', '123', '--', 'qwe', 'rty'] -> ['--arg', '123', ('qwe', 'rty')]
        elif "--help" in args:
            pass  # skip if help is requested
        else:
            # trick: feed an empty string as positional argument
            # so that the latests from args is considered as child command

            # find child command
            for child_command in self.commands:
                if child_command in args:
                    idx = args.index(child_command)
                    args.insert(idx, ())
                    # ['--arg', '123', 'my_child_command'] -> ['--arg', '123', (), 'my_child_command']
                    break
            else:
                # TODO: to be improved
                args.insert(-1, ())
                # ['--arg', '123', 'qwerty'] -> ['--arg', '123', (), 'qwerty']
                # -> should lead to `Error: No such command 'qwerty'`
                # ['--arg', '123', 'qwerty', '--qwe'] -> ['--arg', '123', 'qwerty', (), '--qwe']
                # -> Error: No such command '()'.
                # ['--arg', '123', 'qwerty', '--qwe', '--rty'] -> ['--arg', '123', 'qwerty', '--qwe', (), '--rty']
                # -> Error: No such command '--qwe'.
        super(HandleDoubleDashArg, self).parse_args(ctx, args)


@click.group(cls=HandleDoubleDashArg, invoke_without_command=True)
@click.option("--arg")
@click.argument("double_dash_args")
@click.pass_context
def my_group_command(ctx, double_dash_args):
    # ...
    if ctx.invoked_subcommand is None:
        # ...
        pass


@my_group_command.command()
def my_child_command(ctx):
    # ...
    pass

It’s limited to my use case but might be helpful.

0reactions
davidismcommented, Feb 27, 2022

Pretty much a duplicate of #619, see additional discussion there. Just like if you need to pass an argument that looks like an option, use -- if you need to pass a -- as an argument.

wrapper -- ssh -v -- bash -c ...
Read more comments on GitHub >

github_iconTop Results From Across the Web

What does "--" (double-dash) mean? - Unix Stack Exchange
More precisely, a double dash ( -- ) is used in most Bash built-in commands and many other commands to signify the end...
Read more >
Two-Way Traffic Markings Long Descriptions - FHWA MUTCD
Passing Permitted is a two-way marking with passing permitted in both directions." A two-lane roadway is shown with a centerline marking of a...
Read more >
RangeSlider | Dash for Python Documentation | Plotly
RangeSlider is a component for rendering a range slider. Users interact with a dcc.RangeSlider by selecting areas on the rail or by dragging...
Read more >
HTML list-style-type dash - css - Stack Overflow
90. since dash is commonly used in lists why it's not included by default in css? – temirbek · 122. more importantly, why...
Read more >
Mario Kart: Double Dash!! - Bowser Skip Tutorial - YouTube
Mario Kart: Double Dash !! - Bowser Skip Tutorial. 9.1K views 7 years ... WE LOSING OUR FRIENDSHIP OVER THIS GAME | Mario...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found