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

Documentation for checking if '-' has been passed to File param #1511

Closed
guludo opened this issue Mar 26, 2020 · 2 comments
Closed

Documentation for checking if '-' has been passed to File param #1511

guludo opened this issue Mar 26, 2020 · 2 comments
Labels

Comments

@guludo
Copy link

@guludo guludo commented Mar 26, 2020

Hi there,

This is not a bug report, but rather a documentation question. Parameters of type File interpret - as stdin or stdout depending on the mode. My question is: how to check if - has been passed to my parameter?

With the current version installed in my environment, I checked that the following works:

@click.command()
@click.argument('f', type=click.File())
def command(f):
    if f == sys.stdin:
        click.echo('input is stdin')

However, as there are other ways of opening stdin (instead of using sys.stdin directly) and the documentation does not guarantee that the value of the parameter will be sys.stdin for the "-" input, I'm not sure if this code is safe regarding future releases of Click.

Environment

  • Python version: 3.8.2
  • Click version: 7.1.1
@davidism davidism added the docs label Jun 10, 2020
@davidism
Copy link
Member

@davidism davidism commented Aug 14, 2020

I don't think this is in scope for Click. You would check in whatever way you would check in Python in general, by examining some theoretical property of the file object. In most cases, the name of the file will be the special value <stdin>, but I can't say that's guaranteed given anything can replace sys.stdin, such as PDB, PyDev, pytest, IDLE, etc. All you get from File is an open stream, and I'd suggest that that it shouldn't matter where it's pointing at that point.

@davidism davidism closed this Aug 14, 2020
@jab
Copy link
Member

@jab jab commented Aug 14, 2020

Just chiming in to add that another property of a file that you can inspect to check whether it probably corresponds to the process’s original stdin is its .fileno() value. By convention, the three file descriptors 0, 1, and 2 represent standard input, standard output, and standard error, respectively. I think this should work even if something like pdb, pytest, etc. reassigns the corresponding sys attributes. However, it will not work if something dup’s and then closes the original stdin, but maybe that’s a feature and not a bug depending on what you’re trying to do.

@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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants