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

core_commands: split lexical arguments with shlex #2344

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

slook
Copy link
Member

@slook slook commented Jan 25, 2023

  • Added: Accept "long strings with spaces" as quoted arguments, but don't require quotes if arguments are unambiguous
  • Added: Support long "user names" with spaces for the /msg and /usearch commands
  • Added: Support long "room names" with spaces for the /say command
  • Added: A third argument in the /sample command to test this implementation with 3 arguments

Lexical (shell-like) argument splitting allows support for quoted "user names". Quotes are not mandatory if the argument is the first or last one, nor if the username is a single word, i.e; the following inputs all validate into 2 arguments:

/msg "user name" "message text" <- both arguments quoted, ok.
/msg user name "message text" <- only final argument quoted, ok.
/msg "user name" message text <- only first argument quoted, ok.
/msg username message text <- no quotes required for single-word usernames, ok.

Review/testing points:

  • 3 or more num_required_args are tested working with the new split_args() function. The /msg only needs to accept two arguments, but other commands might need more.

  • Only un-quoted arguments will be split. Quoted arguments are always protected from splitting. A helpful error message is shown on the interface in the case of a mismatch, such as "Too many arguments".

  • Setting num_required_args=0 returns unlimited splits, and thus will split all un-quoted arguments as separate words, as per the standard split function without maxsplit, or maxsplit=-1, but the quoted phrases will be together as one argument.

  • It is very likely that the <user> argument will contain spaces, because many users have such long names. Some commands, such as /usearch for example) are useless without support for arguments that contain spaces.

  • The proposed implementation is a useful utility that should be available to plugin developers to process any string in the way they want, with any amount of required arguments (such as perhaps from secondary input() prompt or another source).

  • Closer integration for argument parsing might be considered into the pluginsystem core, but caution is needed to ensure that optional arguments don't throw an error that prevents the plugin command from being triggered.

  • If there is a combination of required arguments plus two or more optional arguments then arguments can be ambiguous, but having one optional argument (such as in most private chat commands) in the final position would be okay.

  • Strings containing 'inline "quotes" can be "protected" with alternate quotation marks', and other features, but unfortunately there is no maxsplit feature, which is why we need to extend the shlex function such as done in this PR.

See shlex module Python docs for details.

slook and others added 2 commits January 25, 2023 18:42
Lexical (shell-like) argument splitting allows support for quoted "user names".
@slook slook changed the title core_commands: msg command and shlex split core_commands: msg command with shlex split Jan 29, 2023
@slook slook changed the title core_commands: msg command with shlex split core_commands: split lexical arguments with shlex Jan 30, 2023
@slook
Copy link
Member Author

slook commented Feb 8, 2023

help sample
Listing 1 available commands matching "sample":

Nicotine+ Commands:
	/sample, /demo <choice1|choice2> <second argument> [something else..]  -  Sample command description

sample choice1 second arg something else
Hello, testing 3 arguments: >choice1<  >second<  >arg something else<

sample choice1 "second arg" something else
Hello, testing 3 arguments: >choice1<  >second arg<  >something else<

sample choice1 second arg
Hello, testing 3 arguments: >choice1<  >second<  >arg<

sample choice1 "second arg"
Hello, testing 3 arguments: >choice1<  >second arg<  >False<

sample choice1 second arg "something else"
Hello, testing 3 arguments: >choice1<  >second arg<  >something else<

sample choice1 "second arg" "something" "else"
Too many arguments, 3 required, 4 given (3 quoted)
Hello, testing 3 arguments: >False<  >False<  >False<

sample choice1 "second arg" 'some "thing" else'
Hello, testing 3 arguments: >choice1<  >second arg<  >some "thing" else<

sample choice1 'the "second" argument' something else
Hello, testing 3 arguments: >choice1<  >the "second" argument<  >something else<

sample choice1 "second mismatch quoted arg' something else
No closing quotation
Missing <choice1|choice2> argument
Usage: /sample <choice1|choice2> <second argument> [something else..]

sample choice1 "second mismatch quoted arg' something else"
Hello, testing 3 arguments: >choice1<  >second mismatch quoted arg' something else<  >False<

sample choice1 "second mismatch quoted arg' something" else
Hello, testing 3 arguments: >choice1<  >second mismatch quoted arg' something<  >else<

@slook slook marked this pull request as draft February 27, 2023 12:47
@mathiascode
Copy link
Member

I intend to merge this for 3.3.0, just haven't gotten around to reviewing the changes yet.

@slook
Copy link
Member Author

slook commented Aug 8, 2023

I intend to merge this

The actual split_args() method works alright, but I don't like the style of implementation, it needs to be refactored before considering for merging.

The explanatory code comments are only there to help draft code review and will be removed once ratified..

@mathiascode
Copy link
Member

Postponing this in order to release 3.3.0 sooner, hopefully we can get it into a 3.3.x point release.

@mathiascode mathiascode added this to the 3.4.0 milestone May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants