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

feat: Expose clap_lex #3635

Merged
merged 18 commits into from
Apr 15, 2022
Merged

feat: Expose clap_lex #3635

merged 18 commits into from
Apr 15, 2022

Conversation

epage
Copy link
Member

@epage epage commented Apr 15, 2022

This splits out a clap_lex, somewhat inspired by lexopt. The main difference is that lexopt manages all state in one object while we have to split out the state to support:

  • Peeking to know if we should treat a flag as a flag
  • Inserting replacements

This was also done with an eye towards rust-driven completions. This will let the completion engine reuse the same lexer. The parsers will be different for now but as we learn more about a completion parser, we can look to see if and how we can use the same parser between the two. One side effect of this is that we treat -- as potentially a long and - as potentially a short. The problem with this approach is we aren't treating - as a long as well because there is nothing good for us to return. We might need to reconsider that approach due to the inconsistency.

Benefits of factoring out clap_lex:

Deferred:

Fixes #2915

epage added 16 commits April 15, 2022 10:54
Before, we had a generic `next` that provided the next item and peeked
at all remaining items.  This was to work around the borrow checker for
modifying the position while accessing args.

We've now split `Input` into `RawArgs` and `ArgsCursor` so we don't have
overlapping borrows.  This made it so we can split `next` into `next`,
`peek`, and `remaining`.
Since we'll need `skip`, it made me wonder how to name `skip` and
`previous` to fit together, so I decided to play with `seek`.  Its
probably over kill but wondering if its better.
The lexer will soon return `RawOsStr` and it'll cost to turn that into
an `OsStr`.  However, it caches a `str`, so let's just use that.
In considering the design for this, we want:
- Ability to modify the argment list while maintaining the `Cursor` for
  replacements
- Allow picking up subcommand parsing in the middle of short flags
- Ability to peek at the next item to determine if we want to treat it
  as a flag or as a value
- Ability to detect started short and long arguments for completions

Longer term, we also want to consider:
- Allowing users to customize the lexer to support different syntaxes
While figuring out the API, `clap_lex` was tested by clap's tests.  Now
we are focusing on its API directly.
This tempts me to drop our design philosophy but I want to give it more
time.
@epage epage merged commit 7849c35 into clap-rs:master Apr 15, 2022
@epage epage deleted the port branch April 15, 2022 19:00
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

Successfully merging this pull request may close these issues.

[modular] Pull out a clap_lex
1 participant