0

I have to make a program that is called from the command line like this

python3 hello.py <name>

I was able to get it done like this, but it still asks for the name:

def hello(name: str):
    if name == None or name == '':
        return 'Hello, World!'
    else:
        return 'Hello, ' + name


    if __name__ == '__main__':
    print_hello(input("How can I call you?"))
Dusernajder
  • 101
  • 1
  • 14

1 Answers1

2

You can use sys.argv Check the docs

import sys
print_hello(sys.argv[1])
Alexander Santos
  • 1,458
  • 11
  • 22
  • 1
    Editted, used wrong index – Alexander Santos Dec 18 '19 at 18:59
  • What if I want to input multiple arguments? I've tried to use "argv" as an argument ,and called it with a method that has an "*args" parameter, but it throws an error which says: TypeError: can only concatenate str (not "list") to str – Dusernajder Dec 18 '19 at 19:28
  • `sys.argv` returns a list where the first element is the name of the script. You can just iterate this list from `[1:]` and pass that to the method. – Alexander Santos Dec 18 '19 at 19:30