Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for # comments #112

Open
thomask77 opened this issue Jan 26, 2024 · 5 comments
Open

Support for # comments #112

thomask77 opened this issue Jan 26, 2024 · 5 comments

Comments

@thomask77
Copy link

thomask77 commented Jan 26, 2024

When feeding prepared scripts to click-repl, it would be useful to support # comments.

shlex already supports this feature, but click-repl explicitly disables it (why?):

As a workaround, I vendored click-repl and commented out that line.

Additionally, this needs a check to skip processing if the resulting args are empty:

...
             if args is None:
                 continue

+            # 2024-01-24, tk:
+            #
+            # Also continue if args is empty to skip lines that
+            # consist of white space, or are commented out.
+            #
+            if not args:
+                continue
+
@thomask77
Copy link
Author

thomask77 commented Jan 26, 2024

Follow up:

I think split_arg_string and its newly-enabled comment handling should also be used before handle_internal_commands.

Otherwise :help works, but :help # call internal help does not.

@GhostOps77
Copy link
Contributor

I am the one who removed the ability to add comments in the prompt.
and It's because that's how click parses it too.
I just copied that function straight out from here

So, just to parse the args in the same way that click does, I have to do it.
Maybe you can alter the behaviour of handle_internal_commands function to remove the comments before execution.

@GhostOps77
Copy link
Contributor

ok, now whats the behaviour that you want to expect from this utility @thomask77 ?
what are the right scenarios to include and exclude comments?
I'd say enabling comments to pass in as input for normal click commands seems fair
cuz the input can sometimes expect the # character sometimes

do you have any other way to deal with this?

@thomask77
Copy link
Author

thomask77 commented Feb 16, 2024

Hmm, ok.

I see that came from the built-in shell_completion.py part of click.
That's a special case, and parsing with lex.commenters = "" is probably correct there.

Normally click does not parse (or see!) the raw command string, but only the args[] array.
The comments are already removed by the shell.

But in case of click_repl, we are the shell and have to remove comments before invoking click.

@thomask77
Copy link
Author

When you do

myprogram foo bar # this is a comment

in bash, myprogram only ever sees sys.argv = ["myprogram", "foo", "bar"].
The comment is stripped by the shell, and is invisible to click.

(If you want to pass a argument containing a #, normal shell escape rules apply. Use # or enclose in " " / ' '.)

click-repl should match this behavior, and also remove the comment before any further processing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants