-1

Let's say I have such function:

def say(word):
    print(word)

And I want to run it through CMD in such way:

file.py --word 'Hello world!'

I know it works somehow that way, but I can't find out how it is.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Jansindl3r
  • 360
  • 3
  • 18
  • You mean you want to take command line arguments in Python (see e.g. https://stackoverflow.com/q/4033723/3001761)? Or you don't know how to call the function? Or something else? – jonrsharpe Dec 26 '18 at 23:07
  • 1
    take look at this --> https://www.pythonforbeginners.com/argv/more-fun-with-sys-argv – Dev Dec 26 '18 at 23:07
  • I guess that might be it. It's a task for my studies where I should control a variable inside a program through CMD – Jansindl3r Dec 26 '18 at 23:09

1 Answers1

2

use the argparse module

https://docs.python.org/3/library/argparse.html

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('word', type=str, required=True)
args = parser.parse_args()
print(args.word)  # say(args.word)
Derte Trdelnik
  • 2,656
  • 1
  • 22
  • 26