FilepathPrompt - kazhala/InquirerPy GitHub Wiki
This page is deprecated, documentation moved to: https://inquirerpy.readthedocs.io/
An input prompt with pre-built filepath completion.
class FilePathPrompt(InputPrompt):
def __init__(
self,
message: Union[str, Callable[[SessionResult], str]],
style: InquirerPyStyle = None,
vi_mode: bool = False,
default: Union[str, Callable[[SessionResult], str]] = "",
qmark: str = "?",
multicolumn_complete: bool = False,
validate: Union[Callable[[str], bool], Validator] = None,
invalid_message: str = "Invalid input",
only_directories: bool = False,
only_files: bool = False,
transformer: Callable[[str], Any] = None,
filter: Callable[[str], Any] = None,
**kwargs,
) -> None:
Classic Syntax (PyInquirer)
from pathlib import Path
from InquirerPy import prompt
from InquirerPy.validator import PathValidator
questions = [
{
"type": "filepath",
"message": "Enter file to upload:",
"name": "location",
"default": str(Path.cwd()),
"validate": PathValidator(is_file=True, message="Input is not a file"),
"only_files": True,
},
{
"type": "filepath",
"message": "Enter path to download:",
"validate": PathValidator(is_dir=True, message="Input is not a directory"),
"name": "destination",
"only_directories": True,
},
]
result = prompt(questions)
Alternate Syntax
from pathlib import Path
from InquirerPy import inquirer
from InquirerPy.validator import PathValidator
src_path = inquirer.filepath(
message="Enter file to upload:",
default=str(Path.cwd()),
validate=PathValidator(is_file=True, message="Input is not a file"),
only_files=True,
).execute()
dest_path = inquirer.filepath(
message="Enter path to download:",
validate=PathValidator(is_dir=True, message="Input is not a directory"),
only_directories=True,
).execute()
REQUIRED
The question message to display/ask the user.
When providing as a function, the current prompt session result will be provided as a parameter. If you are using the alternate syntax (i.e. inquirer
), put a dummy parameter (_
) in your function.
from InquirerPy import inquirer
def get_message(_) -> str:
message = "Name:"
# logic ...
return message
result = inquirer.filepath(message=get_message)
An InquirerPyStyle
instance. Use get_style
to retrieve an instance, reference Style documentation for more information.
If you are suing classic syntax (i.e.
style
), there's no need to provide this value sinceprompt
already sets the style, unless you would like to apply different style for different question.
Enable vim
keybindings for the filepath
prompt. Its exactly the same as if you enable vi_mode
in bash/readline. Checkout Keybindings documentation for more information.
If you are suing classic syntax (i.e.
prompt
), there's no need to provide this value sinceprompt
already sets sets this value, unless you would like to applyvi_mode
to specific questions.
Set the default value of the filepath
prompt.
When providing as a function, the current prompt session result will be provided as a parameter. If you are using the alternate syntax (i.e. inquirer
), put a dummy parameter (_
) in your function.
The question mark symbol to display in front of the question.
Change the auto-completion UI to a multi column display, checkout the gif demo in InputPrompt page.
Provide the validator for this question. Checkout Validator documentation for full details.
Configure the error message to display to the user when input does not meet compliance.
If the validate
parameter is a Validator
instance, then skip this parameter.
Only populate files in the auto-completion.
Only populate directories in the auto-completion.
A callable to transform the result. This is visual effect only, meaning it doesn't affect the returned result, it only changes the result displayed in the prompt.
Note: filter
function won't affect the answer passed into transformer
, transformer
actually run before the filter
function.
result = inquirer.filepath(
message="File to upload:", default="sample.txt", only_files=True
) # UI -> ? Name: sample.txt
result = inquirer.filepath(
message="File to upload:",
default="sample.txt",
only_files=True
transformer=lambda result: result.replace(".txt", ""),
) # UI -> ? Name: sample
A callable to filter the result. Different than the transformer
, this affects the actual
returned result but doesn't affect the visible prompt content.
result = inquirer.filepath(
message="File to upload:", default="sample.txt", only_files=True
) # result = "sample.txt"
result = inquirer.filepath(
message="File to upload:",
default="sample.txt",
only_files=True
filter=lambda result: result.replace(".txt", ""),
) # result = "sample.txt"