0

If you have idea about linux bash, we can provide arguments in the same line in the terminal.

For Example in sed, the format is,

sed OPTIONS... [SCRIPT] [INPUTFILE...]

Let's say, to greet 'Hello' there is a python program, greet.py

Input in cmd (Suppose, my python file is reg. in env. path)
==========
greet Arun

Output
======
Hello Arun

Wondering, whether this is possible in python, any idea how to?

Arun
  • 27
  • 7
  • In addition to the linked duplicate, you might have tried putting something like `python read command line` [into a search engine](https://duckduckgo.com/?q=python+read+command+line). In fact, just about anything even vaguely related should work in a search engine. – Karl Knechtel Apr 17 '21 at 10:04

2 Answers2

1

I like to use click for commandline tools. Have a look at their documentation.

Here is an example of how it works:

hello.py:

import click

@click.command()
@click.option("--count", default=1, help="Number of greetings.")
@click.option("--name", prompt="Your name", help="The person to greet.")
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for _ in range(count):
        click.echo(f"Hello, {name}!")

if __name__ == '__main__':
    hello()

commandline usage:

$ python hello.py --count=3
Your name: Click
Hello, Click!
Hello, Click!
Hello, Click!
user_na
  • 2,154
  • 1
  • 16
  • 36
0

Yeah, this is possible with the sys library. in the sys.argv list it is present a list of the arguments passed to the python command. For example:

# This is greet.py
import sys
name = sys.argv[1]
print("Hello ", name)

So if you run python greet.py James it will print Hello James

Giuppox
  • 1,393
  • 9
  • 35