Search code examples
pythonpython-3.xpython-click

How to read input from keyboard instead of file in python


I have the below python snippet

@click.argument('file',type=click.Path(exists=True))

The above command read from a file in the below format

python3 code.py file.txt

The same file is processed using a function

def get_domains(domain_names_file):
    with open(domain_names_file) as f:
        domains = f.readlines()
    return domains
domains = get_domains(file)

I dont want to read it from file , I want to provide a domain as an argument while executing in terminal , the command will be

python3 code.py example.com

How should I rewrite the code .

Python Version : 3.8.2


Solution

  • click.argument by default creates arguments that are read from the command line:

    @click.argument('file')
    

    This should create an argument that is read from the command line and made available in the file argument.

    See the docs & examples here