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

File option - prompting with pretty default? #914

Closed
mvolfik opened this issue Jan 8, 2018 · 5 comments
Closed

File option - prompting with pretty default? #914

mvolfik opened this issue Jan 8, 2018 · 5 comments
Labels
Milestone

Comments

@mvolfik
Copy link

mvolfik commented Jan 8, 2018

Hello,
I'm developing simple script, but I have found horrible default when using a file option with prompting with default value...
I have this code:

import click
import os.path
@click.command()
@click.option('-f', default=os.path.realpath("subdir/file.txt"), prompt="Please provide a file", type=click.File('rt'))
def func(f):
    pass
func()

But when I call it, it produces prompt like this:

me@computer:~/directory$ python3 script.py
Please provide a file [<_io.TextIOWrapper name='/home/me/directory/subdir/file.txt' mode='rt' encoding='UTF-8'>]:

Is this solved anywhere or how difficult would it be to fix it, so it would show [/home/me/directory/subdir/file.txt]?

Ubuntu 16.04 LTS 32-bit, Python 3.5.2, click 6.7

Please excuse any imperfections of this issue, I'm quite new to GitHub and haven't written any issues yet...

@tomgoren
Copy link

Can't reproduce on Mac - Python 3.6.2, Click 6.7...

(tmp-314aa1dbdcce094) ✔ ~/.venvs/tmp-314aa1dbdcce094
21:17 $ pip install click
Collecting click
  Using cached click-6.7-py2.py3-none-any.whl
Installing collected packages: click
Successfully installed click-6.7
(tmp-314aa1dbdcce094) ✔ ~/.venvs/tmp-314aa1dbdcce094
21:17 $ python test.py --help
Usage: test.py [OPTIONS]

Options:
  -f FILENAME
  --help       Show this message and exit.
(tmp-314aa1dbdcce094) ✔ ~/.venvs/tmp-314aa1dbdcce094
21:17 $ cat test.py
import click
import os.path
@click.command()
@click.option('-f', default=os.path.realpath("subdir/file.txt"), prompt="Please provide a file", type=click.File('rt'))
def func(f):
    pass
func()
(tmp-314aa1dbdcce094) ✔ ~/.venvs/tmp-314aa1dbdcce094
21:17 $ python test.py
Usage: test.py [OPTIONS]

Error: Invalid value for "-f": Could not open file: /Users/tomgoren/.venvs/tmp-314aa1dbdcce094/subdir/file.txt: No such file or directory
(tmp-314aa1dbdcce094) ✘-2 ~/.venvs/tmp-314aa1dbdcce094
21:17 $ pip freeze
-f file:///Users/tomgoren/.cache/pip/wheelhouse
click==6.7
(tmp-314aa1dbdcce094) ✔ ~/.venvs/tmp-314aa1dbdcce094
21:17 $ python --version
Python 3.6.2

@mvolfik
Copy link
Author

mvolfik commented Mar 14, 2018

Oh, @tomgoren, this isn't meant to be reproduced as is. If you want to:

mkdir subdir
touch subdir/file.txt

Sorry for late reply and writing bad issues...

@tomgoren
Copy link

Okay - I see it now @mvolfik.

The problem seems to be here.

There is only one value returned, f. If we were to return f.name for instance, then what we would get back would only be a string, and not the actual file handler.

So, while the output would be as expected, it would produce the wrong result if you were to try something like f.read(). Test case using your example (I changed click/types.py locally):

22:21 $ python test.py 
Please provide a file [/home/tomgoren/.venvs/tmp-d5c5119e57bcfc5/subdir/file.txt]: 
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    func()
  File "/home/tomgoren/.venvs/tmp-d5c5119e57bcfc5/lib/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/tomgoren/.venvs/tmp-d5c5119e57bcfc5/lib/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/tomgoren/.venvs/tmp-d5c5119e57bcfc5/lib/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/tomgoren/.venvs/tmp-d5c5119e57bcfc5/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "test.py", line 7, in func
    print(f.read())
AttributeError: 'str' object has no attribute 'read'

After digging a bit deeper, there seems to be a whole song and dance between convert, convert_type, prompt, and a few other bits and pieces.

The long and the short of it is that I got a bit lost, and I'm sorry I wasn't able to help more, but it seems like an actual bug, and a tricky one at that. I'm very curious to see how this is solved either way.

Good luck!

@icreatedanaccount
Copy link
Contributor

Starting to work on it

@icreatedanaccount
Copy link
Contributor

PR Created #1309

@jcrotts jcrotts added this to the 7.1 milestone May 23, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants