Skip to content

support type={pathlib.Path,argparse.FileType} without explicit .complete = shtab.FILE #189

@mpkocher

Description

@mpkocher

Requirements:

  • When defining arguments with using pathlib.Path, such as p.add_argument("-s", "--src", type=Path) on an ArgumentParser instance, the autocomplete mechanism should understand the type and emit an autocomplete that is compatible with both a file or directory.

Context/Comments

  • Path is commonly used and communicates the fundamental intent that the argument is PathLike.
  • Currently, when defining File/Dir, the user needs to add .complete (e.g., p.add_argument("-s", "--src", type=Path).complete = shtab.FILE). This is easy to forget and it's not a particulary obvious interface to Action. Also, your text editing won't be able to help or assist you. For example, a simple mistyping of .completion = shtab.FILE can create confusion.
  • Adding .complete doesn't play well with static analysis tools such as mypy in strict mode. Using .complete = ... will yield an error (example.py:29: error: "Action" has no attribute "complete" [attr-defined]
  • Using .complete = shtab.FILE model requires an explicit dependency on shtab. It's useful to make shtab an optional dependency with a try/catch ImportError and use shtab.add_argument_to(p) when defining a get_parser() function.
  • If you don't explicitly add .complete style for a Path, then you loose any "default" autocomplete mechanics of file/dir behavior in the shell.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions