typer icon indicating copy to clipboard operation
typer copied to clipboard

✨ Implement list parsing from string with separators.

Open libklein opened this issue 10 months ago • 16 comments

Implements feature #554.

This merge request introduces the functionality to parse List[T] options from strings using a specified separator. An example of how this can be utilized is demonstrated below:

def main(number: List[float] = typer.Option([], multiple_separator=",")):
    print(f"The sum is {sum(number)}")

#  Passing --number 1,2,3 outputs 6  
#  Works with --number 1,2 --number 3 as well. Output would be 6 again.

The implementation works by overwriting the process_value function of the click parser, doing string splitting before forwarding the result to click. This ensures full compatibility with the existing features of both Click and Typer. The feature does not allow whitespace only separators to avoid ambiguities.

Feedback is much appreciated!

Open questions:

  • Currently, error handling is implemented in the constructor of typer.Option, which delays error detection until the command execution. Should we consider moving this error handling to the OptionInfo constructor to allow for earlier failure detection? ToDo:
  • [x] Add to doc_src
  • [x] Write tests
  • [x] Update documentation

libklein avatar Apr 17 '24 22:04 libklein

📝 Docs preview for commit 7eb3fd2c04bcd5ff34aecb1ce83a6f786d1a0f95 at: https://d9b28bcb.typertiangolo.pages.dev

github-actions[bot] avatar Apr 17 '24 22:04 github-actions[bot]

📝 Docs preview for commit cec52c953e60c29e8fac466b9a604295a71fcb31 at: https://0ff1aa3c.typertiangolo.pages.dev

github-actions[bot] avatar Apr 17 '24 22:04 github-actions[bot]

Hi, thanks for your PR! I'll put this in draft as long as the test suite is failing.

svlandeg avatar Apr 18 '24 08:04 svlandeg

📝 Docs preview for commit aca33a5cb1cd8491cba785fd8acaf98855869057 at: https://d79559eb.typertiangolo.pages.dev

github-actions[bot] avatar Apr 18 '24 19:04 github-actions[bot]

📝 Docs preview for commit 46b4690419052cdc4853407e2e08f861e3e0a8f5 at: https://3c495073.typertiangolo.pages.dev

github-actions[bot] avatar Apr 18 '24 19:04 github-actions[bot]

📝 Docs preview for commit c0aba28c94cd2ef2fd57c3c5141131cb14af6f2d at: https://10533715.typertiangolo.pages.dev

github-actions[bot] avatar Apr 18 '24 19:04 github-actions[bot]

📝 Docs preview for commit 6233289c96b18b7903bbe2625b502c28866f77c9 at: https://722bbed7.typertiangolo.pages.dev

github-actions[bot] avatar Apr 18 '24 19:04 github-actions[bot]

📝 Docs preview for commit f09da736a4e4633b52cbb1205e52d8c866dbf850 at: https://39b00efc.typertiangolo.pages.dev

github-actions[bot] avatar Apr 18 '24 19:04 github-actions[bot]

📝 Docs preview for commit 3cdd682244358cf9108291ce8695a93878c46f92 at: https://809b191b.typertiangolo.pages.dev

github-actions[bot] avatar Apr 18 '24 20:04 github-actions[bot]

📝 Docs preview for commit 03558cb210b2c22837ef644a8f671cf575876860 at: https://9744c225.typertiangolo.pages.dev

github-actions[bot] avatar Apr 25 '24 21:04 github-actions[bot]

📝 Docs preview for commit e54193b385614cbd99b9ac4da710374fb8687704 at: https://4d37e64b.typertiangolo.pages.dev

github-actions[bot] avatar Apr 26 '24 19:04 github-actions[bot]

📝 Docs preview for commit 1793f0fcfb3904a5e00b5f3c4ba408c2b9a16024 at: https://919ea834.typertiangolo.pages.dev

github-actions[bot] avatar Jul 24 '24 13:07 github-actions[bot]

📝 Docs preview for commit ae11002c487bf202146e5a9f5a746fc26c0a3753 at: https://411417d0.typertiangolo.pages.dev

github-actions[bot] avatar Jul 24 '24 13:07 github-actions[bot]

📝 Docs preview for commit de078a64c8e3f77a0ff74fbf5c51e807f319a287 at: https://e3977423.typertiangolo.pages.dev

Modified Pages

  • https://e3977423.typertiangolo.pages.dev/tutorial/multiple-values/multiple-options/

github-actions[bot] avatar Aug 09 '24 09:08 github-actions[bot]

Is there anything I can contribute?

libklein avatar Aug 09 '24 22:08 libklein

At this point the PR is in a good shape to be reviewed by Tiangolo - we've got this on our internal board for him to review when he has time 😉 Thanks for your patience!

svlandeg avatar Aug 12 '24 08:08 svlandeg